using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace QYZH.InteractiveMagazine.Common.Helpers
{
    /// <summary>
    /// 枚举工具类
    /// </summary>
    public static class EnumHelper
    {
        /// <summary>
        /// 根据枚举名称获取枚举值
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <param name="name">枚举名称</param>
        /// <returns>枚举值</returns>
        public static TEnum GetValue<TEnum>(string name) where TEnum : Enum
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("枚举名称不能为空", nameof(name));
            }

            if (Enum.TryParse(typeof(TEnum), name, true, out object? result))
            {
                return (TEnum)result;
            }

            throw new ArgumentException($"枚举 '{typeof(TEnum).Name}' 中不存在名称为 '{name}' 的值");
        }

        /// <summary>
        /// 根据枚举值获取枚举名称
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <param name="value">枚举值</param>
        /// <returns>枚举名称</returns>
        public static string GetName<TEnum>(object value) where TEnum : Enum
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (Enum.IsDefined(typeof(TEnum), value))
            {
                return Enum.GetName(typeof(TEnum), value)!;
            }

            throw new ArgumentException($"枚举 '{typeof(TEnum).Name}' 中不存在值 '{value}'");
        }

        /// <summary>
        /// 获取枚举的所有值
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <returns>枚举值列表</returns>
        public static List<TEnum> GetAllValues<TEnum>() where TEnum : Enum
        {
            return Enum.GetValues(typeof(TEnum)).Cast<TEnum>().ToList();
        }

        /// <summary>
        /// 根据Description获取枚举值
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <param name="description">描述文本</param>
        /// <returns>枚举值</returns>
        public static TEnum GetValueByDescription<TEnum>(string description) where TEnum : Enum
        {
            if (string.IsNullOrWhiteSpace(description))
            {
                throw new ArgumentException("描述不能为空", nameof(description));
            }

            foreach (TEnum value in Enum.GetValues(typeof(TEnum)))
            {
                string desc = GetDescription(value);
                if (desc.Equals(description, StringComparison.OrdinalIgnoreCase))
                {
                    return value;
                }
            }

            throw new ArgumentException($"枚举 '{typeof(TEnum).Name}' 中不存在描述为 '{description}' 的值");
        }

        /// <summary>
        /// 获取枚举的DescriptionAttribute描述
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <param name="value">枚举值</param>
        /// <returns>描述文本</returns>
        public static string GetDescription<TEnum>(TEnum value) where TEnum : Enum
        {
            System.Reflection.FieldInfo? field = value.GetType().GetField(value.ToString());
            if (field == null)
            {
                return value.ToString();
            }

            var attribute = (DescriptionAttribute?)Attribute.GetCustomAttribute(
                field, typeof(DescriptionAttribute));

            return attribute?.Description ?? value.ToString();
        }
    }
}
