using System.Text.Json;
using Base;
using Common;
using Extensions;
using Filters;
using Infrastructure;
using Infrastructure.Model;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.FileProviders;
using Middleware;
using MySystem;
using Services;
using SummerBoot.Core;
using Util;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//注入HttpContextAccessor
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
//IPRatelimit
// builder.Services.AddIPRate(builder.Configuration);
builder.Services.AddSession();
builder.Services.AddHttpContextAccessor();
builder.Services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));
builder.Services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true).Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);

//绑定整个对象到Model上
builder.Services.Configure<OptionsSetting>(builder.Configuration);
// builder.Configuration.AddJsonFile("iprate.json");
//配置文件
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
//请求大小限制
builder.Services.AddRequestLimit(builder.Configuration);
//注册REDIS 服务
var openRedis = builder.Configuration["RedisServer:open"];
if (openRedis == "1")
{
    RedisServer.Initalize();
}
//配置文件
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
//app服务注册
builder.Services.AddAppService();

builder.Services.AddMvc(options =>
{
    options.Filters.Add(typeof(GlobalActionMonitor));//全局注册
    options.Filters.Add(typeof(AuthorizationFilter));
})
.AddJsonOptions(options =>
{
    //options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString;
    options.JsonSerializerOptions.WriteIndented = true;
    options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
    options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
    options.JsonSerializerOptions.Converters.Add(new StringConverter());
    //PropertyNamingPolicy属性用于前端传过来的属性的格式策略,目前内置的仅有一种策略CamelCase
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    //options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;//属性可以忽略大小写格式,开启后性能会降低
});
builder.Services.AddSummerBoot();
builder.Services.AddSummerBootFeign();

var app = builder.Build();
InternalApp.ServiceProvider = app.Services;
InternalApp.Configuration = builder.Configuration;
InternalApp.WebHostEnvironment = app.Environment;
//初始化db
builder.Services.AddDb(app.Environment);
var workId = builder.Configuration["workId"].ParseToInt();
if (app.Environment.IsDevelopment())
{
    workId += 1;
}
SnowFlakeSingle.WorkId = workId;

//使用全局异常中间件
app.UseMiddleware<GlobalExceptionMiddleware>();

app.Use(next => new RequestDelegate(
    async context =>
    {
        context.Request.EnableBuffering();
        await next(context);
    }
));

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
}


app.UseStaticFiles();

app.UseCors();

app.UseHttpsRedirection();

app.UseAuthorization();

app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{Id?}");
});

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.MapControllers();

app.Urls.Add("http://*:5802");

// PrizeDo.sendPrize("QUEUE_KXS_MACHINE_PRIZE_CONFIG_DIVISION", "{\"brand_id\":12,\"pos_sn\":\"00005702880118407887\",\"user_id\":192332}");
// PrizeDo.sendPrize("QUEUE_KXS_WIFI_PRIZE_CONFIG_DIVISION", "{\"brand_id\":24,\"pos_sn\":\"126800303190\",\"user_id\":192332}");
// PrizeDo.sendPrize(4, "{\"PosSn\":\"CS00000000015859\"}");
// PrizeDo.sendPrize("QUEUE_KXS_HAODA_PRIZE_CONFIG_DIVISION", "{\"brand_id\":19,\"pos_sn\":\"00000102249QCQF02551\",\"user_id\":192332}");
// PrizeDo.sendPrize(6, "{\"PosSn\":\"1152131239\"}");
// PrizeDo.sendPrize("QUEUE_KXS_PRIZE_MALL_CONFIG_DIVISION", "{\"order_id\":602763,\"user_id\":264331}");
// PrizeDo.sendPrize(7, "{\"OrderNo\":\"BM2024090817330511847441249\"}");
// PrizeDo.sendPrize(7, "{\"OrderNo\":\"BM2024090817360962275551251\"}");

// PrizeDo.sendPrize("KXS_LEADER_STA_PRIZE", "{\"month\":\"202411\"}");

RabbitMQClient.Instance.Start();

// CopyProject.Copy(5, 7);

// PrizeDo.sendPrize("{\"prize_tag\":\"1\",\"content\":\"{\\\"PosSn\\\":\\\"00002402045980195457\\\"}\"}");

app.Run();