using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Globalization;
using System.Threading;
using System.Text.RegularExpressions;

namespace ChangeCasePart2
{
    public static class StringExtensions
    {
        // Convert the string to Pascal case.
        public static string ToPascalCase(this string theString)
        {
            // If the string is null, just return it.
            if (theString == null) return theString;

            // If there are 0 or 1 characters, return the string capitalized.
            if (theString.Length < 2) return theString.ToUpper();

            TextInfo info = Thread.CurrentThread.CurrentCulture.TextInfo;
            theString = info.ToTitleCase(theString);
            return theString.Replace(" ", "");
        }

        // Convert the string to camel case.
        public static string ToCamelCase(this string theString)
        {
            // If the string is null, just return it.
            if (theString == null) return theString;

            // If there are 0 or 1 characters, return the string in lowercase.
            if (theString.Length < 2) return theString.ToLower();

            // Convert to Pascal case.
            theString = theString.ToPascalCase();

            // Convert the first character to lowercase and return the string.
            return theString.Substring(0, 1).ToLower() + theString.Substring(1);
        }

        // With input a Pascal cased or camel cased string,
        // capitalize the first character and add a space before
        // each following capitalized letter.
        public static string ToProperCase(this string theString)
        {
            // If the string is null, just return it.
            if (theString == null) return theString;

            // If there are 0 or 1 characters, return the string capitalized.
            if (theString.Length < 2) return theString.ToUpper();

            const string pattern = @"(?<=\w)(?=[A-Z])";
            //const string pattern = @"(?<=[^A-Z])(?=[A-Z])";
            string result = Regex.Replace(theString, pattern, " ", RegexOptions.None);
            return result.Substring(0, 1).ToUpper() + result.Substring(1);
        }
    }
}
