using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AyCode.Core.Extensions
{
    public static class EnumExtensions
    {
        private static readonly ConcurrentDictionary<Type, Dictionary<string, DisplayAttribute>> DisplayNameCache = new();

        private static T? GetAttributeOfType<T>(this Enum enumValue) where T : Attribute
        {
            var type = enumValue.GetType();
            var memInfo = type.GetField(enumValue.ToString(), BindingFlags.Public | BindingFlags.Static);

            if (memInfo == null) return null;

            var attributes = memInfo.GetCustomAttributes<T>(false);
            return attributes.FirstOrDefault();
        }

        //public static string GetDisplayValuesName(this Enum enumValue)
        //=> DisplayNameCache[enumValue]

        public static string GetDisplayName2<T>(this T enumValue) where T : class
        {
            return string.Empty; 
            //return GetDisplayAttribute(enumValue).Name!;
        }

        public static string GetDisplayName(this Enum enumValue)
        {
            return GetDisplayAttribute(enumValue).Name!;
        }

        public static DisplayAttribute GetDisplayAttribute(this Enum enumValue)
        {
            return new DisplayAttribute();
            //return DisplayNameCache.GetOrAdd(enumValue, e =>
            //{
            //    var displayAttribute = e.GetAttributeOfType<DisplayAttribute>();
            //    if (displayAttribute != null) return displayAttribute;

            //    var name = e.ToString();
            //    return new DisplayAttribute { Name = name, ShortName = name, Prompt = name, Description = name };
            //});
        }
    }
}

