using AppData;
using SteamWare;
using System;

namespace NKC_WF
{
    public partial class MachineUnload : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ((SiteMaster)this.Master).showSearch = false;
                doUpdate();
            }
        }

        /// <summary>
        /// BatchId corrente...
        /// </summary>
        public int BatchId
        {
            set
            {
                hfBatchID.Value = value.ToString();
            }
            get
            {
                int answ = 0;
                int.TryParse(hfBatchID.Value, out answ);
                return answ;
            }
        }
        /// <summary>
        /// SheetId corrente...
        /// </summary>
        public int SheetId
        {
            set
            {
                hfSheetID.Value = value.ToString();
            }
            get
            {
                int answ = 0;
                int.TryParse(hfSheetID.Value, out answ);
                return answ;
            }
        }
        /// <summary>
        /// Aggiorna componente principale e child components
        /// </summary>
        private void doUpdate()
        {
            // imposto dati correnti
            bool doUpdate = setCurrData();
            if (doUpdate)
            {
                // aggiorno child
                cmp_MU_stats.BatchId = BatchId;
                cmp_MU_bins.BatchId = BatchId;
                cmp_MU_carts.BatchId = BatchId;
                cmp_MU_svgViewer.BatchId = BatchId;
                cmp_MU_svgViewer.SheetId = SheetId;
                cmp_MU_svgViewer.doUpdate();
            }
        }
        /// <summary>
        /// Imposta dati correnti (Bunk / Sheet) indicando SE fare update
        /// </summary>
        private bool setCurrData()
        {
            bool needUpdate = false;
            // recupero bunk corrente...
            DS_App.StackListRow currBunk = ComLib.getCurrBunk();
            if (currBunk != null)
            {
                if (BatchId != currBunk.BatchID)
                {
                    BatchId = currBunk.BatchID;
                    needUpdate = true;
                }

                DS_App.SheetListRow currSheet = ComLib.getCurrSheet(BatchId);
                if (currSheet != null)
                {
                    if (SheetId != currSheet.SheetID)
                    {
                        SheetId = currSheet.SheetID;
                        needUpdate = true;
                    }
                }
            }
            else
            {
                BatchId = 0;
                ComLib.man.resetSheetUnload(0);
            }
            // se sheet/bunk 0 --> update!
            if (BatchId == 0 && SheetId == 0)
            {
                needUpdate = true;
            }
            return needUpdate;
        }

        protected void timerSvg_Tick(object sender, EventArgs e)
        {
            // in base al num di sec dell'ora decido cosa aggiornare...
            int counter = numWaitSvg;
            doUpdate();
            // ogni x cicli
            if (counter % 5 == 0)
            {
                cmp_MU_stats.doUpdate();

#if false
                // verifico se c'è richiesta reload UNLOAD...
                if (!string.IsNullOrEmpty(memLayer.ML.getRSV(redProdForceReload)))
                {
                    Response.Redirect(Request.RawUrl);
                } 
#endif
            }
            if (counter % 2 == 0)
            {
                cmp_MU_carts.doUpdate();
                cmp_MU_bins.doUpdate();
            }
            incrNumWait();
        }
        /// <summary>
        /// stringa x contatore refresh redis...
        /// </summary>
        protected string redProdReq
        {
            get
            {
                return $"{ComLib.redMachUnloadCount}:{Session.SessionID}";
            }
        }
        /// <summary>
        /// stringa x contatore refresh redis...
        /// </summary>
        protected string redProdForceReload
        {
            get
            {
                return ComLib.redMachUnloadForce;
            }
        }

        protected int numWaitSvg
        {
            get
            {
                return memLayer.ML.getRCnt(redProdReq);
            }
        }
        /// <summary>
        /// Incrementa counter redis
        /// </summary>
        protected void incrNumWait()
        {
            if (numWaitSvg > 30)
            {
                memLayer.ML.resetRCnt(redProdReq);
                Response.Redirect(Request.RawUrl);
            }
            memLayer.ML.setRCntI(redProdReq);
        }
    }
}