namespace PlcSiemens.Core.Extension
{
    /// <summary>枚举类型助手类</summary>
    public static class EnumHelper
    {
        /// <summary>枚举变量是否包含指定标识</summary>
        public static bool Has(this Enum value, Enum flag)
        {
            if (value.GetType() != flag.GetType()) throw new ArgumentException("flag", "枚举标识判断必须是相同的类型！");

            ulong num = Convert.ToUInt64(flag);
            return (Convert.ToUInt64(value) & num) == num;
        }

        /// <summary>设置标识位</summary>
        public static T Set<T>(this Enum source, T flag, bool value)
        {
            if (!(source is T)) throw new ArgumentException("source", "枚举标识判断必须是相同的类型！");

            ulong s = Convert.ToUInt64(source);
            ulong f = Convert.ToUInt64(flag);

            if (value)
            {
                s |= f;
            }
            else
            {
                s &= ~f;
            }

            return (T)Enum.ToObject(typeof(T), s);
        }

        /// <summary>获取枚举字段的注释</summary>
        //public static string GetDescription(this Enum value)
        //{
        //    var type = value.GetType();
        //    var item = type.GetField(value.ToString(), BindingFlags.Public | BindingFlags.Static);

        //    var att = item.GetCustomAttribute<DescriptionAttribute>(false);
        //    if (att != null && !string.IsNullOrEmpty(att.Description)) return att.Description;

        //    return null;
        //}

        ///// <summary>获取枚举类型的所有字段注释</summary>
        //public static Dictionary<TEnum, string> GetDescriptions<TEnum>()
        //{
        //    var dic = new Dictionary<TEnum, string>();

        //    foreach (var item in GetDescriptions(typeof(TEnum)))
        //    {
        //        dic.Add((TEnum)(object)item.Key, item.Value);
        //    }

        //    return dic;
        //}

        ///// <summary>获取枚举类型的所有字段注释</summary>
        //public static Dictionary<int, string> GetDescriptions(Type enumType)
        //{
        //    var dic = new Dictionary<int, string>();
        //    foreach (var item in enumType.GetFields(BindingFlags.Public | BindingFlags.Static))
        //    {
        //        if (!item.IsStatic) continue;

        //        // 这里的快速访问方法会报错
        //        //FieldInfoX fix = FieldInfoX.Create(item);
        //        //PermissionFlags value = (PermissionFlags)fix.GetValue(null);
        //        int value = Convert.ToInt32(item.GetValue(null));

        //        string des = item.Name;

        //        //var dna = AttributeX.GetCustomAttribute<DisplayNameAttribute>(item, false);
        //        var dna = item.GetCustomAttribute<DisplayNameAttribute>(false);
        //        if (dna != null && !string.IsNullOrEmpty(dna.DisplayName)) des = dna.DisplayName;

        //        //var att = AttributeX.GetCustomAttribute<DescriptionAttribute>(item, false);
        //        var att = item.GetCustomAttribute<DescriptionAttribute>(false);
        //        if (att != null && !string.IsNullOrEmpty(att.Description)) des = att.Description;
        //        //dic.Add(value, des);
        //        // 有些枚举可能不同名称有相同的值
        //        dic[value] = des;
        //    }

        //    return dic;
        //}
    }
}
