using HuanMeng.DotNetCore.AttributeExtend;

using Microsoft.AspNetCore.Mvc.Controllers;

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CloudGaming.Code.Extend;


/// <summary>
/// 
/// </summary>
public static class MessageAttributeExtend
{
    private static readonly ConcurrentDictionary<MethodInfo, MessageAttribute?> _attributeCache = new ConcurrentDictionary<MethodInfo, MessageAttribute?>();

    /// <summary>
    /// 
    /// </summary>
    /// <param name="controllerActionDescriptor"></param>
    /// <returns></returns>
    public static MessageAttribute? GetMessageAttribute(ControllerActionDescriptor controllerActionDescriptor)
    {
        // 获取方法信息
        var methodInfo = controllerActionDescriptor.MethodInfo;

        // 尝试从缓存中获取MessageAttribute
        if (!_attributeCache.TryGetValue(methodInfo, out var messageAttribute))
        {
            // 如果缓存中没有，则使用反射获取并存储到缓存中
            messageAttribute = methodInfo.GetCustomAttribute<MessageAttribute>();
            _attributeCache.TryAdd(methodInfo, messageAttribute);
        }

        return messageAttribute;
    }
}
