using FccLite.Web.Repositories.FccNozzleInfo;
using FccLite.Web.Repositories.FccOilInfo;
using FccLite.Web.Repositories.FccStationInfo;
using FccLite.Web.Repositories.FccTankInfo;
using FccLite.Web.Services.FccNozzleInfo;
using FccLite.Web.Services.FccOilInfo;
using FccLite.Web.Services.FccStaionInfo;
using FccLite.Web.Services.FccTankInfo;
using Microsoft.EntityFrameworkCore;
using Edge.Core.Processor.Dispatcher;
using Edge.Core.Processor;
using Edge.Core.Processor.Dispatcher.ProcessorLoader;
using Edge.Core.Core.database;
using Edge.Core.HttpClient;
using HengshanPaymentTerminal.Http;
using FccLite.Web.Services.FccOrderInfo;
using FccLite.Web.Repositories.FccOrderInfo;

var builder = WebApplication.CreateBuilder(args);
 DefaultDispatcher processorsDispatcher = null;
//数据库
builder.Services.AddDbContext<MysqlDbContext>(options =>
    options.UseMySql(builder.Configuration.GetConnectionString("DefaultConnection"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("DefaultConnection")))
    .LogTo(Console.WriteLine,LogLevel.Debug));

// http Client
builder.Services.AddHttpClient();
builder.Services.AddSingleton<HttpClientService>();
//builder.Services.AddSingleton<HttpClientUtils>();
//controller
builder.Services.AddControllers();

//服务注册
builder.Services.AddScoped<IStationService, StationServiceImpl>();
builder.Services.AddScoped<IStationRepository, StationRepository>();
builder.Services.AddScoped<IOilInfoService, OilInfoServiceImpl>();
builder.Services.AddScoped<IOilInfoReposity, OilInfoReposity>();
builder.Services.AddScoped<ITankInfoService, TankInfoServiceImpl>();
builder.Services.AddScoped<ITankReposity, TankReposity>();
builder.Services.AddScoped<INozzleInfnService, NozzleInfoServiceImpl>();
builder.Services.AddScoped<INozzleInfoReposity, NozzleInfoReposity>();
builder.Services.AddScoped<IOrderInfoService, OrderInfoService>();
builder.Services.AddScoped<IOrderInfoReposity, OrderInfoReposity>();
builder.Services.AddScoped<IHttpClient, HttpClientService>();
builder.Services.AddScoped<IHttpClientUtil, HttpClientUtils>();

//swagger
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
    {
        Title = ".NET 6.0 API",
        Version = "v1",
        Description = "二维码项目 FCC 页面接口",
    });
});
{
    builder.Services.AddSingleton(ProcessorLoaderFactory.Create);
    builder.Services.AddSingleton(ProcessorMetaConfigAccessorFactory.Create);
    var serviceProvider = builder.Services.BuildServiceProvider();
 

    Console.CancelKeyPress += (sender, arg) =>
    {

        Environment.Exit(-1);
    };
    processorsDispatcher = new DefaultDispatcher(serviceProvider);
    IEnumerable<ProcessorInstanceOperatingResult> instantiateResults = null;
    instantiateResults = await processorsDispatcher.CreateProcessorsAsync("Main starting", false);
    var startResults = await processorsDispatcher.StartProcessorsAsync(
                 instantiateResults.Where(r => r.Succeed).Select(r => r.ProcessorInstance),
                 "Main starting");
}
var app = builder.Build();

//controller
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

//swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", ".NET 6 API V1");
});

//迁移数据库
//string? isMigrate = builder.Configuration.GetConnectionString("isMigrate");
//if (isMigrate != null && "true".Equals(isMigrate))
//{
//    //using var context = new MysqlDbContext();
//    //context.Database.Migrate();
//    using (var scope = app.Services.CreateScope())
//    {
//        var context = scope.ServiceProvider.GetRequiredService<MysqlDbContext>();
//        context.Database.Migrate();
//    }
//}

app.Run();
