using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProductionControl.Utils
{
    public static class EnumUtil
    {

        /// <summary>
        /// 动态创建枚举
        /// </summary>
        /// <param name="enumDictionary">枚举元素列表</param>
        /// <param name="enumName">枚举名</param>
        /// <returns>Enum枚举</returns>
        public static Enum CreateEnum(Dictionary<string, int> enumDictionary, string enumName = "DefalutEnum")
        {
            if (enumDictionary == null || enumDictionary.Count <= 0)
                return null;

            AppDomain currentDomain = AppDomain.CurrentDomain;
            AssemblyName aName = new AssemblyName("TempAssembly");
            AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
            ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
            if (string.IsNullOrWhiteSpace(enumName))
            {
                enumName = "DefalutEnum";
            }
            EnumBuilder eb = mb.DefineEnum(enumName, TypeAttributes.Public, typeof(int));

            foreach (var item in enumDictionary)
            {
                eb.DefineLiteral(item.Key, item.Value);
            }

            Type finished = eb.CreateType();
            return (Enum)Enum.Parse(finished, enumName);
            //ab.Save(aName.Name + ".dll");
            //Enum.GetValues(finished)
            //Enum eEnum = System.Reflection(finished) as Enum;
            //foreach (object item in Enum.GetValues(eEnum.GetType()))
            //{
            //    Debug.LogError(string.Format("{0}.{1} = {2}", finished, item, ((int)item)));
            //}
            //return eEnum;
        }


        /// <summary>
        /// 动态创建枚举
        /// </summary>
        /// <param name="enumDictionary">枚举元素列表</param>
        /// <param name="enumName">枚举名</param>
        /// <returns>Enum枚举</returns>
        public static Enum CreateEnum(List<string> enumList, string enumName = "DefalutEnum")
        {
            if (enumList == null || enumList.Count <= 0)
                return null;

            AppDomain currentDomain = AppDomain.CurrentDomain;
            AssemblyName aName = new AssemblyName("TempAssembly");
            AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
            ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
            if (string.IsNullOrWhiteSpace(enumName))
            {
                enumName = "DefalutEnum";
            }
            EnumBuilder eb = mb.DefineEnum(enumName, TypeAttributes.Public, typeof(int));

            for (int i = 0; i < enumList.Count; i++)
            {
                eb.DefineLiteral(enumList[i], i);
            }
            Type finished = eb.CreateType();
            return (Enum)Enum.Parse(finished, enumName);
            //Enum eEnum = Reflection.GetDefault(finished) as Enum;
            //foreach (object item in Enum.GetValues(eEnum.GetType()))
            //{
            //    Debug.LogError(string.Format("{0}.{1} = {2}", finished, item, ((int)item)));
            //}
            //return eEnum;
        }

        /// <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, int> getDic<T>()
        {

            Dictionary<string, int> resultList = new Dictionary<string, int>();
            Type type = typeof(T);
            //var strList = GetNamesArr<T>().ToList();
            //foreach (string key in strList)
            //{
            //    string val = Enum.Format(type, Enum.Parse(type, key), "d");
            //    resultList.Add(key, int.Parse(val));
            //}
            foreach (int value in Enum.GetValues(type))
            {
                string strName = Enum.GetName(type, value);//获取名称
                //string strVaule = value.ToString();//获取值
                resultList.Add(strName, value);
            }
            return resultList;
        }
        /// <summary>
        /// 将枚举转换成字典
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static Dictionary<string, int> getDic2<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;
        }
        public static ArrayList GetArrayList<T>()
        {
            ArrayList list = new ArrayList();
            Type type = typeof(T);
            //list.Add(new DictionaryEntry("start", "启动按钮"));
            foreach (int value in Enum.GetValues(type))
            {
                string strName = Enum.GetName(type, value);//获取名称 
                list.Add(new DictionaryEntry(value, strName));
            }

            return list;
        }
        /// <summary>
        /// 通过Enum的名称转为Enum类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="codeName"></param>
        /// <returns></returns>
        public static T Convert2Enum<T>(string codeName)
        {
            T myEnum = (T)Enum.Parse(typeof(T), codeName);
            return  myEnum;
        }

        #region 绑定到ComboBox
        /// <summary>
        /// 绑定ComboBoxEx数据源到枚举类型
        /// </summary>
        /// <param name="cmb"></param>
        /// <param name="enumType"></param>
        /// <param name="selectIndex"></param>
        public static void BindToEnumName<T>(ComboBox cmb, Type enumType, T value)
        {
            cmb.DataSource = Enum.GetNames(enumType);
            if(value!=null)
                SetSelectedItemToEnum(cmb, value);
        }

        /// <summary>
        ///  获取选择项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmb"></param>
        /// <returns></returns>
        public static T GetSelectedItemToEnum<T>(this ComboBox cmb)
        {
            return (T)Enum.Parse(typeof(T), cmb.SelectedItem.ToString(), false);
        }

        /// <summary>
        /// 设置选定项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmb"></param>
        /// <param name="t"></param>
        public static void SetSelectedItemToEnum<T>(this ComboBox cmb, T t)
        {
            cmb.SelectedItem = t.ToString();
        }
        #endregion
    }
}
