using System.ComponentModel;
using System.Reflection;
namespace WIDESEA_Core.Enums
{
    public static class EnumHelper
    {
        ///   
        /// 枚举转字典集合  
        ///   
        /// 枚举类名称  
        /// 默认key值  
        /// 默认value值  
        /// 返回生成的字典集合  
        public static Dictionary EnumListDic(string keyDefault, string valueDefault = "")
        {
            Dictionary dicEnum = new Dictionary();
            Type enumType = typeof(T);
            if (!enumType.IsEnum)
            {
                return dicEnum;
            }
            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项  
            {
                dicEnum.Add(keyDefault, valueDefault);
            }
            string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组  
            foreach (var item in fieldstrs)
            {
                string description = string.Empty;
                var field = enumType.GetField(item);
                object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组  
                if (arr != null && arr.Length > 0)
                {
                    description = ((DescriptionAttribute)arr[0]).Description;   //属性描述  
                }
                else
                {
                    description = item;  //描述不存在取字段名称  
                }
                dicEnum.Add(description, (int)Enum.Parse(enumType, item));  //不用枚举的value值作为字典key值的原因从枚举例子能看出来,其实这边应该判断他的值不存在,默认取字段名称  
            }
            return dicEnum;
        }
        /// 
        /// 获取枚举项描述信息 例如GetEnumDesc(Days.Sunday)
        /// 
        /// 枚举项 如Days.Sunday
        /// 
        public static string GetIntegralRuleTypeEnumDesc(this Enum en)
        {
            Type type = en.GetType();
            MemberInfo[] memInfo = type.GetMember(en.ToString());
            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if (attrs != null && attrs.Length > 0)
                    return ((DescriptionAttribute)attrs[0]).Description;
            }
            return en.ToString();
        }
        /// 
        /// 获取枚举集合
        /// 
        /// 枚举类名称
        /// 
        public static IEnumerable GetEnumList()
        {
            var model = default(T);
            FieldInfo[] fieldinfo = typeof(T).GetFields();
            List result = new List();
            foreach (FieldInfo field in fieldinfo)
            {
                EnumModel enumModel = new EnumModel();
                if (!(Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute))
                {
                    enumModel.Desc = field.GetValue(model).ToString();
                }
                else
                {
                    enumModel.Desc = attribute.Description;
                }
                enumModel.Value = field.GetValue(model).GetHashCode();
                enumModel.Key = field.GetValue(model) as ValueType;
                if (field.GetValue(model).ToString() != "0")
                {
                    result.Add(enumModel);
                }
            }
            return result;
        }
    }
    public class EnumModel
    {
        /// 
        /// Enum的值
        /// 
        public int Value { get; set; }
        /// 
        /// Enum的key
        /// 
        public ValueType Key { get; set; }
        /// 
        /// Enum描述
        /// 
        public string Desc { get; set; }
    }
}