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

namespace NKC_WF.WebUserControls
{
    public partial class cmp_batchList : BaseUserControl
    {
        /// <summary>
        /// modalità funzionamento controllo
        /// </summary>
        public BatchListMode listMode { get; set; } = BatchListMode.Standard;
        /// <summary>
        /// Caricamento pagina
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                deleteEnabled = false;
                cmp_numRow.numRow = 25;
                grView.PageSize = cmp_numRow.numRow;
                divDetail.Visible = false;
                // in base al ListMode decido cosa mostrare...
                checkFixOds();
                checkFixMode();
            }
            cmp_batchDetail.eh_doRefresh += Cmp_batchDetail_eh_doRefresh;
            cmp_numRow.eh_doRefresh += Cmp_numRow_eh_doRefresh;
        }
        private void checkFixOds()
        {
            string isPartVal = "";
            switch (listMode)
            {
                case BatchListMode.PartsEval:
                    isPartVal = "1";
                    break;
                case BatchListMode.Standard:
                default:
                    isPartVal = "0";
                    break;
            }
            hfPartValid.Value = isPartVal;
        }
        /// <summary>
        /// In base al modo richiesto imposta filtraggi...
        /// </summary>
        private void checkFixMode()
        {
            ListItem currItem = null;
            int valore = -100;
            if (listMode == BatchListMode.Standard)
            {
                foreach (var item in ddlStatus.Items)
                {
                    try
                    {
                        currItem = (ListItem)item;
                        int.TryParse(currItem.Value, out valore);
                        if (valore > 7)
                        {
                            currItem.Enabled = false;
                        }
                    }
                    catch
                    { }
                }
            }
            else
            {
                foreach (var item in ddlStatus.Items)
                {
                    try
                    {
                        currItem = (ListItem)item;
                        int.TryParse(currItem.Value, out valore);
                        if (valore < 8 && valore >= 0)
                        {
                            currItem.Enabled = false;
                        }
                    }
                    catch
                    { }
                }
            }
        }

        private void Cmp_numRow_eh_doRefresh(object sender, EventArgs e)
        {
            // recupero num righe ed aggiorno...
            grView.PageSize = cmp_numRow.numRow;
            checkFixOds();
            grView.DataBind();
        }

        private void Cmp_batchDetail_eh_doRefresh(object sender, EventArgs e)
        {
            resetSelezione();
        }

        /// <summary>
        /// Codice CSS in base a status...
        /// </summary>
        /// <param name="_status"></param>
        /// <returns></returns>
        public string cssByStatus(object _status)
        {
            string answ = "text-muted";
            int status = -1;
            int.TryParse(_status.ToString(), out status);
            switch (status)
            {
                case 1:
                    answ = "font-weight-bold text-info";
                    break;
                case 2:
                    answ = "font-weight-bold text-primary";
                    break;
                case 3:
                    answ = "font-weight-bold text-warning";
                    break;
                case 4:
                    answ = "font-weight-bold text-danger";
                    break;
                case 5:
                    answ = "font-weight-bold text-success";
                    break;
                case 6:
                    answ = "font-weight-bold text-secondary";
                    break;
                default:
                    break;
            }
            return answ;
        }
        /// <summary>
        /// Verifica se sia visualizzabile preview dato stato
        /// </summary>
        /// <param name="_status"></param>
        /// <returns></returns>
        public bool isPreviewVisible(object _status)
        {
            bool answ = false;
            int status = -1;
            int.TryParse(_status.ToString(), out status);
            if (status > 3 && status < 8)
            {
                answ = true;
            }
            return answ;
        }

        /// <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>
        /// comando reset
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtReset_Click(object sender, EventArgs e)
        {
            resetSelezione();
        }

        private void resetSelezione()
        {
            checkFixOds();
            grView.SelectedIndex = -1;
            grView.DataBind();
            divDetail.Visible = false;
            raiseReset();
        }

        /// <summary>
        /// BatchId selezionato
        /// </summary>
        protected int BatchIdSel
        {
            get
            {
                int answ = 0;
                int.TryParse(grView.SelectedValue.ToString(), out answ);
                return answ;
            }
        }
        protected void grView_SelectedIndexChanged(object sender, EventArgs e)
        {
            divDetail.Visible = true;
            // recupero BatchId selezionato
            cmp_batchDetail.BatchId = BatchIdSel;
        }

        public void doUpdate()
        {
            cmp_batchDetail.doUpdate();
            grView.DataBind();
        }

        protected void grView_PageIndexChanged(object sender, EventArgs e)
        {
            checkFixOds();
        }

        protected void lbRedoEval_Click(object sender, EventArgs e)
        {
            LinkButton lbt = (LinkButton)sender;
            if (!string.IsNullOrEmpty(lbt.CommandArgument))
            {
                // eseguo richiesta reset
                DataLayer.man.taBL.redoPartValid(lbt.CommandArgument);
                // eventualmente mando primo batch da validare...
                bool newValidSent = ComLib.sendFirstValidationBatch();
                // refresh!
                resetSelezione();
            }
        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            checkFixOds();
        }

        protected void lbtEnableDel_Click(object sender, EventArgs e)
        {
            deleteEnabled = true;
            resetSelezione();
        }
        protected void lbtDisableDel_Click(object sender, EventArgs e)
        {
            deleteEnabled = false;
            resetSelezione();
        }

        public bool deleteEnabled
        {
            get
            {
                bool answ = false;
                bool.TryParse(hfDeleteEnabled.Value, out answ);
                return answ;
            }
            set
            {
                hfDeleteEnabled.Value = value.ToString();
            }
        }
    }
}