using System;

namespace JiShe.CollectBus.Common.Extensions
{
    public static class EnumExtensions
    {
        /// <summary>
        /// 将枚举转换为<string, int>字典
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static Dictionary<string, int> ToDictionary<TEnum>() where TEnum : Enum
        {
            return Enum.GetValues(typeof(TEnum))
                       .Cast<TEnum>()
                       .ToDictionary(
                           e => e.ToString(),
                           e => Convert.ToInt32(e)
                       );
        }

        /// <summary>
        /// 将枚举转换为<string, int>字典
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static Dictionary<string, TEnum> ToEnumDictionary<TEnum>() where TEnum : Enum
        {
            return Enum.GetValues(typeof(TEnum))
                       .Cast<TEnum>()
                       .ToDictionary(
                           e => e.ToString(),
                           e => e
                       );
        }

        /// <summary>
        /// 将枚举转换为<int, string>字典
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static Dictionary<int, string> ToValueNameDictionary<TEnum>() where TEnum : Enum
        {
            return Enum.GetValues(typeof(TEnum))
                       .Cast<TEnum>()
                       .ToDictionary(
                           e => Convert.ToInt32(e),
                           e => e.ToString()
                       );
        }

        /// <summary>
        /// 将枚举转换为<int, string>字典
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static Dictionary<string, int> ToNameValueDictionary<TEnum>() where TEnum : Enum
        {
            return Enum.GetValues(typeof(TEnum))
                       .Cast<TEnum>()
                       .ToDictionary(
                           e => e.ToString(),
                           e => Convert.ToInt32(e)
                       );
        }

        /// <summary>
        /// 将枚举转换为<TEnum, string>字典
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <returns></returns>
        public static Dictionary<TEnum, string> ToEnumNameDictionary<TEnum>() where TEnum : Enum
        {
            return Enum.GetValues(typeof(TEnum))
                       .Cast<TEnum>()
                       .ToDictionary(
                           e => e,
                           e => e.ToString()
                       );
        }

    }
}
