using HighWayIot.Plc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace HighWayIot.Winform.Business
{
    public class GeneralUtils
    {
        /// <summary>
        /// 在字符串数组前面加一个空字符串
        /// </summary>
        /// <param name="str"></param>
        public static string[] HeadAddEmptyString(string[] str)
        {
            Array.Resize(ref str, str.Length + 1);
            Array.Copy(str, 0, str, 1, str.Length - 1);
            str[0] = string.Empty;
            return str;
        }

        /// <summary>
        /// 转换string为int
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static int? StringNullOrToInt(string text)
        {
            return int.TryParse(text, out int result) ? (int?)result : null;
        }

        public static string IntEmptyOrToString(int? value)
        {
            if (value == null)
            {
                return string.Empty;
            }
            else
            {
                return value.ToString();
            }
        }

        /// <summary>
        /// 返回对应的枚举类Type类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static Type GetTypeByEnum(DataTypeEnum type)
        {
            switch (type)
            {
                case DataTypeEnum.Bool:
                    return typeof(bool);
                case DataTypeEnum.Byte:
                    return typeof(byte);
                case DataTypeEnum.Int16:
                    return typeof(short);
                case DataTypeEnum.UInt16:
                    return typeof(ushort);
                case DataTypeEnum.Int32:
                    return typeof(int);
                case DataTypeEnum.UInt32:
                    return typeof(uint);
                case DataTypeEnum.Int64:
                    return typeof(long);
                case DataTypeEnum.UInt64:
                    return typeof(ulong);
                case DataTypeEnum.Float:
                    return typeof(float);
                case DataTypeEnum.Double:
                    return typeof(double);
                default:
                    return default;
            }
        }

        /// <summary>
        /// 获取枚举类的键值对
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IEnumerable<KeyValuePair<string, int>> GetEnumKeyValuePairs<T>() where T : Enum
        {
            var enumType = typeof(T);
            var fields = enumType.GetFields();

            foreach (var fi in fields)
            {
                if (fi.FieldType != enumType || !fi.IsLiteral)
                    continue;

                var name = fi.Name;
                var value = (int)enumType.GetField(name).GetValue(null);
                yield return new KeyValuePair<string, int>(name, value);
            }
        }

        /// <summary>
        /// 获取枚举类值的description元数据(没有Des返回名字)
        /// </summary>
        /// <returns></returns>
        public static string GetEnumStringDescription(Enum enumValue)
        {
            string value = enumValue.ToString();
            FieldInfo field = enumValue.GetType().GetField(value);
            object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
            if (objs.Length == 0)    //当描述属性没有时，直接返回名称
                return value;
            DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
            return descriptionAttribute.Description;
        }

        /// <summary>
        /// 将DateTime间隔转换为字符串格式
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime? dateTimeStart, DateTime? dateTimeEnd)
        {
            if(dateTimeEnd == null || dateTimeStart == null)
            {
                return string.Empty;
            }
            TimeSpan span = (TimeSpan)(dateTimeEnd - dateTimeStart);
            string result;
            if (span > TimeSpan.FromSeconds(60))
            {
                int spanMinutes = (int)span.TotalMinutes;
                int spanSeconds = (int)span.TotalSeconds % 60;
                result = $"{spanMinutes} 分 {spanSeconds} 秒";
            }
            else
            {
                int spanSeconds = (int)span.TotalSeconds % 60;
                result = $"{spanSeconds} 秒";
            }
            return result;
        }

    }
}
