using JiShe.CollectBus.Common;
using JiShe.CollectBus.Host.HealthChecks;
using JiShe.CollectBus.Host.Swaggers;
using JiShe.ServicePro.FreeRedisProvider;
using JiShe.ServicePro.ServerOptions;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting.Internal;
using Swashbuckle.AspNetCore.SwaggerUI;
using Volo.Abp;
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.Autofac;
using Volo.Abp.Caching.StackExchangeRedis;
using Volo.Abp.Modularity;
using Volo.Abp.Swashbuckle;
using Volo.Abp.Timing;

namespace JiShe.CollectBus.Host
{
    /// <summary>
    /// 
    /// </summary>
    [DependsOn(typeof(CollectBusHttpApiModule),
        typeof(AbpAutofacModule),
        typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
        typeof(AbpAspNetCoreSerilogModule),
        typeof(AbpSwashbuckleModule),
        typeof(AbpTimingModule),
        typeof(CollectBusApplicationModule),
        typeof(AbpCachingStackExchangeRedisModule)
        )]
    public partial class CollectBusHostModule : AbpModule
    {
        /// <summary>
        /// ConfigureServices
        /// </summary>
        /// <param name="context"></param>
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            ConfigureSwaggerServices(context, configuration);
            ConfigureNetwork(context, configuration);
            ConfigureJwtAuthentication(context, configuration);
            //ConfigureHangfire(context);
            //ConfigureAuditLog(context);
            ConfigureCustom(context, configuration);
            ConfigureHealthChecks(context, configuration);
            Configure<AbpClockOptions>(options => { options.Kind = DateTimeKind.Local; });

            Configure<ServerApplicationOptions>(options =>
            {
                configuration.GetSection(nameof(ServerApplicationOptions)).Bind(options);
            });

            context.Services
                   .AddDataProtection()
                   .PersistKeysToStackFreeRedis("JiSheCollectBus-Protection-Keys");

        }


        /// <summary>
        /// OnApplicationInitialization
        /// </summary>
        /// <param name="context"></param>
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var configuration = context.GetConfiguration();
            var env = context.GetEnvironment();
            app.UseCorrelationId();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseCors(CollectBusHostConst.DefaultCorsPolicyName);
            app.UseAuthentication();
            app.UseAuthorization();
            if (env.IsDevelopment())
            {
                app.UseSwagger();
                app.UseAbpSwaggerUI(options =>
                {
                    configuration.GetSection("SwaggerConfig").Get<List<SwaggerConfig>>()?.ForEach(group =>
                    {
                        options.SwaggerEndpoint($"/swagger/{group.GroupName}/swagger.json", group.Title); //分组显示
                    });
                    options.DocExpansion(DocExpansion.None);
                    options.DefaultModelsExpandDepth(-1);
                });
            }
            app.UseAuditing();
            app.UseAbpSerilogEnrichers();
            app.UseUnitOfWork();
            //app.UseHangfireDashboard("/hangfire", new DashboardOptions
            //{
            //    IgnoreAntiforgeryToken = true
            //});
            app.UseConfiguredEndpoints(endpoints =>
            {
                if (!configuration.GetValue<bool>("HealthChecks:IsEnable")) return;
                endpoints.MapHealthChecks("/health", new HealthCheckOptions
                {
                    Predicate = _ => true,
                    ResponseWriter = HealthCheckResponse.Writer
                });
                endpoints.MapHealthChecksUI(options =>
                {
                    options.UIPath = "/health-ui";
                });
            });
        }
    }
}
