using AppData;
using NKC_SDK;
using System;
using System.Net.Http.Headers;
using System.Web.UI;

namespace NKC_WF.site
{
    public partial class GlobalSearch : BasePage
    {
        /// <summary>
        /// ItemID selezionato
        /// </summary>
        protected int ItemID
        {
            get
            {
                int answ = 0;
                int.TryParse(hfItemID.Value, out answ);
                return answ;
            }
            set
            {
                hfItemID.Value = value.ToString();
            }
        }
        /// <summary>
        /// CartID selezionato
        /// </summary>
        public int CartID
        {
            get
            {
                return cmp_KS_Cart.CartID;
            }
            set
            {
                cmp_KS_Cart.CartID = value;
            }
        }
        /// <summary>
        /// BinID selezionato
        /// </summary>
        protected int BinID
        {
            get
            {
                return cmp_searchBin.BinID;
            }
            set
            {
                cmp_searchBin.BinID = value;
            }
        }
        /// <summary>
        /// Comando barcode letto
        /// </summary>
        protected string lastCmd
        {
            get
            {
                return hfLastBCode.Value;
            }
            set
            {
                hfLastBCode.Value = value;
                lastValidCmd = 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;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ((SiteContent)this.Master).showSearch = false;
                ItemID = 0;
                CartID = 0;
                showResults(false);
            }
            cmp_barcode.eh_doRefresh += Cmp_barcode_eh_doRefresh;
            cmp_barcode.eh_doReset += Cmp_barcode_eh_doReset;
        }

        private void Cmp_barcode_eh_doReset(object sender, EventArgs e)
        {
            cmp_searchItems.Visible = false;
        }

        private void Cmp_barcode_eh_doRefresh(object sender, EventArgs e)
        {

            bool doShowResults = false;
            // processo evento..
            lastCmd = cmp_barcode.inputAcquired.ToUpper();
            doShowResults = processLastCmd(doShowResults);
            // reset comando
            cmp_barcode.inputAcquired = "";
            // fix visualizzazione
            showResults(doShowResults);
        }

        private bool processLastCmd(bool doShowResults)
        {
            // processiamo barcode letto
            decodedData decoData = DLMan.decodeBcode(lastCmd);
            switch (decoData.codeType)
            {
                case codeType.Item:
                case codeType.ItemGeneric:
                case codeType.OtherItem:
                    cmp_barcode.showOutput(cssClass.success, $"{decoData.description}");
                    cmp_searchItems.itemDtmx = decoData.rawData;
                    BinID = 0;
                    CartID = 0;
                    ItemID = decoData.codeInt;
                    doShowResults = true;
                    break;
                case codeType.Material:
                    cmp_barcode.showOutput(cssClass.warning, $"{decoData.description}");
                    doShowResults = false;
                    break;
                case codeType.Sheet:
                    cmp_barcode.showOutput(cssClass.warning, $"{decoData.description}");
                    doShowResults = false;
                    break;
                case codeType.Stack:
                    cmp_barcode.showOutput(cssClass.warning, $"{decoData.description}");
                    doShowResults = false;
                    break;
                case codeType.Batch:
                    cmp_barcode.showOutput(cssClass.warning, $"{decoData.description}");
                    doShowResults = false;
                    break;
                case codeType.Cart:
                    cmp_barcode.showOutput(cssClass.success, $"{decoData.description}");
                    BinID = 0;
                    CartID = decoData.codeInt;
                    ItemID = 0;
                    cmp_KS_Cart.CartDtmx = decoData.rawData;
                    cmp_KS_Cart.doUpdate();
                    doShowResults = true;
                    break;
                case codeType.Bin:
                case codeType.BinProcessed:
                    cmp_barcode.showOutput(cssClass.success, $"{decoData.description}");
                    var tabBin = DLMan.taBN.getByKey(decoData.codeInt);
                    BinID = decoData.codeInt;
                    cmp_searchBin.isPainted = (decoData.codeType == codeType.BinProcessed);
                    CartID = 0;
                    ItemID = 0;
                    cmp_searchBin.doUpdate();
                    doShowResults = true;
                    break;
                case codeType.UNK:
                default:
                    cmp_barcode.showOutput(cssClass.danger, $"Unknown Data: {decoData.rawData} --> no action");
                    doShowResults = false;
                    break;
            }

            return doShowResults;
        }
        /// <summary>
        /// Determina visualizzazione risultati
        /// </summary>
        /// <param name="shoItems"></param>
        protected void showResults(bool showDetail)
        {
            cmp_searchItems.Visible = showDetail && ItemID > 0;
            divCart.Visible = showDetail && CartID > 0;
            divBin.Visible = showDetail && BinID > 0;
        }

    }
}