using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace wispro.sp.utility
{
    public class EmunHelper
    {
        
        ///// <summary>
        ///// 根据枚举的值获取枚举名称
        ///// </summary>
        ///// <typeparam name="T">枚举类型</typeparam>
        ///// <param name="status">枚举的值</param>
        ///// <returns></returns>
        //public static string GetEnumName<T>(this int status)
        //{
        //    return Enum.GetName(typeof(T), status);
        //}
        /// <summary>
        /// 获取枚举名称集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string[] GetNamesArr<T>()
        {
            return Enum.GetNames(typeof(T));
        }
        /// <summary>
        /// 将枚举转换成描述字典集合
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns></returns>
        public static Dictionary<string, T> getEnumDescriptionDic<T>()
        {
            Dictionary<string, T> resultList = new Dictionary<string, T>();
            Type type = typeof(T);
            var strList = GetNamesArr<T>().ToList();
            foreach (string key in strList)
            {
                var obj = (T)Enum.Parse(type, key);
                FieldInfo field=  obj.GetType().GetField(obj.ToString());

                DescriptionAttribute att = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute), false) as DescriptionAttribute;
                
                if (att == null)
                {
                    resultList.Add(key, obj);
                }
                else
                {
                    resultList.Add(att.Description, obj);
                }
            }
            return resultList;
        }
        /// <summary>
        /// 将枚举转换成字典
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static Dictionary<string, int> GetDic<TEnum>()
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            Type t = typeof(TEnum);
            var arr = Enum.GetValues(t);
            foreach (var item in arr)
            {
                dic.Add(item.ToString(), (int)item);
            }

            return dic;
        }
    }
}
