using AppData;
using NKC_SDK;
using SteamWare;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_batchDetail : BaseUserControl
    {
        #region Protected Properties

        protected bool BatchIsAncestor
        {
            get
            {
                return ComLib.BType(BatchId) == BatchType.Ancestor;
            }
        }

        #endregion Protected Properties

        #region Public Properties

        public int BatchId
        {
            set
            {
                hfBatchId.Value = value.ToString();
                frmView.DataBind();
                cmp_batchDetailSplit.BatchId = value;
                cmp_batchDetailMongo.BatchId = value;
                if (BatchId > 0)
                {
                    fixDisplayDesc();
                }
            }
            get
            {
                int answ = 0;
                int.TryParse(hfBatchId.Value, out answ);
                return answ;
            }
        }

        /// <summary>
        /// verifica possibilità avvio TASK x presenza task NON chiusi
        /// </summary>
        /// <returns></returns>
        public bool canStartNew
        {
            get
            {
                return ComLib.canStartNew;
            }
        }

        #endregion Public Properties

        #region Private Methods

        private void fixDisplayDesc()
        {
            divSplit.Visible = BatchIsAncestor;
            cmp_batchDetailMongo.Visible = !BatchIsAncestor;
        }

        #endregion Private Methods

        #region Protected Methods

        /// <summary>
        /// Accetta Nesting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtAccept_Click(object sender, EventArgs e)
        {
            // registro accettazione Nesting
            DLMan.taBL.acceptBatch(BatchId, DLMan.CodSoggCurrUser);
            raiseEvent();
        }

        /// <summary>
        /// Riporta nesting a stato ESTIMATED
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtResetNest_Click(object sender, EventArgs e)
        {
            // ri-assegnazione ordini/kit a batch ancestor
            DLMan.taOLT.setBatchSplit(BatchId, false);
            // registro accettazione Nesting
            DLMan.taBL.refuseNesting(BatchId, DLMan.CodSoggCurrUser);
            raiseEvent();
        }

        protected void lbtSendEstim_Click(object sender, EventArgs e)
        {
            // invia a redis una richiesta...
            ComLib.sendMaterials();
            ComLib.sendBatchReq(BatchId, "Estimation", 3, false);

            // registro su DB nesting iniziato...
            DLMan.taBL.updateStatus(BatchId, (int)BatchStatus.EstimationRequested, "", 0);
            raiseEvent();
        }

        protected void lbtSendNesting_Click(object sender, EventArgs e)
        {
            // invia a redis a a richiesta...
            ComLib.sendMaterials();
            // controllo SE SIA un batch ANCESTOR --> invio il PRIMO batch da calcolare...
            if (BatchIsAncestor)
            {
                // effettuo VERA ri-assegnazione ordini/kit a nuovi batch
                DLMan.taOLT.setBatchSplit(BatchId, true);

                // invio il PRIMO batch descendant
                var tabDesc = ComLib.BatchDescendant(BatchId);
                if (tabDesc != null && tabDesc.Count > 0)
                {
                    int BatchDescId = tabDesc[0].BatchID;
                    ComLib.sendBatchReq(BatchDescId, "Nesting", 2, false);
                    // registro su DB nesting iniziato...
                    DLMan.taBL.updateStatus(BatchDescId, (int)BatchStatus.NestRequested, "", -1);
                }
            }
            else
            {
                // invio il batch corrente
                ComLib.sendBatchReq(BatchId, "Nesting", 2, false);
                // registro su DB nesting iniziato...
                DLMan.taBL.updateStatus(BatchId, (int)BatchStatus.NestRequested, "", -1);
            }

            raiseEvent();
        }

        protected void lbtStopEstim_Click(object sender, EventArgs e)
        {
            // resetto richiesta
            ComLib.resetBatchReq();
            // registro su DB nesting iniziato...
            DLMan.taBL.updateStatus(BatchId, (int)BatchStatus.Imported, "", -1);
            raiseEvent();
        }

        protected void lbtStopNesting_Click(object sender, EventArgs e)
        {
            // resetto richiesta
            ComLib.resetBatchReq();
            // registro su DB nesting iniziato...
            DLMan.taBL.updateStatus(BatchId, (int)BatchStatus.EstimationDone, "", -1);
            raiseEvent();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Effettua divisione evitando zeri a den...
        /// </summary>
        /// <param name="num"></param>
        /// <param name="den"></param>
        /// <returns></returns>
        protected double ratioProt(double num, double den)
        {
            den = den == 0 ? 1 : den;
            return num / den;
        }

        #endregion Protected Methods

        #region Public Methods

        /// <summary>
        /// Converte il codice stato in effettivo campo
        /// </summary>
        /// <param name="_status"></param>
        /// <returns></returns>
        public string BStatus(object _status)
        {
            string answ = ComLib.BatchStatusDescr(_status);
            return answ;
        }

        /// <summary>
        /// Controlla se lo stato sia uguale a quello richiesto
        /// </summary>
        /// <param name="_status"></param>
        /// <param name="statusReq"></param>
        /// <returns></returns>
        public bool checkStatus(object _status, BatchStatus statusReq)
        {
            bool answ = false;
            int status = -1;
            int.TryParse(_status.ToString(), out status);
            answ = (status == (int)statusReq);
            return answ;
        }

        /// <summary>
        /// Controlla se il  type sia uguale a quello richiesto
        /// </summary>
        /// <param name="_bType"></param>
        /// <param name="bTypeReq"></param>
        /// <returns></returns>
        public bool checkType(object _bType, BatchType bTypeReq)
        {
            bool answ = false;
            int bType = -1;
            int.TryParse(_bType.ToString(), out bType);
            answ = (bType == (int)bTypeReq);
            return answ;
        }

        public void doUpdate()
        {
            frmView.DataBind();
            fixDisplayDesc();
            if (divSplit.Visible)
            {
                cmp_batchDetailSplit.doUpdate();
            }
        }

        #endregion Public Methods
    }
}