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

namespace Common
{
    public static class EnumHelper
    {
        /// <summary>
        /// 私有,,获取彩种配置的方法
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<EnumModel> GetEnumList<T>()
        {
            var list = new List<EnumModel>();
            foreach (var field in typeof(T).GetFields())
            {
                if (!field.IsSpecialName)
                {
                    var d = field.GetCustomAttributes(typeof(DescriptionAttribute), true);
                    var o = field.GetCustomAttributes(typeof(OtherAttribute), true);
                    list.Add(new EnumModel
                    {
                        Key = field.Name,
                        Value = (int)field.GetRawConstantValue(),
                        Description = d.Count() > 0 ? (d[0] as DescriptionAttribute).Description : "",
                        Other = o.Count() > 0 ? (o[0] as OtherAttribute).Name : "",
                    });
                }
            }
            return list;

        }

        /// <summary>
        /// 通过key获取EnumModel
        /// </summary>
        /// <param name="list"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static EnumModel GetEnumModelByKey(this List<EnumModel> list, string value)
        {
            return list.Where(p => p.Key == value).FirstOrDefault();
        }

        public static EnumModel GetEnumModelByKey(this List<EnumModel> list, Enum value)
        {
            return list.Where(p => p.Key == value.ToString()).FirstOrDefault();
        }

        /// <summary>
        /// 根据EnumModel的Enum获取相应的详情
        /// </summary>
        /// <param name="list"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetEnumDescription(this List<EnumModel> list, Enum value)
        {
            return list.Where(p => p.Key == value.ToString()).FirstOrDefault().Description;
        }

       
    }


}