using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrazyStudio.Core.Common.Tools.Extensions
{
    public static class SwaggerExtensions
    {
        /// <summary>
        /// 默认添加SwaggerGen配置（包含JWT）
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void AddSwaggerGenDefault(this IServiceCollection services)
        {
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title = "速纺Api文档",
                    Description = "速纺Api文档 V1.0,包含常用CRUD，生产图生成等操作",
                    Contact = new OpenApiContact
                    {
                        Name = "xy451",
                        Url = new Uri("https://www.instafabric.com")
                    },
                    License = new OpenApiLicense
                    {
                        Name = "",
                        Url = new Uri("https://www.instafabric.com")
                    },
                    Version = "1.0"
                });
                options.OrderActionsBy(o => o.RelativePath);//对Action名称进行排序
                options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "SF.WebApi.xml"), true);
                options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
                {
                    Description = "在下框中输入请求头中需要添加Jwt授权Token：",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    BearerFormat = "JWT",
                    Scheme = "Bearer"
                });

                options.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            }
                        },
                        new string[] { }
                    }
                });
            });
        }
    }
}
