using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace XYY.Core.Standard.Mvc
{
    public class EnumItem
    {
        public string Name
        {
            get; set;
        }

        public int Value
        {
            get; set;
        }
    }
    public class EnumItem2
    {
        public string Name
        {
            get; set;
        }

        public int Id
        {
            get; set;
        }
    }
    public static class EnumsHelper
    {

        public static List<EnumItem2> GetEnumItemsDes<T>() where T : Enum
        {
            List<EnumItem2> enumItems = new List<EnumItem2>();
            var t = typeof(T);
            if (t.IsEnum)
            {
                var values = t.GetEnumValues();
                foreach (var v in values)
                {
                    FieldInfo field = t.GetField(t.GetEnumName(v));
                    DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                    enumItems.Add(new EnumItem2
                    {
                        Id = (int)v,
                        Name = attribute == null ? field.Name : attribute.Description
                    });
                }
            }
            return enumItems;
        }

        public static List<EnumItem2> GetEnumItems(string enumName)
        {
            List<EnumItem2> enumItems = new List<EnumItem2>();
            string fileName = System.IO.Path.Combine(AppContext.BaseDirectory, "XYY.Model.Standard.dll");
            var ass = Assembly.LoadFile(fileName);
            var t = ass.GetType("XYY.Model.Standard." + enumName);
            if (t != null)
            {
                if (t.IsEnum)
                {
                    var values = t.GetEnumValues();
                    foreach (var v in values)
                    {
                        enumItems.Add(new EnumItem2
                        {
                            Id = (int)v,
                            Name = t.GetEnumName(v)
                        });
                    }
                }
            }
            return enumItems;
        }
         

        public static List<EnumItem> GetEnumItems(Type type)
        {
            string[] Names = Enum.GetNames(type);
            var valueAraay = Enum.GetValues(type);
            List<EnumItem> list = new List<EnumItem>();
            for (int i = 0; i < Names.Length; i++)
            {
                list.Add(new EnumItem
                {
                    Name = Names[i],
                    Value = (int)valueAraay.GetValue(i)
                });
            }
            return list;
        }
        public static List<EnumItem2> GetEnumItemsDes(string enumName)
        {
            List<EnumItem2> enumItems = new List<EnumItem2>();
            string fileName = System.IO.Path.Combine(AppContext.BaseDirectory, "XYY.Model.Standard.dll");
            var ass = Assembly.LoadFile(fileName);
            var t = ass.GetType("XYY.Model.Standard." + enumName);
            if (t != null)
            {
                if (t.IsEnum)
                {
                    var values = t.GetEnumValues();
                    foreach (var v in values)
                    {
                        FieldInfo field = t.GetField(t.GetEnumName(v));
                        DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                        enumItems.Add(new EnumItem2
                        {
                            Id = (int)v,
                            Name = attribute == null ? "未知" : attribute.Description
                        });
                    }
                }
            }
            return enumItems;
        }
    }
}
