using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using EpsonPrinter.Model;
using ESCPOS_NET;
using ESCPOS_NET.Emitters;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;

namespace EpsonPrinter.Services
{
    public class EpsonPrintService
    {
        private readonly SerialPrinter _serialPrinter;
        private readonly IConfiguration _config;

        public EpsonPrintService(IConfiguration config)
        {
            _config = config;
            string comPort = _config["PrintSettings:ComPort"];
            int baudRate = Convert.ToInt32(_config["PrintSettings:BaudRate"]);
            _serialPrinter = new SerialPrinter(comPort,baudRate);
            
        }

        private void PrintReceipt(ReceiptModel model)
        {

        }


        public void PrintSaleOfDay(SaleOfDayModel model)
        {
            ICommandEmitter e = new EPSON();
            Init(e);
            PrintSaleOfDay(model,e);
        }

        private void Init(ICommandEmitter e)
        {
            _serialPrinter.Write(e.Initialize());
            _serialPrinter.Write(e.Enable());
            _serialPrinter.Write(e.CodePage(CodePage.ISO8859_15_LATIN9));
            _serialPrinter.Write(e.EnableAutomaticStatusBack());
        }

        //private void PrintReceipt(ReceiptModel model, ICommandEmitter e)
        //{
        //    List<byte[][]> printBytes = new List<byte[][]>();
        //    PrintHeader printHeader = new PrintHeader(e);
        //    printBytes.AddRange(printHeader.Print(model.Header));

        //    PrintBody printBody = new PrintBody(e,_config);
        //    printBytes.AddRange(printBody.Print(model.BodyModel));

        //    PrintFooter printFooter = new PrintFooter(e);
        //    printBytes.AddRange(printFooter.Print(model.Footer));

        //    //And at last, print
        //    byte[][] array = printBytes.SelectMany(c => c).ToArray();

        //    _serialPrinter.Write(array);
        //    _serialPrinter.Write(e.FeedLines(5));
        //    _serialPrinter.Write(e.FullCut());

        //}
        private void PrintSaleOfDay(SaleOfDayModel model, ICommandEmitter e)
        {
            List<byte[][]> data = new List<byte[][]>();

            int printWidth = Convert.ToInt32(_config["PrintSettings:PrintWidth"])-8;
            PrintString printString = new PrintString();

            byte[][] dateBytes =
            {
                e.SetStyles(PrintStyle.DoubleWidth),
                e.LeftAlign(),
                e.PrintLine($"Dato: {model.Date:dd-MM-yyyy}")
            };
            data.Add(dateBytes);

            byte[][] seperater =
            {
                e.SetStyles(PrintStyle.None),
                e.LeftAlign(),
                e.PrintLine($"-----------------------------------")
            };
            data.Add(seperater);


            foreach (SaleOfDayDetail saleOfDayDetail in model.SaleOfDayDetail)
            {
                //string pS = printString.MakePrintString(printWidth, saleOfDayDetail.Category, saleOfDayDetail.TotalSale.ToString(CultureInfo.InvariantCulture));
                string pS = $"{saleOfDayDetail.Category} : {saleOfDayDetail.TotalSale.ToString("#.#0")}";
                byte[][] totalPrice =
                {
                    e.SetStyles(PrintStyle.DoubleWidth),
                    e.LeftAlign(),
                    e.PrintLine(pS)
                };
                data.Add(totalPrice);
            }

            data.Add(seperater);

            //string totalSale = printString.MakePrintString(printWidth, "Salg", model.TotalSale.ToString(CultureInfo.InvariantCulture));
            string totalSale = $"Total salg: {model.TotalSale.ToString("#.#0")}";
            byte[][] tSBytes =
            {
                e.SetStyles(PrintStyle.DoubleWidth),
                e.LeftAlign(),
                e.PrintLine(totalSale)
            };
            data.Add(tSBytes);

            //string totalCustomer = printString.MakePrintString(printWidth, "Kunder", model.TotalCustomers.ToString());
            string totalCustomer = $"Antal kunder: {model.TotalCustomers}";
            byte[][] tCBytes =
            {
                e.SetStyles(PrintStyle.DoubleWidth),
                e.LeftAlign(),
                e.PrintLine(totalCustomer)
            };
            data.Add(tCBytes);

            byte[][] array = data.SelectMany(c => c).ToArray();
            _serialPrinter.Write(array);
            _serialPrinter.Write(e.FeedLines(5));
            _serialPrinter.Write(e.FullCut());
        }

    }
}
