using Microsoft.Extensions.Configuration;

namespace CrazyStudio.Core.Common
{
    public class Utils
    {
        public IConfiguration Configuration { get; }

        public Utils(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// 逗号分隔的字符串转整型列表
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>返回整型列表</returns>
        public static IList<int> StrToListInt(string str)
        {
            var list = new List<int>();
            if (!str.Contains(","))
            {
                list.Add(int.Parse(str));
                return list;
            }
            var sList = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);    //返回值不包括含有空字符串的数组元素
            foreach (var i in sList)
            {
                list.Add(Convert.ToInt32(i));
            }
            return list;
        }

        /// <summary>
        /// 取子字符串
        /// </summary>
        /// <param name="pSrcString"></param>
        /// <param name="pLength"></param>
        /// <param name="pTailString"></param>
        /// <returns></returns>
        public static string GetSubString(string pSrcString, int pLength, string pTailString)
        {
            return GetSubString(pSrcString, 0, pLength, pTailString);
        }

        public static string GetSubString(string pSrcString, int pStartIndex, int pLength, string pTailString)
        {
            string str = pSrcString;
            byte[] bytes = Encoding.UTF8.GetBytes(pSrcString);
            foreach (char ch in Encoding.UTF8.GetChars(bytes))
            {
                if (((ch > 'ࠀ') && (ch < '一')) || ((ch > 0xac00) && (ch < 0xd7a3)))
                {
                    if (pStartIndex >= pSrcString.Length)
                    {
                        return "";
                    }
                    return pSrcString.Substring(pStartIndex, ((pLength + pStartIndex) > pSrcString.Length) ? (pSrcString.Length - pStartIndex) : pLength);
                }
            }
            if (pLength < 0)
            {
                return str;
            }
            byte[] sourceArray = Encoding.Default.GetBytes(pSrcString);
            if (sourceArray.Length <= pStartIndex)
            {
                return str;
            }
            int length = sourceArray.Length;
            if (sourceArray.Length > (pStartIndex + pLength))
            {
                length = pLength + pStartIndex;
            }
            else
            {
                pLength = sourceArray.Length - pStartIndex;
                pTailString = "";
            }
            int num2 = pLength;
            int[] numArray = new int[pLength];
            byte[]? destinationArray = null;
            int num3 = 0;
            for (int i = pStartIndex; i < length; i++)
            {
                if (sourceArray[i] > 0x7f)
                {
                    num3++;
                    if (num3 == 3)
                    {
                        num3 = 1;
                    }
                }
                else
                {
                    num3 = 0;
                }
                numArray[i] = num3;
            }
            if ((sourceArray[length - 1] > 0x7f) && (numArray[pLength - 1] == 1))
            {
                num2 = pLength + 1;
            }
            destinationArray = new byte[num2];
            Array.Copy(sourceArray, pStartIndex, destinationArray, 0, num2);
            return (Encoding.Default.GetString(destinationArray) + pTailString);
        }

        /// <summary>
        /// 通过流取得前端传过来的传
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static async Task<string> GetStreamDataAsync(Stream stream)
        {
            using (var reader = new StreamReader(stream, Encoding.UTF8))
            {
                var str = reader.ReadToEndAsync();
                return await str;
            }
        }

        /// <summary>
        /// 生成激活码集合
        /// </summary>
        /// <param name="length">码长度</param>
        /// <param name="count">码数量</param>
        /// <param name="useNum">是否包含数字</param>
        /// <param name="useLow">是否包含小写字母</param>
        /// <param name="useUpp">是否包含大写字母</param>
        /// <param name="useSpe">是否包含符号</param>
        /// <param name="custom">自定义字符串</param>
        /// <returns>返回激活码集合</returns>
        public static ISet<string> GetRandomStringCollection(int length, int count, bool useNum = true, bool useLow = true, bool useUpp = true, bool useSpe = true, string custom = "")
        {
            byte[] b = new byte[4];
            //new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
            Random r = new Random(BitConverter.ToInt32(b, 0));
            string s = string.Empty, str = custom;
            if (useNum == true) { str += "23456789"; }
            if (useLow == true) { str += "abcdefghjkmnpqrstuvwxyz"; }
            if (useUpp == true) { str += "ABCDEFGHJKMNPQRSTUVWXYZ"; }
            if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }

            ISet<string> set = new HashSet<string>();
            int idx;
            bool flag = true;
            while (flag)
            {
                for (int i = 0; i < length; i++)
                {
                    s += str.Substring(r.Next(0, str.Length - 1), 1);
                }

                set.Add(s);
                s = string.Empty;
                idx = set.Count;

                if (idx == count)
                {
                    flag = false;
                }
            }

            return set;
        }

        /// <summary>
        /// 取4位随机数作为注册激活码
        /// </summary>
        public static int GetRandomCode()
        {
            return new Random().Next(1000, 10000);
        }
        /// <summary>   
        /// 将指定字符串按指定长度进行剪切，   
        /// </summary>   
        /// <param name="str"> 需要截断的字符串 </param>   
        /// <param name="maxLength"> 字符串的最大长度 </param>   
        /// <param name="endWith"> 超过长度的后缀 </param>   
        /// <returns> 如果超过长度，返回截断后的新字符串加上后缀，否则，返回原字符串 </returns>   
        public static string StringTruncat(string str, int maxLength, string endWith="")
        {
            if (string.IsNullOrEmpty(str))
            {
                return str + endWith;
            }
            if (maxLength < 1)
            {
                return str;
            }
            if (str.Length > maxLength)
            {
                string strTmp = str.Substring(0, maxLength);
                if (string.IsNullOrEmpty(endWith))
                    return strTmp;
                else
                    return strTmp + endWith;
            }
            return str;
        }
        /// <summary>
        /// 前后指定的字符显示，其他用星号标识
        /// </summary>
        /// <param name="str">原字符</param>
        /// <param name="start">截取前几位字符</param>
        /// <param name="end">截取后几位字符</param>
        /// <returns></returns>
        public static string HideString(string str, int start, int end) 
        {
            string mergeStr = string.Empty;
            try
            {
                str = Regex.Replace(str, @"\s", "");
                if (string.IsNullOrEmpty(str) || str.Length < start)
                {
                    return str;
                }
                if (str.Length > start)
                {
                    mergeStr = str.Substring(0, start) + "***";
                    mergeStr += str.Length - end > end ? str.Substring(str.Length - end, end) : "";
                    return mergeStr;
                }
            }
            catch
            {
                return "***";
            }
            return mergeStr;
        }
    }
}
