using AppData;
using NKC_SDK;
using System;
using System.Diagnostics;
using System.Web.UI;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_stackBuilding : BaseUserControl
    {
        /// <summary>
        /// Comando barcode letto
        /// </summary>
        protected string lastCmd
        {
            get
            {
                return hfLastBCode.Value;
            }
            set
            {
                hfLastBCode.Value = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // pulisco dir temp dai file dei cartellini stampati
                reportPrinter.obj.pulisciDir();
                chkDetail.Text = traduci("PrintDetail");
            }
            cmp_barcode.eh_doRefresh += Cmp_barcode_eh_doRefresh;
        }
        /// <summary>
        /// Verifica bvisibilità button stampa
        /// ...se NON ci sono altri campi "next" allora posso mostrare stampa...
        /// </summary>
        private void checkPrint()
        {
            DS_App.SheetListDataTable nextTbl = DLMan.taSHL.getNextByStack(StackId);
            // se tab vuota --> HO FINITO, mostro stampa...
            divPrint.Visible = (nextTbl.Count == 0);
        }


        private void Cmp_barcode_eh_doRefresh(object sender, EventArgs e)
        {

            bool doRaiseEv = false;
            lastCmd = cmp_barcode.inputAcquired.ToUpper();
            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.UNK:
                    cmp_barcode.showOutput(cssClass.danger, traduci("UnkownData"));
                    doRaiseEv = true;
                    break;
                case codeType.Item:
                case codeType.ItemGeneric:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("Part")} - {traduci("Ignored")}");
                    doRaiseEv = true;
                    break;
                case codeType.Material:
                    // verifico SE il materiale SIA quello dello sheet richiesto dallo stack/bunk corrente...
                    DS_App.SheetListDataTable nextTbl = DLMan.taSHL.getNextByStack(StackId);
                    // se tab vuota --> HO FINITO!!!!
                    if (nextTbl.Count == 0)
                    {
                        cmp_barcode.showOutput(cssClass.warning, $"{traduci("BunkCompleted")}, {decoData.description}");
                    }
                    // se ho valori --> controllo se corretto...
                    else
                    {
                        string codReq = nextTbl[0].MatExtCode.ToString();
                        if (codReq == decoData.codeInt.ToString())
                        {
                            cmp_barcode.showOutput(cssClass.success, $"{traduci("SheetRecorded")}: {decoData.description}");
                            // chiamo stored x indicare preparato
                            DLMan.taSHL.setPrepared(nextTbl[0].SheetID);
                            lgInfo($"cmpStackBuilding | taSHL.setPrepared | SheetID: {nextTbl[0].SheetID}");
                        }
                        else
                        {
                            cmp_barcode.showOutput(cssClass.danger, $"{traduci("WrongSheet")}: {decoData.description}");
                        }
                    }
                    break;
                case codeType.Sheet:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("Sheet")} - {traduci("Ignored")}");
                    doRaiseEv = true;
                    break;
                case codeType.Stack:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("Bunk")} - {traduci("Ignored")}");
                    doRaiseEv = true;
                    break;
                case codeType.Batch:
                    cmp_barcode.showOutput(cssClass.warning, $"{traduci("Batch")} - {traduci("Ignored")}");
                    doRaiseEv = true;
                    break;
                default:
                    break;
            }
            lastCmd = "";
            cmp_barcode.inputAcquired = "";
            cmp_barcode.resetInput();
            return doRaiseEv;
        }

        public void doUpdate()
        {
            cmp_StackBuildDetail.doUpdate();
            checkPrint();
        }

        public int StackId
        {
            set
            {
                cmp_StackBuildDetail.StackId = value;
                lastCmd = "";
                cmp_barcode.resetMessage();
                ComLib.updateBatchPositionByBunk(value);
                doUpdate();
            }
            get
            {
                return cmp_StackBuildDetail.StackId;
            }
        }

        /// <summary>
        /// Chiesta stampa stack corrente
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtPrint_Click(object sender, EventArgs e)
        {
            // chiamo procedura stampa report x stack
            string currQueue = DLMan.getPrinter("PC-MACHINE-STACK-BUILD");
            // se richeisto detail --> coda Detail...
            if (chkDetail.Checked)
            {
                currQueue += "Detail";
            }
            DLMan.stampaDoc(StackId.ToString(), currQueue, tipoDocumento.docStack, Request.UserHostName);
            lgInfo($"PRINT req | StackId : {StackId} | currQueue: {currQueue} | tipoDoc: {tipoDocumento.docStack} | user: {Request.UserHostName} | IP: {currIpAddress}");
            // aggiorno stato BATCH
            ComLib.updateBatchPositionByBunk(StackId);
        }

    }
}