using System;
using System.Collections.Generic;
using System.Text;

namespace Apewer.Internals
{

    internal class TextGenerator
    {

        /// <summary>生成新的 GUID。</summary>
        public static string NewGuid(bool argHyphenation = false, bool argLowerCase = true)
        {
            var vguid = Guid.NewGuid().ToString();
            if (argLowerCase) vguid = vguid.ToLower();
            else vguid = vguid.ToUpper();
            if (!argHyphenation) vguid = vguid.Replace("-", "");
            return vguid;
        }

        /// <summary>合并字符串。</summary>
        public static string Merge(IEnumerable<string> argStrings)
        {
            if (argStrings == null) return "";
            var vsb = new StringBuilder();
            foreach (var vstring in argStrings)
            {
                if (string.IsNullOrEmpty(vstring)) continue;
                vsb.Append(vstring);
            }
            return vsb.ToString();
        }

        /// <summary>合并字符串。</summary>
        public static string Merge(params string[] argStrings)
        {
            if (argStrings == null) return "";
            var vsb = new StringBuilder();
            foreach (var vstring in argStrings)
            {
                if (string.IsNullOrEmpty(vstring)) continue;
                vsb.Append(vstring);
            }
            return vsb.ToString();
        }

        /// <summary>合并字符串。</summary>
        public static string Merge(IEnumerable<char> argChars)
        {
            if (argChars == null) return "";
            var vsb = new StringBuilder();
            foreach (var vchar in argChars)
            {
                vsb.Append(vchar.ToString());
            }
            return vsb.ToString();
        }

        /// <summary>合并字符串。</summary>
        public static string Merge(params char[] argChars)
        {
            if (argChars == null) return "";
            var vsb = new StringBuilder();
            foreach (var vchar in argChars)
            {
                vsb.Append(vchar.ToString());
            }
            return vsb.ToString();
        }

        private static int Asc(char argChar)
        {
            int num = Convert.ToInt32(argChar);
            if (num < 128) return num;
            int result = 0;
            try
            {
                Encoding fileIOEncoding = Encoding.Default;
                char[] chars = new char[] { argChar };
                if (fileIOEncoding.IsSingleByte)
                {
                    byte[] array = new byte[1];
                    int bytes = fileIOEncoding.GetBytes(chars, 0, 1, array, 0);
                    result = (int)array[0];
                }
                else
                {
                    byte[] array = new byte[2];
                    int bytes = fileIOEncoding.GetBytes(chars, 0, 1, array, 0);
                    if (bytes == 1)
                    {
                        result = (int)array[0];
                    }
                    else
                    {
                        if (BitConverter.IsLittleEndian)
                        {
                            byte b = array[0];
                            array[0] = array[1];
                            array[1] = b;
                        }
                        result = (int)BitConverter.ToInt16(array, 0);
                    }
                }
            }
            catch { }
            return result;
        }

        /// <summary>按字符编码获取字符。</summary>
        public static char Chr(int argCode)
        {
            if (argCode < -32768 || argCode > 65535) return ' ';
            if (argCode >= 0 && argCode <= 127) return Convert.ToChar(argCode);
            checked
            {
                char result;
                try
                {
                    Encoding encoding = Encoding.Default;
                    if (encoding.IsSingleByte && (argCode < 0 || argCode > 255))
                    {
                        return ' ';
                    }
                    char[] array = new char[2];
                    byte[] array2 = new byte[2];
                    Decoder decoder = encoding.GetDecoder();
                    if (argCode >= 0 && argCode <= 255)
                    {
                        array2[0] = (byte)(argCode & 255);
                        int chars = decoder.GetChars(array2, 0, 1, array, 0);
                    }
                    else
                    {
                        array2[0] = (byte)((argCode & 65280) >> 8);
                        array2[1] = (byte)(argCode & 255);
                        int chars = decoder.GetChars(array2, 0, 2, array, 0);
                    }
                    result = array[0];
                }
                catch { return ' '; }
                return result;
            }
        }

        /// <summary>获取 ASCII 字符。</summary>
        /// <param name="argCode">ASCII 值。</param>
        public static string Ascii(int argCode)
        {
            try { return Chr(argCode).ToString(); }
            catch { return ""; }
        }

        /// <summary>获取范围内 ASCII 值连续的字符。</summary>
        /// <param name="argBegin">范围起始 ASCII 值。</param>
        /// <param name="argEnd">范围结束 ASCII 值。</param>
        public static string Ascii(int argBegin, int argEnd)
        {
            try
            {
                int vb = argBegin;
                int ve = argEnd;
                if (vb == ve) return Ascii(vb);
                else
                {
                    var vr = new TextBuilder();
                    if (vb < ve)
                    {
                        for (int i = vb; i <= ve; i++) vr.Append(Ascii(i));
                    }
                    else
                    {
                        for (int i = vb; i >= ve; i--) vr.Append(Ascii(i));
                    }
                    return vr.Value;
                }
            }
            catch { return ""; }
        }

        /// <summary>重复指定子字符串,直到达到指定长度。</summary>
        /// <param name="argCell">子字符串。</param>
        /// <param name="argLength">目标字符串的长度。</param>
        public static string CopyChar(string argCell, int argLength)
        {
            if (argLength > 0)
            {
                var vcell = string.IsNullOrEmpty(argCell) ? " " : argCell;
                var vtext = new TextBuilder();
                while (vtext.Length < argLength) vtext.Append(vcell);
                var vresult = vtext.Value;
                if (vresult.Length > argLength) vresult = vresult.Substring(0, argLength);
                return vresult;
            }
            return "";
        }

        /// <summary>获取指定长的的空格。</summary>
        public static string Space(int argLength)
        {
            return CopyChar(" ", argLength);
        }

        /// <summary>获取首字母大写的拼音。</summary>
        private static string GetHeadLetterByString(string argText)
        {
            string tempStr = "";
            foreach (char vchar in argText)
            {
                if ((int)vchar >= 33 && (int)vchar <= 126)
                {//字母和符号原样保留
                    tempStr += vchar.ToString();
                }
                else
                {//累加拼音声母
                    tempStr += GetHeadLetterByChar(vchar.ToString());
                }
            }
            return tempStr;
        }

        /// <summary>取单个字符的拼音声母</summary>
        /// <param name="argChar">要转换的单个汉字</param>
        /// <returns>拼音首字母</returns>
        public static string GetHeadLetterByChar(string argChar)
        {
            byte[] array = new byte[2];
            array = Encoding.Default.GetBytes(argChar);
            int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
            if (i < 0xB0A1) return "*";
            if (i < 0xB0C5) return "a";
            if (i < 0xB2C1) return "b";
            if (i < 0xB4EE) return "c";
            if (i < 0xB6EA) return "d";
            if (i < 0xB7A2) return "e";
            if (i < 0xB8C1) return "f";
            if (i < 0xB9FE) return "g";
            if (i < 0xBBF7) return "h";
            if (i < 0xBFA6) return "j";
            if (i < 0xC0AC) return "k";
            if (i < 0xC2E8) return "l";
            if (i < 0xC4C3) return "m";
            if (i < 0xC5B6) return "n";
            if (i < 0xC5BE) return "o";
            if (i < 0xC6DA) return "p";
            if (i < 0xC8BB) return "q";
            if (i < 0xC8F6) return "r";
            if (i < 0xCBFA) return "s";
            if (i < 0xCDDA) return "t";
            if (i < 0xCEF4) return "w";
            if (i < 0xD1B9) return "x";
            if (i < 0xD4D1) return "y";
            if (i < 0xD7FA) return "z";
            return "*";
        }

    }

}