using AppData;
using NKC_SDK;
using System;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_painting : BaseUserControl
    {
#if false
        /// <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;
                }
            }
        } 
#endif
        /// <summary>
        /// caricamento pagina
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
#if false
            if (!Page.IsPostBack)
            {
                // pulisco dir temp dai file dei cartellini stampati
                reportPrinter.obj.pulisciDir();
            }
            fixButtons();
            cmp_barcode.eh_doRefresh += Cmp_barcode_eh_doRefresh; 
#endif
        }

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

        public void doUpdate()
        {
            cmp_paint_binsIN.doUpdate();
            cmp_paint_binsOUT.doUpdate();
        }

#if false
        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 = DataLayer.man.decodeBcode(lastCmd);
            switch (decoData.codeType)
            {
                case codeType.UNK:
                    cmp_barcode.showOutput("text-danger", $"Unknown Data: {decoData.rawData} --> no action");
                    doRaiseEv = true;
                    break;
                case codeType.Item:
                    cmp_barcode.showOutput("badge badge-warning", $"Item - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.ItemGeneric:
                    cmp_barcode.showOutput("badge badge-warning", $"Item Generic - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Material:
                    cmp_barcode.showOutput("badge badge-warning", $"Material - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Sheet:
                    cmp_barcode.showOutput("badge badge-warning", $"Sheet - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Stack:
                    cmp_barcode.showOutput("badge badge-warning", $"BUNK - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Batch:
                    cmp_barcode.showOutput("badge badge-warning", $"Batch - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Cart:
                    cmp_barcode.showOutput("badge badge-warning", $"Cart - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;
                case codeType.Bin:
                    var tabBin = DataLayer.man.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("badge badge-success", $"Valid BN Code: {decoData.description}");
                            // salvo
                            lastValidCmd = lastCmd;
                        }
                        else
                        {
                            // se uguale e valido --> stampo!
                            executePaintConfirm();
                            allOk = false;
                            doRaiseEv = false;
                        }
                    }
                    else
                    {
                        cmp_barcode.showOutput("badge badge-danger", $"{decoData.description} --> no valid action");
                    }
                    lbtConfirmPaint.Visible = allOk;
                    break;
                case codeType.BinProcessed:
                    cmp_barcode.showOutput("text-danger", $"BinProcessed - ignored: {decoData.rawData} --> no action");
                    doRaiseEv = true;
                    break;
                case codeType.SecScreen:
                    cmp_barcode.showOutput("badge badge-warning", $"SecScreen - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;
                default:
                    cmp_barcode.showOutput("text-danger", $"Unknown Data: {decoData.rawData} --> no action");
                    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 = DataLayer.man.getPrinter("PC-MACHINE-PAINT-STATION");
            DataLayer.man.stampaDoc(decoData.codeInt.ToString(), printer, tipoDocumento.docBinPost, Request.UserHostName);
        }

        private void executePaintConfirm()
        {
            // processiamo barcode letto
            decodedData decoData = DataLayer.man.decodeBcode(lastValidCmd);
            // lo indico come painted (update dtPainted)
            DataLayer.man.taBN.updatePainted(decoData.codeInt, DateTime.Now);
            printNewLabel(decoData);
            // update tabelle!
            doUpdate();
            cmp_barcode.showOutput("badge badge-success", $"Paint confirmed: {decoData.description}");
        } 
#endif
    }
}