using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;

namespace XKRS.Common.Model.Helper
{
    public static class EnumHelper
    {


        /// <summary>
        /// 获取具体某一枚举的中文描述
        /// </summary>
        /// <param name="enumObj"></param>
        /// <returns></returns>
        public static string GetEnumDescription(this Enum enumObj)
        {
            Type t = enumObj.GetType();
            FieldInfo f = t.GetField(enumObj.ToString());
            if (f == null)
            {
                return enumObj.ToString();
            }
            DescriptionAttribute attr = f.GetCustomAttribute<DescriptionAttribute>();
            if (attr != null)
            {
                return attr.Description;
            }
            else
            {
                return enumObj.ToString();
            }
        }




        public static System.Drawing.Color GetEnumSelectedColor(this Enum enumObj)
        {
            Type t = enumObj.GetType();
            FieldInfo f = t.GetField(enumObj.ToString());
            ColorSelectAttribute attr = f.GetCustomAttribute<ColorSelectAttribute>();
            if (attr != null)
            {
                return System.Drawing.Color.FromName(attr.SelectdColor);
            }
            else
            {
                return System.Drawing.Color.Transparent;
            }

        }
        public static System.Drawing.Color GetEnumSelectedFontColor(this Enum enumObj)
        {
            Type t = enumObj.GetType();
            FieldInfo f = t.GetField(enumObj.ToString());

            var attr = f.GetCustomAttribute<FontColorSelectAttribute>();
            if (attr != null)
            {
                return System.Drawing.Color.FromName(attr.SelectedColor);
            }
            else
            {
                return System.Drawing.Color.Transparent;
            }
        }

        /// <summary>
        /// 设备状态定义
        /// 未初始化和异常状态无前置状态要求
        /// 初始化操作前置状态必须是未初始化、关闭状态、异常状态
        /// 打开前置必须是初始化和暂停
        /// 关闭前置必须是打开和暂停和异常
        /// 暂停前置必须是打开
        /// </summary>
        public enum DeviceState
        {
            TBD = -1,

            [ColorSelect("Gray")]
            [FontColorSelect("Black")]
            [Description("未初始化")]
            DSUninit = 1,

            [ColorSelect("Gold")]
            [FontColorSelect("White")]
            [PreState(1 + 2 + 4 + 8 + 32)]
            [Description("初始化")]
            DSInit = 2,

            [ColorSelect("Lime")]
            [FontColorSelect("Black")]
            [PreState(2 + 4 + 16)]
            [Description("运行中")]
            DSOpen = 4,

            [ColorSelect("Gray")]
            [FontColorSelect("White")]
            [PreState(1 + 4 + 8 + 16 + 32)]
            [Description("关闭")]
            DSClose = 8,

            [ColorSelect("Gold")]
            [FontColorSelect("White")]
            [PreState(4 + 16)]
            [Description("暂停")]
            DSPause = 16,

            [ColorSelect("Red")]
            [FontColorSelect("White")]
            [Description("异常")]
            DSExcept = 32
        }

        [Flags]//将枚举视为位域
        public enum DeviceAttributeType
        {


            [Description("运行控件")]
            RunCtrl = 512,
        }

        public enum LogLevel
        {
            [Description("辅助")]
            [ColorSelect("White")]
            [FontColorSelect("Blue")]
            Assist = 1,
            [Description("详细")]
            [ColorSelect("White")]
            [FontColorSelect("Green")]
            Detail = 2,
            [Description("信息")]
            [ColorSelect("White")]
            [FontColorSelect("Dark")]
            Information = 3,
            [Description("动作")]
            [ColorSelect("White")]
            [FontColorSelect("Blue")]
            Action = 4,
            [Description("错误")]
            [ColorSelect("White")]
            [FontColorSelect("Blue")]
            Error = 5,
            [Description("警报")]
            [ColorSelect("White")]
            [FontColorSelect("Blue")]
            Warning = 6,
            [Description("异常")]
            [ColorSelect("White")]
            [FontColorSelect("Blue")]
            Exception = 7,

        }
    }
}
