using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using JiShe.CollectBus.Data;
using Serilog;
using Volo.Abp;
using Volo.Abp.Data;
using JiShe.ServicePro.ApacheIoTDB.Provider.SessionPools;

namespace JiShe.CollectBus.DbMigrator;

public class DbMigratorHostedService : IHostedService
{
    private readonly IHostApplicationLifetime _hostApplicationLifetime;
    private readonly IConfiguration _configuration;

    public DbMigratorHostedService(IHostApplicationLifetime hostApplicationLifetime, IConfiguration configuration)
    {
        _hostApplicationLifetime = hostApplicationLifetime;
        _configuration = configuration;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        using (var application = await AbpApplicationFactory.CreateAsync<CollectBusDbMigratorModule>(options =>
        {
           options.Services.ReplaceConfiguration(_configuration);
           options.UseAutofac();
           options.Services.AddLogging(c => c.AddSerilog());
           options.AddDataMigrationEnvironment();
        }))
        {
            await application.InitializeAsync();

            //await application
            //    .ServiceProvider
            //    .GetRequiredService<CollectBusDbMigrationService>()
            //    .MigrateAsync();

            //初始化IoTDB表模型
            await application
                .ServiceProvider
                .GetRequiredService<IoTDBSessionPoolProvider>().GetSessionPool(true)
                .InitTableSessionModelAsync();

            await application.ShutdownAsync();

            _hostApplicationLifetime.StopApplication();
        }
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}
