using AppData;
using NKC_SDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_paintingSmart : BaseUserControl
    {
        /// <summary>
        /// Comando barcode letto
        /// </summary>
        protected string lastCmd
        {
            get
            {
                return hfLastBCode.Value;
            }
            set
            {
                hfLastBCode.Value = value;
            }
        }
        /// <summary>
        /// ULTIMO Comando barcode VALIDO (!="") letto
        /// </summary>
        protected string lastValidCmd
        {
            get
            {
                return hfLastValidBCode.Value;
            }
            set
            {
                // solo se è !=""
                if (!string.IsNullOrEmpty(value))
                {
                    hfLastValidBCode.Value = value;
                }
            }
        }
        /// <summary>
        /// caricamento pagina
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // pulisco dir temp dai file dei cartellini stampati
                reportPrinter.obj.pulisciDir();
            }
            fixButtons();
            cmp_barcode.eh_doRefresh += Cmp_barcode_eh_doRefresh;
        }

        private void fixButtons()
        {
            lbtConfirmPaint.Visible = false;
        }

        private void doUpdate()
        {
        }

        private void Cmp_barcode_eh_doRefresh(object sender, EventArgs e)
        {
            bool doRaiseEv = false;
            lastCmd = cmp_barcode.inputAcquired.ToUpper();
            doRaiseEv = processLastCmd(doRaiseEv);
            doUpdate();
        }

        private bool processLastCmd(bool doRaiseEv)
        {
            // verifico il datamatrix dei BIN da pitturare
            if (string.IsNullOrEmpty(lastCmd)) doRaiseEv = true;
            // processiamo barcode letto
            decodedData decoData = DLMan.decodeBcode(lastCmd);
            switch (decoData.codeType)
            {
                case codeType.Item:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("Item")} - {traduci("Ignored")}: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.ItemGeneric:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("ItemGeneric")} - {traduci("Ignored")}: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Material:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("Material")} - {traduci("Ignored")}: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Sheet:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("Sheet")} - {traduci("Ignored")}: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Stack:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("Bunk")} - {traduci("Ignored")}: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Batch:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("Batch")} - {traduci("Ignored")}: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Cart:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("Cart")} - {traduci("Ignored")}: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Bin:
                    var tabBin = DLMan.taBN.getByKey(decoData.codeInt);
                    bool allOk = false;
                    // se ne trovo 1 --> OK verde
                    if (tabBin.Count == 1)
                    {
                        // se NON E' PAINTED ma è COMPLETE
                        var riga = tabBin[0];
                        allOk = (riga.Complete == 1 && riga.Painted == 0);
                    }
                    if (allOk)
                    {
                        // ora confronto cmd con ultimo valido
                        if (lastValidCmd != lastCmd)
                        {
                            cmp_barcode.showOutput(cssClass.success, $"{traduci("ValiBnCode")}: {decoData.description}");
                            // salvo
                            lastValidCmd = lastCmd;
                        }
                        else
                        {
                            // se uguale e valido --> stampo!
                            executePaintConfirm();
                            allOk = false;
                            doRaiseEv = false;
                        }
                    }
                    else
                    {
                        cmp_barcode.showOutput(cssClass.danger, $"{decoData.description} --> {traduci("NoValiAction")}");
                    }
                    lbtConfirmPaint.Visible = allOk;
                    break;
                case codeType.BinProcessed:
                    cmp_barcode.showOutput(cssClass.danger, $"{traduci("BinProcessed")} - {traduci("Ignored")}: {decoData.rawData} --> no action");
                    doRaiseEv = true;
                    break;
                case codeType.SecScreen:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("SecScreen")} - {traduci("Ignored")}: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.UNK:
                default:
                    cmp_barcode.showOutput(cssClass.danger, $"{traduci("UnknownData")}: {decoData.rawData} --> {traduci("NoValiAction")}");
                    doRaiseEv = true;
                    break;
            }
            return doRaiseEv;
        }


        protected void lbtConfirmPaint_Click(object sender, EventArgs e)
        {
            executePaintConfirm();
        }
        /// <summary>
        /// Effettua stampa nuova label BP
        /// </summary>
        private void printNewLabel(decodedData decoData)
        {
            // chiamo procedura stampa report x BIN pitturati
            string printer = DLMan.getPrinter("PC-MACHINE-PAINT-STATION");
            DLMan.stampaDoc(decoData.codeInt.ToString(), printer, tipoDocumento.docBinPost, Request.UserHostName);
            lgInfo($"PRINT req for BIN | codeInt : {decoData.codeInt} | currQueue: {printer} | tipoDoc: {tipoDocumento.docBinPost} | user: {Request.UserHostName} | IP: {currIpAddress}");
        }

        private void executePaintConfirm()
        {
            // processiamo barcode letto
            decodedData decoData = DLMan.decodeBcode(lastValidCmd);
            // lo indico come painted (update dtPainted)
            DLMan.taBN.updatePainted(decoData.codeInt, DateTime.Now);
            lgInfo($"cmp_paintingSmart | executePaintConfirm | codeInt: {decoData.codeInt} | raw: {decoData.rawData}");
            printNewLabel(decoData);
            // update tabelle!
            doUpdate();
            cmp_barcode.showOutput(cssClass.success, $"{traduci("PaintConfirmed")}: {decoData.description}");
        }
    }
}