using Inspectron.Epson;
using Inspectron.Epson.PrintServer.Printers.Utils;
using Inspectron.Epson.PrintServer.Printers.Utils.KitchenReceipt;
using Inspectron.Epson.Templates.Configuration;
using Inspectron.Epson.Templates.Interpreter;
using Inspectron.Epson.Templates.Language;
using System.Text.Json;



// Add additional info




PrinterProfile _profileT30 = new("tm-t30iii", "TM-T30III", 48, 24, false);
PrinterProfile _profile220 = new("tm-220", "TM-220", 33, 18, true);


var printCommands = CreateCommands(_profileT30, "Templates\\final-receipt.template", "Samples\\final-receipt.json");


Console.WriteLine("=== PRINT COMMANDS ===\n");
var printer = new HtmlPrinter(@".\test.html", paperWidth: 384);
await printer.ConnectAsync("");
bool useDelay = false;
//var printer = new EpsonPrinter();
//await printer.ConnectAsync("127.0.0.1", 8888);
if (useDelay) await Task.Delay(200);
await printer.FeedLinesAsync(1);
//await printer.SetCustomLineSpacing(22);
foreach (var command in printCommands)
{
    string attributes = "";
    if (command.IsBig) attributes += "[BIG] ";
    if (command.IsBold) attributes += "[BOLD] ";
    if (command.IsRed) attributes += "[RED] ";

    Console.WriteLine($"{attributes}{command.Text}");
    await printer.SetBiggerFontTM220(command.IsBig, command.IsTall , secondaryFont: false);



    if (useDelay) await Task.Delay(200);

    await printer.SetRedColor(command.IsRed);

    if (useDelay) await Task.Delay(200);

    await printer.SetEmphasized(command.IsBold);
    if (useDelay) await Task.Delay(200);

    await printer.PrintTextAsync(command.Text + "\n");
    if (useDelay) await Task.Delay(200);

}

await printer.FeedLinesAsync(5);
if (useDelay) await Task.Delay(200 * 5);
await printer.CutAsync();


List<PrintCommand> CreateCommands(PrinterProfile printerProfile, string templateFile,string jsonFile)
{
    var lexer = new Lexer(File.ReadAllText(templateFile));
    var parser = new Parser(lexer.Tokenize().Tokens);
    var template = parser.Parse().Template;

    var interpreter = new TemplateInterpreter(printerProfile);
    var printCommands = interpreter.Interpret(template, File.ReadAllText(jsonFile));
    return printCommands;

}