using Inspectron.Epson;
using Inspectron.Epson.PrintServer;
using Inspectron.Epson.PrintServer.ConfigurationSources;
using Inspectron.Epson.PrintServer.JobSources;
using Inspectron.Epson.PrintServer.JobStatusReporters;
using Inspectron.Epson.PrintServer.PrinterAssinment;
using Inspectron.Epson.PrintServer.Printers;
using Inspectron.Epson.PrintServer.Printers.Utils;
using Inspectron.Epson.PrintServer.PrintServices;
using Inspectron.Epson.Queue;
using Microsoft.Extensions.Logging;
using Ninject;

namespace EpsonPrintService;

public class PrintServerBootstrapper
{
    private readonly EpsonPrintServiceConfiguration _config;
    private readonly CancellationTokenSource _cts;
    private StandardKernel? _kernel;

    public bool IsRunning { get; private set; }

    public PrintServerBootstrapper(EpsonPrintServiceConfiguration config, CancellationTokenSource cts)
    {
        _config = config;
        _cts = cts;
    }

    public async Task StartAsync()
    {
        _kernel = new StandardKernel();

        _kernel.Bind<IDiscoveryService>().To<DiscoveryService>().InSingletonScope();
        _kernel.Bind<HttpClient>().ToConstant(new HttpClient { Timeout = TimeSpan.FromSeconds(30) }).InSingletonScope();
        _kernel.Bind<EpsonPrintServiceConfiguration>().ToConstant(_config);
        _kernel.Bind<IPrinterFactory>().To<EpsonPrinterFactory>();
        _kernel.Bind<IPrintJobSource, SignalRPrintJobSource>().To<SignalRPrintJobSource>().InSingletonScope();
        _kernel.Bind<IPrintService>().To<Inspectron.Epson.PrintServer.PrintServices.EpsonPrintService>();
        _kernel.Bind<ILogger>().ToMethod(ctx =>
        {
            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddConsole();
                builder.SetMinimumLevel(LogLevel.Trace);
            });
            return loggerFactory.CreateLogger("EpsonPrintService");
        });
        _kernel.Bind<IWrapperPrinterFactory>().To<PrinterWrapperFactory>().InSingletonScope();
        _kernel.Bind<IPrinterConfigurationSource>().To<FixedConfigurationSource>();
        _kernel.Bind<IReceiptConverterFactory>().To<ReceiptConverterFactory>().InSingletonScope();
        _kernel.Bind<IJobStatusReporter>().To<JamesStatusReporter>();
        _kernel.Bind<PrintServer>().ToSelf().InSingletonScope();
        _kernel.Bind<IDiscoveredPrintersReceiver>().To<JamesDiscoveredPrintersReceiver>().InSingletonScope();
        _kernel.Bind<PrinterDiscoveryBackgroundTask>().ToSelf().InSingletonScope();
        _kernel.Bind<HeartbeatBackgroundTask>().ToSelf().InSingletonScope();

        var printLoop = _kernel.Get<PrintLoop>();
        var discoveryTask = _kernel.Get<PrinterDiscoveryBackgroundTask>();
        var heartbeatTask = _kernel.Get<HeartbeatBackgroundTask>();
        var jobSourceTask = _kernel.Get<IPrintJobSource>();

        // Start background tasks
        _ = discoveryTask.StartAsync(_cts.Token);
        _ = heartbeatTask.StartAsync(_cts.Token);

        // Delay for a moment to allow discovery to find printers
        Console.WriteLine("Waiting for printer discovery...");
        await Task.Delay(3000);

        _ = jobSourceTask.StartAsync();

        await printLoop.StartAsync();

        IsRunning = true;
        Console.WriteLine("Print server started.");
    }
}
