using Microsoft.OpenApi.Models;

using Swashbuckle.AspNetCore.SwaggerGen;

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

namespace HuanMeng.DotNetCore.SwaggerUtile
{
    /// <summary>
    /// 自定义参数过滤器
    /// </summary>
    public class LowercaseParameterFilter : IParameterFilter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="context"></param>
        public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
        {
            // 将参数名称改为小写开头
            parameter.Name = Char.ToLower(parameter.Name[0]) + parameter.Name.Substring(1);
        }
    }

    /// <summary>
    /// 自定义参数过滤器
    /// </summary>
    public class LowercaseRequestFilter : IRequestBodyFilter
    {
        public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
        {

            if (requestBody.Content != null)
            {
                foreach (var mediaType in requestBody.Content.Values)
                {
                    if (mediaType.Schema?.Properties != null)
                    {
                        var propertiesToRename = new Dictionary<string, OpenApiSchema>(mediaType.Schema.Properties);
                        // 清空旧的属性
                        mediaType.Schema.Properties.Clear();

                        foreach (var property in propertiesToRename)
                        {
                            // 创建新的属性，并将名称改为小写开头
                            var newPropertyName = Char.ToLower(property.Key[0]) + property.Key.Substring(1);
                            mediaType.Schema.Properties.Add(newPropertyName, property.Value);
                        }
                    }
                }
            }
        }
    }
}
