using ASHermed.SingleSignOnFramework;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.WebApi.Filters
{
    /// <summary>
    /// 在Swagger的请求头中增加参数
    /// 创建人：杨江军
    /// 创建时间：2020/4/13 17:00:13
    /// </summary>
    public class AddSwaggerHeaderParameterFilter : IOperationFilter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="context"></param>
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

            var controller = context.ApiDescription.ActionDescriptor.RouteValues["controller"].ToLower();
            var action = context.ApiDescription.ActionDescriptor.RouteValues["action"].ToLower();
            if (!SingleSignOnConfig.GetInstance().ExcludeControllerNames.Contains(action))
            {
                operation.Parameters.Add(new OpenApiParameter()
                {
                    Name = "Authorization",//Token
                    In = ParameterLocation.Header,
                    Description = "用户Token",
                    Required = true
                });
                operation.Parameters.Add(new OpenApiParameter()
                {
                    Name = "version",//ClientId
                    In = ParameterLocation.Header,
                    Description = "客户端ID（每个设备的唯一ID）",
                    Required = true
                });
                operation.Parameters.Add(new OpenApiParameter()
                {
                    Name = "device",//ClientType
                    In = ParameterLocation.Header,
                    Description = "客户端类型(1:Html5; 2:IOS; 3:Android;)",
                    Required = true
                });
                
            }
        }
    }
}
