using CloudGaming.Code.AppExtend;
using CloudGaming.Code.DataAccess.MultiTenantUtil;
using CloudGaming.Code.Extend;
using CloudGaming.Code.Filter;

using HuanMeng.DotNetCore.MiddlewareExtend;
using HuanMeng.DotNetCore.SwaggerUtile;

using Microsoft.AspNetCore.Authentication.JwtBearer;

using Microsoft.OpenApi.Models;

using Newtonsoft.Json.Serialization;

using Serilog;

using System.Diagnostics;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

#region 日志
// Add services to the container.
builder.Host.UseSerilog((context, services, configuration) => configuration
            .ReadFrom.Configuration(context.Configuration)
            .ReadFrom.Services(services)
            .Enrich.FromLogContext());

builder.Services.AddSingleton(typeof(ILogger<CloudGamingBase>), serviceProvider =>
{
    var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
    return loggerFactory.CreateLogger<CloudGamingBase>();
});
//
builder.Services.AddSingleton(typeof(ILogger<ExceptionMiddleware>), serviceProvider =>
{
    var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
    return loggerFactory.CreateLogger<ExceptionMiddleware>();
});
#endregion

builder.Services.AddHttpContextAccessor(); //添加httpContext注入访问
#region 返回数据解析  
//builder.Services.AddControllers();
builder.Services.AddControllers(options =>
{
})
    .AddNewtonsoftJson(options =>
    {
        // 配置 Newtonsoft.Json 选项
        options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; // 忽略循环引用
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();// 首字母小写（驼峰样式）
        //options.SerializerSettings.ContractResolver = new LanguageContractResolver(builder.Services);
        options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";// 时间格式化
        options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.None;
    });
//CustomResultFilter
//builder.Services.AddSingleton<ObjectResultExecutor, CustomObjectResultExecutor>();

#endregion

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{


    string description = "";
    var filePath = Path.GetFullPath(".versionDescribe");
    if (File.Exists(filePath))
    {
        description = File.ReadAllText(filePath);
    }
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "蒸汽云游戏支付", Version = "0.0.1", Description = description });
    foreach (var assemblies in AppDomain.CurrentDomain.GetAssemblies())
    {
        // 添加 XML 注释文件路径
        var xmlFile = $"{assemblies.GetName().Name}.xml";
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        if (File.Exists(xmlPath))
        {
            c.IncludeXmlComments(xmlPath);
        }
    }
    c.ParameterFilter<LowercaseParameterFilter>();
    c.RequestBodyFilter<LowercaseRequestFilter>();
});
await builder.AddAppConfigClient();

var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {

        c.EnableDeepLinking();
        c.DefaultModelsExpandDepth(3);
        c.DefaultModelExpandDepth(3);
        c.EnableFilter("true");
        //c.RoutePrefix = string.Empty;
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "蒸汽云游戏支付 API V1");
        // 使用自定义CSS
        c.InjectStylesheet("/custom.css");
    });
//}

app.UseAuthorization();
//数据库中间件
app.UseMultiTenant();
app.MapControllers();
app.UseMiddlewareAll();
#region 默认请求
app.UseAppRequest("pay");
#endregion
app.Run();
