using EpsonTest;
using Inspectron.Epson;
using Inspectron.Epson.PrintServer.Printers.Utils.FinalRecipe;
using Inspectron.Epson.PrintServer.Printers.Utils.FinalReceipt;
using Inspectron.Epson.PrintServer.Printers.Utils.InvoiceReceipt;
using Inspectron.Epson.PrintServer.Printers.Utils.KitchenReceipt;
using Inspectron.Epson.PrintServer.Printers.Utils.OrderItems;
using System.Text.Json;

// ============================================================================
// FINAL RECEIPT - HTML PRINTER
// ============================================================================
//var receipt = TestHelpers.BuildSampleFinalReceipt(true);
//var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });

//var converter = new FinalReceiptConverter(lineWidth: 48, bigFontLineWidth: 24);
//var printCommands = converter.Convert(serializedReceipt);

//TestHelpers.PrintCommandsToConsole(printCommands);

//var printer = await TestHelpers.CreateHtmlPrinterAsync();
//await TestHelpers.PrintCommandsToHtmlPrinterAsync(
//    printer,
//    printCommands,
//    logoPath: "ristorante-klinglers.ch-logo_white_bg.png");


// ============================================================================
// FINAL RECEIPT - EPSON PRINTER
// ============================================================================
//var receipt = TestHelpers.BuildSampleFinalReceipt();
//var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });

//var converter = new FinalReceiptConverter(lineWidth: 48, bigFontLineWidth: 24);
//var printCommands = converter.Convert(serializedReceipt);

//TestHelpers.PrintCommandsToConsole(printCommands);

//var printer = await TestHelpers.CreateEpsonPrinterAsync();
//await TestHelpers.PrintCommandsToEpsonPrinterAsync(printer, printCommands);


// ============================================================================
// INVOICE RECEIPT - HTML PRINTER
// ============================================================================
//var receipt = TestHelpers.BuildSampleInvoiceReceipt();
////var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });
//var fileContent = File.ReadAllText("invoice.json");
//var serializedReceipt = JsonSerializer.Deserialize<string>(fileContent);
//var converter = new InvoiceReceiptConverter(lineWidth: 48, bigFontLineWidth: 24);
//var printCommands = converter.Convert(serializedReceipt);

//TestHelpers.PrintCommandsToConsole(printCommands);

//var printer = await TestHelpers.CreateHtmlPrinterAsync();
//await TestHelpers.PrintCommandsToHtmlPrinterAsync(
//    printer,
//    printCommands,
//    logoPath: "ristorante-klinglers.ch-logo_white_bg.png");


// ============================================================================
// INVOICE RECEIPT - EPSON PRINTER
// ============================================================================
//var receipt = TestHelpers.BuildSampleInvoiceReceipt();
//var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });

//var converter = new InvoiceReceiptConverter(lineWidth: 48, bigFontLineWidth: 24);
//var printCommands = converter.Convert(serializedReceipt);

//TestHelpers.PrintCommandsToConsole(printCommands);

//var printer = await TestHelpers.CreateEpsonPrinterAsync();
//await TestHelpers.PrintCommandsToEpsonPrinterAsync(printer, printCommands);


// ============================================================================
// KITCHEN RECEIPT - HTML PRINTER
// ============================================================================
var receipt = TestHelpers.BuildSampleKitchenReceipt();
var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });
var deserializedReceipt = JsonSerializer.Deserialize<KitchenReceipt>(serializedReceipt);

var converter = new KitchenReceiptConverter(bigLineWidth: 20, lineWidth: 33);
var printCommands = converter.ConvertToPrintCommands(deserializedReceipt);

TestHelpers.PrintCommandsToConsole(printCommands);

var printer = await TestHelpers.CreateHtmlPrinterAsync(
    path: @".\kitchen_test.html",
    paperWidth: TestHelpers.KitchenPaperWidth);
await TestHelpers.PrintKitchenCommandsToHtmlPrinterAsync(printer, printCommands);


// ============================================================================
// KITCHEN RECEIPT (NEXT DISH) - HTML PRINTER
// ============================================================================
//var receipt = TestHelpers.BuildSampleKitchenReceiptNextDish();
//var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });
//var deserializedReceipt = JsonSerializer.Deserialize<KitchenReceipt>(serializedReceipt);

//var converter = new KitchenReceiptConverter(bigLineWidth: 18, lineWidth: 33);
//var printCommands = converter.ConvertToPrintCommands(deserializedReceipt);

//TestHelpers.PrintCommandsToConsole(printCommands);

//var printer = await TestHelpers.CreateHtmlPrinterAsync(
//    path: @".\kitchen_next_dish.html",
//    paperWidth: TestHelpers.KitchenPaperWidth);
//await TestHelpers.PrintKitchenCommandsToHtmlPrinterAsync(printer, printCommands);


// ============================================================================
// KITCHEN RECEIPT - EPSON PRINTER
// ============================================================================
//var receipt = TestHelpers.BuildSampleKitchenReceipt();
//var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });
//var deserializedReceipt = JsonSerializer.Deserialize<KitchenReceipt>(serializedReceipt);

//var converter = new KitchenReceiptConverter(bigLineWidth: 18, lineWidth: 33);
//var printCommands = converter.ConvertToPrintCommands(deserializedReceipt);

//TestHelpers.PrintCommandsToConsole(printCommands);

//var printer = await TestHelpers.CreateEpsonPrinterAsync(paperWidth: TestHelpers.KitchenPaperWidth);
//await TestHelpers.PrintCommandsToEpsonPrinterAsync(printer, printCommands, useDelay: true);


// ============================================================================
// ORDER ITEMS RECEIPT - HTML PRINTER
// ============================================================================
//var receipt = TestHelpers.BuildSampleOrderItemsReceipt();
//var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });
//Console.WriteLine(serializedReceipt);
//var deserializedReceipt = JsonSerializer.Deserialize<OrderItemsReceipt>(serializedReceipt);

//var converter = new ReceiptConverter(lineWidth: 50, bigFontLineWidth: 21);
//var printCommands = converter.ConvertToPrintCommands(deserializedReceipt);

//TestHelpers.PrintCommandsToConsole(printCommands);

//var printer = await TestHelpers.CreateHtmlPrinterAsync(path: @".\order_items.html");
//await TestHelpers.PrintCommandsToHtmlPrinterAsync(printer, printCommands);


// ============================================================================
// BASIC PRINTER STATUS CHECK
// ============================================================================
//var printer = await TestHelpers.CreateEpsonPrinterAsync();
//var status = await printer.GetPrinterStatusAsync();
//Console.WriteLine($"Printer Status: {status}");
//var paperStatus = await printer.GetPaperSensorStatusAsync();
//Console.WriteLine($"Paper Sensor Status: {paperStatus}");


// ============================================================================
// SIMPLE TEXT PRINT TEST
// ============================================================================
//var printer = await TestHelpers.CreateEpsonPrinterAsync();
//await printer.InitAsync();
//await printer.PrintTextAsync("Hello, World!\n");
//await printer.FeedLinesAsync(5);
//await printer.CutAsync();


// ============================================================================
// IMAGE PRINT TEST
// ============================================================================
//var printer = await TestHelpers.CreateEpsonPrinterAsync();
//await printer.LoadImageAsync("test.png", 100);
//await printer.PrintLoadedImage();
//await printer.FeedLinesAsync(5);
//await printer.CutAsync();


// ============================================================================
// FONT SIZE TEST
// ============================================================================
//var printer = await TestHelpers.CreateEpsonPrinterAsync();
//await printer.InitAsync();

//// Double size (24 chars per line)
//await printer.SetFontSizeAsync(2, 2);
//await printer.SelectFont(EpsonCommands.PrinterFont.A);
//await printer.PrintTextAsync("HHHH_HHHH_HHHH_HHHH_HHHH_HHHH_HHHH_\n");

//// Normal size (48 chars per line)
//await printer.SetFontSizeAsync(1, 1);
//await printer.SelectFont(EpsonCommands.PrinterFont.A);
//await printer.PrintTextAsync("HHHH_HHHH_HHHH_HHHH_HHHH_HHHH_HHHH_HHHH_HHHH_HHHH_HHHH_HHHH_\n");

//await printer.FeedLinesAsync(5);
//await printer.CutAsync();


// ============================================================================
// SOAP PRINTER TEST
// ============================================================================
//var printer = new SoapEpsonPrinter("localhost:8888");
//var print = new SoapEpsonPrint();
//print
//    .AddTextAlign(SoapEpsonPrint.ALIGN_CENTER)
//    .AddTextSize(2, 2)
//    .AddText("It works! Finally!!!")
//    .AddFeed()
//    .AddFeed()
//    .AddFeed()
//    .AddCut(SoapEpsonPrint.CUT_FEED);

//var task = await printer.Send(print);
//var status = await printer.GetStatus(print);
//Console.WriteLine($"Printer Status: {status}");
