using Microsoft.Extensions.Hosting;
using SqlSugar;

namespace EEMSServerCore;

internal class HostLifeTime(IEEMSBaseServerProvider provider, SqlSugarCustom orm, DeviceManager deviceManager) : IHostedService
{
    public Task StartAsync(CancellationToken cancellationToken)
    {

        orm.Initialize();
        if (!orm.Open(EEMSCenterConfig.DBString, DbType.PostgreSQL, true))
        {
            MessageBox.Show("Connect to DataBase Failed");
            Application.Current.Shutdown();
        }

        orm.CreateTable<DeviceInfo>(EEMSCenterConfig.DeviceTableName);

        if (!deviceManager.Initialize())
        {
            MessageBox.Show("DeviceManager Initialize Failed");
            Application.Current.Shutdown();
        }




        provider.Started();
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {

        return Task.CompletedTask;
    }
}
