using DbCommon.Enties.DbModels;
using ProjectManagementSystem.Common.Config;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Common.Extenions
{
    public static class StringExtenions
    {
        public static T[] ToValueArray<T>(this string str)
        {
            return string.IsNullOrEmpty(str) ? new T[0] :
                str.Trim().Split(new char[] { ';', ',', '|' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(s =>
                {
                    return s.ToValue<T>();
                })
                .ToArray();
        }

        public static T[] ToValueArray<T>(this string str, params char[] separator)
        {
            return string.IsNullOrEmpty(str) ? new T[0] :
                str.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries)
                .Select(s =>
                {
                    return s.ToValue<T>();
                })
                .ToArray();
        }

        public static T ToValue<T>(this string str)
        {
            if (string.IsNullOrEmpty(str)) return default(T);

            try
            {
                var type = typeof(T);
                if (type.Name == "Boolean")
                {
                    var value = str == "1" || str == "True" || str == "true" ? true : false;
                    return (T)Convert.ChangeType(value, type);
                }
                else
                {
                    return (T)Convert.ChangeType(str, type);
                }
            }
            catch (Exception ex)
            {
                Logger.CLog.Instance.SystemLog.WriteException(str, ex);
                throw ex;
            }

        }

        public static T ToValue<T>(this object obj)
        {
            if (obj == null) return default(T);
            return (T)Convert.ChangeType(obj, typeof(T));
        }

        public static string ToChineseString(this bool value)
        {
            return value ? "成功" : "失败";
        }

        public static string ToShortPosString(this string str)
        {
            var posArr = str.ToValueArray<string>();
            if (posArr == null) return null;

            List<string> shortPosList = new List<string>();
            for (int i = 0; i < posArr.Length; i++)
            {
                if (i > 0)
                {
                    if (posArr[i] != posArr[i - 1])
                    {
                        shortPosList.Add(posArr[i]);
                    }
                }
                else
                {
                    shortPosList.Add(posArr[i]);
                }
            }
            return string.Join(",", shortPosList);
        }

        public static LocationStatus ToLocationStatus(this string status)
        {
            if (string.IsNullOrEmpty(status)) return LocationStatus.None;

            int tempValue;
            if (int.TryParse(status, out tempValue))
            {
                return (LocationStatus)tempValue;
            }
            return LocationStatus.None;
        }

        public static string ToStatusString(this LocationStatus status)
        {
            string statusStr = null;
            switch (status)
            {
                case LocationStatus.None:
                    statusStr = "未知";
                    break;
                case LocationStatus.Filled:
                    statusStr = "满";
                    break;
                case LocationStatus.Empty:
                    statusStr = "空";
                    break;
                case LocationStatus.Locked:
                    statusStr = "锁定";
                    break;
                case LocationStatus.LineCall:
                    statusStr = "叫料";
                    break;
                case LocationStatus.Exception:
                    statusStr = "异常";
                    break;
                default:
                    statusStr = "未知";
                    break;
            }
            return statusStr;
        }

        public static Color ToColor(this LocationStatus status)
        {
            return Function.ColorSetting.Instance.GetColor(status);
        }

        public static string ToHexString(this byte[] bytes)
        {
            return string.Join(" ", bytes.Select(d => d.ToString("X2")));
        }

        public static string GetLocationMember(this string location, string member)
        {
            return ExcelConfig.Instance.GetLocationMemberCache(location, member);
        }

        public static bool IsChineseText(this string addrDesc)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(addrDesc, @"[\u4e00-\u9fa5]");
        }

        public static string GetPlcTaskAddr(this string location, string addrDesc)
        {
            if (!addrDesc.IsChineseText())
            {
                return addrDesc;
            }

            return location.GetLocationMember(addrDesc);
        }

        public static bool GetBitVaule(this ushort vaule, int bit)
        {
            return (vaule & (0x01 << bit)) > 0;
        }

        public static bool GetBitVaule(this byte vaule, int bit)
        {
            return (vaule & (0x01 << bit)) > 0;
        }

        public static T GetArrayValue<T>(this string str, int index)
        {
            if (string.IsNullOrEmpty(str)) return default(T);

            var array = str.ToValueArray<T>();
            if (array.Length == 0) return default(T);

            return array.Length > index ? array[index] : array[array.Length - 1];
        }
    }
}
