using AppData;
using SteamWare;
using System;

namespace NKC_WF
{
    public partial class MachineUnload : BasePage
    {
        #region Protected Properties

        protected int numWaitSvg
        {
            get
            {
                int answ = 999;
                answ = memLayer.ML.getRCnt(redProdReq);
                return answ;
            }
        }

        /// <summary>
        /// stringa x contatore refresh redis...
        /// </summary>
        protected string redProdForceReload
        {
            get
            {
                return ComLib.redMachUnloadForce;
            }
        }

        /// <summary>
        /// stringa x contatore refresh redis...
        /// </summary>
        protected string redProdReq
        {
            get
            {
                return $"{ComLib.redMachUnloadCount}:{Session.SessionID}";
            }
        }

        protected int savedBatchId
        {
            get
            {
                return ComLib.getCurrBatchId(PlaceCod);
            }
        }

        protected int savedSheetId
        {
            get
            {
                return ComLib.getCurrSheetId(PlaceCod);
            }
        }

        #endregion Protected Properties

        #region Public Properties

        /// <summary>
        /// BatchId corrente...
        /// </summary>
        public int BatchId
        {
            set
            {
                hfBatchID.Value = value.ToString();
                ComLib.setCurrBatchId(PlaceCod, value);
            }
            get
            {
                int answ = 0;
                int.TryParse(hfBatchID.Value, out answ);
                return answ;
            }
        }

        /// <summary>
        /// SheetId corrente...
        /// </summary>
        public int SheetId
        {
            set
            {
                hfSheetID.Value = value.ToString();
                ComLib.setCurrSheetId(PlaceCod, value);
            }
            get
            {
                int answ = 0;
                int.TryParse(hfSheetID.Value, out answ);
                return answ;
            }
        }

        /// <summary>
        /// Verifica se sia da mostrare pagina MissingInAction
        /// </summary>
        public bool showPartMIA
        {
            get
            {
                bool answ = false;
                try
                {
                    answ = DLMan.taIL.getMissingByBatch(BatchId).Count > 0;
                }
                catch
                { }
                return answ;
            }
        }

        #endregion Public Properties

        #region Private Methods

        /// <summary>
        /// Aggiorna componente principale e child components
        /// </summary>
        private void doUpdate()
        {
            // verifico CI SIA la macchina...
            if (string.IsNullOrEmpty(cmp_MachSem.currMachine))
            {
                divMain.Visible = false;
                PlaceCod = "#";
            }
            else
            {
                PlaceCod = cmp_MachSem.currMachine;
                divMain.Visible = true;
                // imposto dati correnti
                bool doUpdate = setCurrData();
                // 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;
                if (doUpdate)
                {
                    cmp_MU_svgViewer.doUpdate();
                    upnlDrawings.DataBind();
                    upnlDrawings.Update();
                    upnlTitle.Update();
                }
                // definisco se mostrare PartMIA
                hlPartMIA.Visible = showPartMIA;
            }
        }

        /// <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(PlaceCod);
            if (currBunk != null)
            {
                bool chgBtLocal = BatchId != currBunk.BatchID;
                bool chgBtCache = savedBatchId != currBunk.BatchID;
                if (chgBtLocal || chgBtCache)
                {
                    BatchId = currBunk.BatchID;
                    SheetId = 0;
                    needUpdate = true;
                }

                DS_App.SheetListRow currSheet = ComLib.getCurrSheet(BatchId, PlaceCod);
                if (currSheet != null)
                {
                    bool chgShLocal = SheetId != currSheet.SheetID;
                    bool chgShCache = savedSheetId != currSheet.SheetID;
                    if (chgShLocal || chgShCache)
                    {
                        SheetId = currSheet.SheetID;
                        needUpdate = true;
                    }
                }
            }
            else
            {
                BatchId = 0;
                SheetId = 0;
                ComLib.setCurrBunkId(PlaceCod, 0);
                ComLib.man.resetSheetUnload(0);
            }
            // se sheet/bunk 0 --> update!
            if (BatchId <= 0 && SheetId <= 0)
            {
                needUpdate = true;
            }
            return needUpdate;
        }

        #endregion Private Methods

        #region Protected Methods

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

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ((SiteMaster)this.Master).showSearch = false;
                doUpdate();
                int timerSvgInt = memLayer.ML.CRI("timerSvgInt");
                timerSvgInt = timerSvgInt > 0 ? timerSvgInt : 500;
                timerSvg.Interval = timerSvgInt;
            }
        }

        /// <summary>
        /// Main SVG timer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void timerSvg_Tick(object sender, EventArgs e)
        {
            // in base al num di sec dell'ora decido cosa aggiornare...
            int counter = numWaitSvg;
            // ogni x cicli resetto batch/Foglio
            if (counter % 15 == 0)
            {
                SheetId = -3;
            }
            // ora faccio verifiche
            doUpdate();
            // ogni x cicli
            if (counter % 5 == 0)
            {
                cmp_MU_stats.doUpdate();
                upnlStats.Update();
            }
            // faccio update bin/cart ogni pari/dispari...
            if (counter % 2 == 0)
            {
                cmp_MU_carts.doUpdate();
                upnlCarts.Update();
            }
            else
            {
                cmp_MU_bins.doUpdate();
                upnlBins.Update();
            }
            incrNumWait();
        }

        #endregion Protected Methods
    }
}