using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace VCommon.Reflection
{
    public class EnumMeta<T>
    {
        public static IReadOnlyDictionary<T, EnumMetaAttribute> Meta { get; }

        static EnumMeta()
        {
            if (false == typeof(T).IsEnum) throw new VCommonException("Generic type argument must be Enum.");

            //TODO: EnumMeta 检查枚举值重复

            Meta = typeof(T).GetFields().Where(p => p.FieldType == typeof(T))
                .ToDictionary(
                    k => (T)k.GetRawConstantValue(),
                    p => p.GetCustomAttributes(typeof(EnumMetaAttribute))
                        .OfType<EnumMetaAttribute>()
                        .Single()
                );
        }
    }
}