using Inspectron.Epson;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using Ninject;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Channels;
using Inspectron.Epson.PrintServer;
using Inspectron.Epson.PrintServer.ConfigurationSources;
using Inspectron.Epson.PrintServer.JobSources;
using Inspectron.Epson.PrintServer.PrinterAssinment;
using Inspectron.Epson.PrintServer.Printers;
using Inspectron.Epson.PrintServer.Printers.Utils;
using Inspectron.Epson.PrintServer.PrintServices;
using EpsonPrintService;

StandardKernel kernel = new StandardKernel();


var config = JsonSerializer.Deserialize<EpsonPrintServiceConfiguration>(
    File.ReadAllText("config.json"));

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>().ToMethod(sp=>new HtmlPrinterFactory("TM-U220II.html", paperWidth: 258,type:0x0d, logger:sp.Kernel.Get<ILogger>()));
//kernel.Bind<IPrinterFactory>().ToMethod(sp=>new HtmlPrinterFactory("TM-T30III.html",logger:sp.Kernel.Get<ILogger>()));
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<IAssignedPrinterRepository>().ToConstant(config);
kernel.Bind<IWrapperPrinterFactory>().To<PrinterWrapperFactory>().InSingletonScope();
kernel.Bind<IPrinterConfigurationSource>().To<FixedConfigurationSource>();
kernel.Bind<IReceiptConverterFactory>().To<ReceiptConverterFactory>().InSingletonScope();
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 printServer = kernel.Get<PrintServer>();
var discoveryTask = kernel.Get<PrinterDiscoveryBackgroundTask>();
var heartbeatTask = kernel.Get<HeartbeatBackgroundTask>();

//printServer.RegisterPrinter("10.0.20.12");


await printLoop.StartAsync();


var cts = new CancellationTokenSource();
Console.CancelKeyPress += (sender, e) =>
{
    Console.WriteLine("\nShutting down...");
    e.Cancel = true;
    cts.Cancel();
};

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

try
{
    await Task.Delay(Timeout.Infinite, cts.Token);
}
catch (TaskCanceledException)
{
    // Expected when Ctrl+C is pressed
}

