using AppData;
using SteamWare;
using System;
using System.Web.UI;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_validationSummary : BaseUserControl
    {
        #region Private Methods

        /// <summary>
        /// fix visibilità buttons
        /// </summary>
        private void fixVisibility()
        {
            // controllo SE HO nuove PARTS da validare...
            var tabItems2Valid = DLMan.taIL.getNeedValid();
            var tabItemsUnValid = DLMan.taIL.getUnValid();
            var tabItems2ReValid = DLMan.taIL.getNeedRevalid();
            lbtValidateMissing.Visible = tabItems2Valid.Count > 0;
            lbtRevalidate.Visible = tabItemsUnValid.Count > 0;
            lbtReValidAll.Visible = tabItems2ReValid.Count > 0;
        }

        #endregion Private Methods

        #region Protected Methods

        protected void lbtForceRevali_Click(object sender, EventArgs e)
        {
            // rimetto da validare tramite stored
            try
            {
                DLMan.taBL.redoPartValid("*");
                // eventualmente mando primo batch da validare...
                bool newValidSent = ComLib.sendFirstValidationBatch();
            }
            catch
            { }
            // update!
            doUpdate();
        }

        protected void lbtRevalAll_Click(object sender, EventArgs e)
        {
            // rimetto da validare tramite stored
            try
            {
                // eventualmente mando primo batch da validare...
                bool newValidSent = ComLib.sendFirstValidationBatch();
            }
            catch
            { }
            // update!
            doUpdate();
        }

        protected void lbtRevalidate_Click(object sender, EventArgs e)
        {
            // rimetto da validare tramite stored
            try
            {
                DLMan.taBL.resetPartUnValid();
                // eventualmente mando primo batch da validare...
                bool newValidSent = ComLib.sendFirstValidationBatch();
            }
            catch
            { }
            // update!
            doUpdate();
        }

        protected void lbtValidateMissing_Click(object sender, EventArgs e)
        {
            // chiamo stored x creazione NUOVI BATCH...
            try
            {
                string cadBasePath = $"{memLayer.ML.CRS("nestBasePath")}{memLayer.ML.CRS("drawingFolder")}/";
                DLMan.taBL.createPartValBatch(cadBasePath);
                // eventualmente mando primo batch da validare...
                bool newValidSent = ComLib.sendFirstValidationBatch();
            }
            catch
            { }
            // update!
            doUpdate();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                fixVisibility();
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            doUpdate();
        }

        #endregion Protected Methods

        #region Public Methods

        /// <summary>
        /// Update forzato
        /// </summary>
        public void doUpdate(bool reportEvent = true)
        {
            fixVisibility();
            frmView.DataBind();
            if (reportEvent)
            {
                raiseEvent();
            }
        }

        #endregion Public Methods
    }
}