using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OASystem.API.Middlewares;
using OASystem.API.OAMethodLib;
using OASystem.API.OAMethodLib.AMapApi;
using OASystem.API.OAMethodLib.APNs;
using OASystem.API.OAMethodLib.Hub.Hubs;
using OASystem.API.OAMethodLib.JuHeAPI;
using OASystem.API.OAMethodLib.QiYeWeChatAPI;
using OASystem.API.OAMethodLib.Quartz.Jobs;
using OASystem.API.OAMethodLib.SignalR.HubService;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using QuzrtzJob.Factory;

Console.Title = $"FMGJ OASystem Server";
var builder = WebApplication.CreateBuilder(args);
var basePath = AppContext.BaseDirectory;

//���������ļ�
var _config = new ConfigurationBuilder()
                 .SetBasePath(basePath)
                 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                 .AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
                 .AddEnvironmentVariables()
                 .Build();
builder.Services.AddSingleton(new AppSettingsHelper(_config));

//������������ɲ���
builder.Services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddControllers()
    .AddJsonOptions(options =>
    {
        //���ֶβ���ӦResponse
        //options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;

        options.JsonSerializerOptions.Converters.Add(new NullJsonConverter());

        //ʱ���ʽ����Ӧ
        options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter("yyyy-MM-dd HH:mm:ss"));

        //decimal ��λС��
        //options.JsonSerializerOptions.Converters.Add(new DecimalConverter(_decimalPlaces)); // ������С��λ���������ݸ��Զ������л���
    });

builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

#region Cors
builder.Services.AddCors(policy =>
{
    //policy.AddPolicy("Cors", opt => opt
    //.AllowAnyOrigin()
    //.AllowAnyHeader()
    //.AllowAnyMethod()
    //.WithExposedHeaders("X-Pagination"));

    policy.AddPolicy("Cors", opt => opt
            .SetIsOriginAllowed(origin =>
            {
                // ������������Դ�б�
                var allowedOrigins = new List<string>
                    {
                       "http://132.232.92.186:9002"

                    };

                // ����������Դ�Ƿ����������б���
                return allowedOrigins.Contains(origin);
            })

          //.AllowAnyOrigin()
          .AllowAnyHeader()
          .WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")
          .AllowCredentials());

});
#endregion

#region �ϴ��ļ� 
builder.Services.AddCors(policy =>
{
    policy.AddPolicy("Cors", opt => opt
    .AllowAnyOrigin()
    .AllowAnyHeader()
    .AllowAnyMethod()
    .WithExposedHeaders("X-Pagination"));
});

builder.Services.Configure<FormOptions>(options =>
{
    options.KeyLengthLimit = int.MaxValue;
    options.ValueLengthLimit = int.MaxValue;
    options.MultipartBodyLengthLimit = int.MaxValue;
    options.MultipartHeadersLengthLimit = int.MaxValue;
});

builder.Services.Configure<KestrelServerOptions>(options =>
{
    options.Limits.MaxRequestBodySize = int.MaxValue;
    options.Limits.MaxRequestBufferSize = int.MaxValue;
});

#endregion

#region �ӿڷ���
var groups = new List<Tuple<string, string>>
{
    //new Tuple<string, string>("Group1","����һ"),
    //new Tuple<string, string>("Group2","�����")
};
#endregion

#region ע�����ݿ�
builder.Services.AddScoped(options =>
{
    return new SqlSugarClient(new List<ConnectionConfig>()
    {
        new ConnectionConfig() {
            ConfigId = DBEnum.OA2023DB,
            ConnectionString = _config.GetConnectionString("OA2023DB"),
            DbType = DbType.SqlServer,
            IsAutoCloseConnection = true
        },
        new ConnectionConfig()
        {
            ConfigId = DBEnum.OA2014DB,
            ConnectionString = _config.GetConnectionString("OA2014DB"),
            DbType = DbType.SqlServer,
            IsAutoCloseConnection = true },
    }
    //    , db =>
    //{
    //    //SQLִ����
    //    db.Aop.OnLogExecuted = (sql, pars) =>
    //    {
    //        //if (db.Ado.SqlExecutionTime.TotalSeconds > 1)
    //        //{
    //            //����CS���
    //            var fileName = db.Ado.SqlStackTrace.FirstFileName;
    //            //��������
    //            var fileLine = db.Ado.SqlStackTrace.FirstLine;
    //            //������
    //            var FirstMethodName = db.Ado.SqlStackTrace.FirstMethodName;

    //            //ִ�����˿������SQLִ��ʱ�� (OnLogExecutedDelegate) 
    //            Console.WriteLine("NowTime:" + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"));
    //            Console.WriteLine("MethodName:" + FirstMethodName);
    //            Console.WriteLine("ElapsedTime:" + db.Ado.SqlExecutionTime.ToString());
    //            Console.WriteLine("ExecuteSQL:" + sql);

    //        //}
    //    };
    //    //SQLִ��ǰ
    //    db.Aop.OnLogExecuting = (sql, pars) => 
    //    {
    //        //��ȡԭ��SQL�Ƽ� 5.1.4.63  ����OK
    //        //UtilMethods.GetNativeSql(sql, pars);

    //        //��ȡ�޲�����SQL Ӱ������ֻ�ʺϵ���
    //        //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
    //    };
    //    //SQL����
    //    db.Aop.OnError = (exp) =>
    //    {
    //        //��ȡԭ��SQL�Ƽ� 5.1.4.63  ����OK
    //        //UtilMethods.GetNativeSql(exp.sql, exp.parameters);

    //        //��ȡ�޲���SQL��������Ӱ�죬�ر���SQL������ģ�����ʹ��
    //        //UtilMethods.GetSqlString(DbType.SqlServer, exp.sql, exp.parameters);
    //    };
    //    //�޸�SQL�Ͳ�����ֵ
    //    db.Aop.OnExecutingChangeSql = (sql, pars) => 
    //    {
    //        //sql=newsql
    //        //foreach(var p in pars) //�޸�
    //        return new KeyValuePair<string, SugarParameter[]>(sql, pars);
    //    };

    //}
    );
});
#endregion

//#region Identity ����
//builder.Services.AddDataProtection();
////��Ҫ�� AddIdentity �� AddIdentity ����MVC����е�
//builder.Services.AddIdentityCore<User>(opt =>
//{
//    opt.Password.RequireDigit = false; //����
//    opt.Password.RequireLowercase = false;//Сд��ĸ
//    opt.Password.RequireNonAlphanumeric = false;//������� ���� ��#@�� 
//    opt.Password.RequireUppercase = false; //��д��ĸ
//    opt.Password.RequiredLength = 6;//���볤�� 6 
//    opt.Password.RequiredUniqueChars = 1;//��ͬ�ַ����Գ��ּ���
//    opt.Lockout.MaxFailedAccessAttempts = 5; //���������������û���/�������
//    opt.Lockout.DefaultLockoutTimeSpan = new TimeSpan(0, 5, 0);//���������
//    opt.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider; // �޸�����ʹ���ʼ�����֤��ģʽ��
//    opt.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider;  //// 
//});
//var idBuilder = new IdentityBuilder(typeof(User), typeof(UserRole), services);
//idBuilder.AddEntityFrameworkStores<swapDbContext>().AddDefaultTokenProviders().AddRoleManager<RoleManager<UserRole>>().AddUserManager<UserManager<User>>();
//#endregion

#region ע��Swaggerע��(����)

if (AppSettingsHelper.Get("UseSwagger").ToBool())
{
    builder.Services.AddSwaggerGen(a =>
    {
        a.SwaggerDoc("v1", new OpenApiInfo
        {
            Version = "v1",
            Title = "Api",
            Description = "Api�ӿ��ĵ�"
        });
        foreach (var item in groups)
        {
            a.SwaggerDoc(item.Item1, new OpenApiInfo { Version = item.Item1, Title = item.Item2, Description = $"{item.Item2}�ӿ��ĵ�" });
        }
        a.DocumentFilter<SwaggerApi>();
        a.IncludeXmlComments(Path.Combine(basePath, "OASystem.Api.xml"), true);
        a.IncludeXmlComments(Path.Combine(basePath, "OASystem.Domain.xml"), true);
        a.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
        {
            Description = "Value: Bearer {token}",
            Name = "Authorization",
            In = ParameterLocation.Header,
            Type = SecuritySchemeType.ApiKey,
            Scheme = "Bearer"
        });
        a.AddSecurityRequirement(new OpenApiSecurityRequirement()
        {{
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }, Scheme = "oauth2", Name = "Bearer", In = ParameterLocation.Header }, new List<string>()
            }
        });

    });
}
#endregion

#region ������

builder.Services.AddTransient<OASystemAuthentication>();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
        {
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = true,
                ValidAudience = "OASystem.com",
                ValidIssuer = "OASystem.com",
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"])),
                ClockSkew = TimeSpan.FromSeconds(30), //����ʱ���ݴ�ֵ�������������ʱ�䲻ͬ�����⣨�룩
                RequireExpirationTime = true,
            };
            options.Events = new JwtBearerEvents
            {
                OnMessageReceived = context =>
                {
                    var path = context.HttpContext.Request.Path;
                    //�����signalr������Ҫ��tokenת�棬����JWT��ȡ����token��OPTIONS������Ҫ���˵�����ΪOPTIONS�����ȡ����Token����NGINX���˵�OPTION����.
                    if (path.StartsWithSegments("/ChatHub"))
                    {
                        string accessToken = context.Request.Query["access_token"].ToString();
                        if (string.IsNullOrWhiteSpace(accessToken))
                        {
                            accessToken = context.Request.Headers["Authorization"].ToString();
                        }
                        context.Token = accessToken.Replace("Bearer ", "").Trim();
                    }
                    return Task.CompletedTask;
                }
            };
        });

#endregion

#region ��ʼ����־
Log.Logger = new LoggerConfiguration()
       .MinimumLevel.Debug()
       .WriteTo.File(Path.Combine("Logs", @"Log.txt"), rollingInterval: RollingInterval.Day)
       .CreateLogger();
#endregion

#region ����ע��Autofac Module
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
var hostBuilder = builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
{
    try
    {
        builder.RegisterModule(new AutofacRegister());
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message + "\n" + ex.InnerException);
    }
});
#endregion

#region AutoMapper

AutoMapper.IConfigurationProvider config = new MapperConfiguration(cfg =>
{
    cfg.AddProfile<_baseMappingProfile>();
});
builder.Services.AddSingleton(config);
builder.Services.AddScoped<IMapper, Mapper>();

#endregion

#region �ۺ�API ����
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IJuHeApiService, JuHeApiService>();
builder.Services.AddHttpClient("PublicJuHeApi", c => c.BaseAddress = new Uri("http://web.juhe.cn"));
builder.Services.AddHttpClient("PublicJuHeTranslateApi", c => c.BaseAddress = new Uri("http://apis.juhe.cn"));
#endregion

#region ��ҵ΢��API ����

builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IQiYeWeChatApiService, QiYeWeChatApiService>();
builder.Services.AddHttpClient("PublicQiYeWeChatApi", c => c.BaseAddress = new Uri("https://qyapi.weixin.qq.com"));

#endregion

#region �е�API ����
//builder.Services.AddControllersWithViews();
//builder.Services.AddSingleton<IYouDaoApiService, YouDaoApiService>();
//builder.Services.AddHttpClient("PublicYouDaoApi", c => c.BaseAddress = new Uri("https://openapi.youdao.com"));
#endregion

#region �ߵµ�ͼAPI ����
builder.Services.AddHttpClient<GeocodeService>();
#endregion

#region Quartz

builder.Services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
builder.Services.AddSingleton<QuartzFactory>();
builder.Services.AddSingleton<ALiYunPostMessageJob>();
builder.Services.AddSingleton<TaskJob>();
builder.Services.AddSingleton<TaskNewsFeedJob>();
//# new business
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IAPNsService, APNsService>();

builder.Services.AddSingleton<IJobFactory, IOCJobFactory>();

#endregion

#region SignalR
builder.Services.AddSignalR()
                .AddJsonProtocol(options =>
                {
                    options.PayloadSerializerOptions.PropertyNamingPolicy = null;
                });
builder.Services.TryAddSingleton(typeof(CommonService));
#endregion

#region ����������

//// 
//builder.Services.AddHealthChecks()
//    .AddCheck("ExampleHealthCheck", () =>
//        HealthCheckResult.Healthy("Everything is OK!"), tags: new[] { "example" });

//// ���� HealthChecks UI ����
//builder.Services.AddHealthChecksUI(setup =>
//{
//    setup.SetEvaluationTimeInSeconds(60); // ÿ 60 ����һ��
//    setup.MaximumHistoryEntriesPerEndpoint(50); // ÿ���˵���ౣ�� 50 ����ʷ��¼
//    setup.SetApiMaxActiveRequests(1); // UI API ����󲢷�������
//})
//.AddInMemoryStorage(); // ʹ���ڴ�洢���������

#endregion

var app = builder.Build();

AutofacIocManager.Instance.Container = app.UseHostFiltering().ApplicationServices.GetAutofacRoot();//AutofacIocManager

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();

app.UseRouting();

app.UseCors("Cors");  //Cors

//app.UseMiddleware<FixedPromptMiddleware>();
app.UseMiddleware<ExceptionHandlingMiddleware>();

// ��������API�ķ���ʱ���
//var startTime = DateTime.Parse(_config["ApiAccessTime:StartTime"]);
//var endTime = DateTime.Parse(_config["ApiAccessTime:EndTime"]);
//app.UseMiddleware<TimeRestrictionMiddleware>(startTime, endTime);

//ָ��API������¼��Ϣ
app.UseMiddleware<RecordAPIOperationMiddleware>();

app.UseAuthentication(); // ��֤
app.UseAuthorization();  // ��Ȩ

// ��Ȩ·��
//app.MapGet("generatetoken", c => c.Response.WriteAsync(JWTBearer.GenerateToken(c)));

#region ����swaggerUI
if (AppSettingsHelper.Get("UseSwagger").ToBool())
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ver0.1");
        foreach (var item in groups)
        {
            c.SwaggerEndpoint($"/swagger/{item.Item1}/swagger.json", item.Item2);
        }
        c.RoutePrefix = string.Empty;
        c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
        c.DefaultModelsExpandDepth(-1);

        //c.EnableFilter();// ������������
        //c.EnableDeepLinking(); // �����������
    });
}
#endregion

#region Quartz

//��ȡ�����е�QuartzFactory
var quartz = app.Services.GetRequiredService<QuartzFactory>();
app.Lifetime.ApplicationStarted.Register(async () =>
{
    await quartz.Start();
});

app.Lifetime.ApplicationStopped.Register(() =>
{
    //Quzrtz�رշ���
    //quartz.Stop();
});

#endregion

#region SignalR

app.MapHub<ChatHub>("/ChatHub", options =>
{
    options.Transports =
        HttpTransportType.WebSockets |
        HttpTransportType.LongPolling;
});

#endregion

#region ���ý������˵�

// ���ý������˵�
//app.MapHealthChecks("/health", new HealthCheckOptions
//{
//    Predicate = _ => true, // ������������
//    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse // ʹ�� UI ����Ӧ��ʽ
//});



#endregion

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

app.Run();