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

namespace NKC_WF.site
{
    public partial class GlobalSearch : BasePage
    {
        /// <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;
                cmp_searchItems.Visible = 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 = DataLayer.man.decodeBcode(lastCmd);
            switch (decoData.codeType)
            {
                case codeType.UNK:
                    cmp_barcode.showOutput("text-danger", $"Unknown Data: {decoData.rawData} --> no action");
                    doShowResults = false;
                    break;
                case codeType.Item:
                    cmp_barcode.showOutput("badge badge-success", $"Valid IT Code: {decoData.rawData}");
                    cmp_searchItems.itemDtmx = decoData.rawData;
                    doShowResults = true;
                    break;
                case codeType.ItemGeneric:
                    cmp_barcode.showOutput("badge badge-success", $"Valid IG Code: {decoData.rawData}");
                    cmp_searchItems.itemDtmx = decoData.rawData;
                    doShowResults = true;
                    break;
                case codeType.OtherItem:
                    cmp_barcode.showOutput("badge badge-success", $"Valid Generic PART Code: {decoData.rawData}");
                    doShowResults = false;
                    break;
                case codeType.Material:
                    cmp_barcode.showOutput("badge badge-warning", $"Material - ignored: {decoData.description}");
                    doShowResults = false;
                    break;
                case codeType.Sheet:
                    cmp_barcode.showOutput("badge badge-warning", $"Sheet - ignored: {decoData.description}");
                    doShowResults = false;
                    break;
                case codeType.Stack:
                    cmp_barcode.showOutput("badge badge-warning", $"BUNK - ignored: {decoData.description}");
                    doShowResults = false;
                    break;
                case codeType.Batch:
                    cmp_barcode.showOutput("badge badge-warning", $"Batch - ignored: {decoData.description}");
                    doShowResults = false;
                    break;
                case codeType.Cart:
                    cmp_barcode.showOutput("badge badge-success", $"Cart - ignored: {decoData.description}");
                    doShowResults = false;
                    break;
                case codeType.Bin:
                    cmp_barcode.showOutput("badge badge-success", $"Bin - ignored: {decoData.description}");
                    doShowResults = false;
                    break;
                default:
                    cmp_barcode.showOutput("text-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 shoItems)
        {
            cmp_searchItems.Visible = shoItems;
        }

    }
}