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

namespace TEAMModelOS.SDK.Helper.Common.StringHelper
{
    public class StringHelper
    {
        #region 截取字符串
        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);
        }
        #endregion

        /// <summary>
        /// 将字符串转换为int类型数组
        /// </summary>
        /// <param name="str">如1,2,3,4,5</param>
        /// <returns></returns>
        public static List<string> StrToListString(string str)
        {
            var list = new List<string>();
            if (!str.Contains(","))
            {
                list.Add(str);
                return list;
            }
            var slist = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in slist)
            {
                list.Add(item);
            }
            return list;
        }

        /// <summary>
        /// 截取两个字符串中间的字符串
        /// </summary>
        /// <param name="sourse"></param>
        /// <param name="startstr"></param>
        /// <param name="endstr"></param>
        /// <returns></returns>
        public static string SubMidString(string sourse, string startstr, string endstr)
        {
            string result = string.Empty;
            int startindex, endindex;
            startindex = sourse.IndexOf(startstr);
            if (startindex == -1)
                return result;
            string tmpstr = sourse.Substring(startindex + startstr.Length);
            endindex = tmpstr.IndexOf(endstr);
            if (endindex == -1)
                return result;
            result = tmpstr.Remove(endindex);
            return result;
        }
        public static double[] ListTodouble(List<double> list)
        {
            Double[] doubles = new Double[list.Count];
            int k = 0;
            foreach (double p in list)
            {
                doubles[k] = p;
                k++;
            }
            //doubles = list;
            if (doubles == null)
            {
                return null;
            }
            double[] result = new double[doubles.Length];
            for (int i = 0; i < doubles.Length; i++)
            {
                result[i] = doubles[i];
            }
            return result;
        }
        public static double[,] ListToDouble(List<List<double>> list)
        {
            int itemCount = 0;
            //int stuCount = list.Count;
            foreach (List<double> p in list)
            {
                if (p.Count != 0)
                {
                    itemCount = p.Count;
                    break;
                }

            }
            for (int k = 0; k < list.Count; k++)
            {
                if (list[k].Count == 0)
                {
                    list.Remove(list[k]);
                }
            }
            double[,] doubles = new double[list.Count, itemCount];
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < itemCount; j++)
                {
                    doubles[i, j] = list[i][j];
                }
            }
            return doubles;
        }

        public static int getKeyCount(JsonElement element) {
            int keys = 0;
            var emobj = element.EnumerateObject();
            while (emobj.MoveNext())
            {
                keys++;
            }
            return keys;
        }
        public static string getEduName(string key) {
            string name = "";
            switch (key) {
                case "0":
                    name = "知识";
                    break;
                case "1":
                    name = "理解";
                    break;
                case "2":
                    name = "应用";
                    break;
                case "3":
                    name = "分析";
                    break;
                case "4":
                    name = "综合";
                    break;
                case "5":
                    name = "评鉴";
                    break;
            }
            return name;
        }
        public static string getTypeName(string key)
        {
            string name = "";
            switch (key)
            {
                case "0":
                    name = "Single";
                    break;
                case "1":
                    name = "Multiple";
                    break;
                case "2":
                    name = "Judge";
                    break;
                case "3":
                    name = "Complete";
                    break;
            }
            return name;
        }
    }
}