using AppData;
using Newtonsoft.Json;
using SteamWare;
using System;
using System.Linq;
using System.Web.UI;

namespace NKC_WF.site
{
    public partial class BatchPreview : BasePage
    {
        /// <summary>
        /// Batch corrente...
        /// </summary>
        public int BatchId
        {
            get
            {
                int answ = memLayer.ML.QSI("BatchId");
                return answ;
            }
        }
        /// <summary>
        /// Indice bunk selezionato (0..n-1)
        /// </summary>
        protected int bunkIndex
        {
            get
            {
                return cmp_BP_bunkList.selIndex;
            }
            set
            {
                cmp_BP_bunkList.selIndex = value;
            }
        }
        /// <summary>
        /// BunkId selezionato
        /// </summary>
        protected int BunkId
        {
            get
            {
                return cmp_BP_bunkList.BunkIdSel;
            }
        }
        /// <summary>
        /// Indice sheet selezionato (0..n-1)
        /// </summary>
        protected int sheetIndex
        {
            get
            {
                return cmp_BP_sheetList.selIndex;
            }
            set
            {
                cmp_BP_sheetList.selIndex = value;
            }
        }
        /// <summary>
        /// BunkId selezionato
        /// </summary>
        protected int SheetId
        {
            get
            {
                return cmp_BP_sheetList.SheetIdSel;
            }
        }
        /// <summary>
        /// valore selezionato nello slider
        /// </summary>
        protected int valSlider
        {
            get
            {
                return cmp_slider.selValue;
            }
            set
            {
                cmp_slider.selValue = value;
            }
        }
        /// <summary>
        /// Chaive URL base x gestione sheet del batch corrente
        /// </summary>
        protected string tabSheetKey;
        /// <summary>
        /// Elenco fogli della gestione corrente
        /// </summary>
        protected DS_App.SheetListDataTable tabSheets
        {
            get
            {
                DS_App.SheetListDataTable answ = null;
                string rawData = memLayer.ML.getRSV(tabSheetKey);
                if (string.IsNullOrEmpty(rawData))
                {
                    answ = DLMan.taSHL.getByBatch(BatchId);
                    tabSheets = answ;
                }
                else
                {
                    answ = JsonConvert.DeserializeObject<DS_App.SheetListDataTable>(rawData);
                }
                return answ;
            }
            set
            {
                string rawData = JsonConvert.SerializeObject(value);
                memLayer.ML.setRSV(tabSheetKey, rawData, 60);
            }
        }
        /// <summary>
        /// Restituisce la riga dall'indice inserito (
        /// </summary>
        /// <param name="indice">valore 1..maxSheets</param>
        /// <returns></returns>
        protected DS_App.SheetListRow rigaSel(int indice)
        {
            DS_App.SheetListRow answ = null;
            // init var
            int maxNum = tabSheets.Count;
            // base 0 --> riduco di 1...
            indice--;
            // controllo valore sia accettabile...
            indice = indice < 0 ? 0 : indice;
            indice = indice >= maxNum ? maxNum - 1 : indice;
            try
            {
                answ = tabSheets[indice];
            }
            catch
            { }
            // restituisco!
            return answ;
        }
        /// <summary>
        /// Restituisce la PRIMA riga dal codice BUNK inserito
        /// </summary>
        /// <param name="BunkId">valore 1..numBunk</param>
        /// <returns></returns>
        protected DS_App.SheetListRow rigaByBunk(int BunkId)
        {
            DS_App.SheetListRow answ = null;
            try
            {
                answ = tabSheets.First(x => x.StackID == BunkId);
            }
            catch
            { }
            // restituisco!
            return answ;
        }
        /// <summary>
        /// Restituisce la riga dal codice Sheet inserito
        /// </summary>
        /// <param name="SheetId">valore 1..numBunk</param>
        /// <returns></returns>
        protected DS_App.SheetListRow rigaBySheet(int SheetId)
        {
            DS_App.SheetListRow answ = null;
            try
            {
                answ = tabSheets.First(x => x.StackID == SheetId);
            }
            catch
            { }
            // restituisco!
            return answ;
        }
        /// <summary>
        /// caicamento pagina
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ((SiteMaster)this.Master).showSearch = false;
                tabSheetKey = $"{memLayer.ML.redHash($"TabSheets")}:{BatchId}";
                // imposto bunk!
                cmp_BP_bunkList.BatchId = BatchId;
                setupSlider();
                doUpdate();
            }
            cmp_BP_bunkList.eh_doRefresh += Cmp_BP_bunkList_eh_doRefresh;
            cmp_BP_sheetList.eh_doRefresh += Cmp_BP_sheetList_eh_doRefresh;
            cmp_slider.eh_doRefresh += Cmp_slider_eh_doRefresh;
        }
        /// <summary>
        /// Setup slider da num fogli
        /// </summary>
        private void setupSlider()
        {
            cmp_slider.minVal = 1;
            cmp_slider.maxVal = tabSheets.Count;
            cmp_slider.selValue = 1;
        }


        private void doUpdate()
        {
            updateBySlider();
        }
        private void Cmp_slider_eh_doRefresh(object sender, EventArgs e)
        {
            updateBySlider();
        }
        /// <summary>
        /// Effettua update da Slider --> valore selezionato
        /// </summary>
        private void updateBySlider()
        {
            // il valore assoluto dello slider è già corretto... cerco nella tab!
            DS_App.SheetListRow currRow = rigaSel(valSlider);
            if (currRow != null)
            {
                // recupero indice bunk e sheets e seleziono
                bunkIndex = currRow.StackIndex - 1;
                cmp_BP_sheetList.BunkId = cmp_BP_bunkList.BunkIdSel;
                sheetIndex = currRow.SheetIndex - 1;
                // update svg...
                cmp_MU_svgViewer.SheetId = currRow.SheetID;
            }
        }

        private void Cmp_BP_sheetList_eh_doRefresh(object sender, EventArgs e)
        {
            // calcolo indice slider da chiave sheet...
            int num = 1;
            foreach (var item in tabSheets)
            {
                if (item.SheetID == cmp_BP_sheetList.SheetIdSel)
                {
                    break;
                }
                else
                {
                    num++;
                }
            }
            cmp_slider.selValue = num;
            // update svg...
            cmp_MU_svgViewer.SheetId = cmp_BP_sheetList.SheetIdSel;
        }

        private void Cmp_BP_bunkList_eh_doRefresh(object sender, EventArgs e)
        {
            DS_App.SheetListRow currRow = rigaByBunk(cmp_BP_bunkList.BunkIdSel);
            if (currRow != null)
            {
                // calcolo indice slider da chiave sheet...
                int num = 1;
                foreach (var item in tabSheets)
                {
                    if (item.SheetID == currRow.SheetID)
                    {
                        break;
                    }
                    else
                    {
                        num++;
                    }
                }
                cmp_slider.selValue = num;
                // recupero bunk e sheets e seleziono
                cmp_BP_sheetList.BunkId = cmp_BP_bunkList.BunkIdSel;
                sheetIndex = currRow.SheetIndex - 1;
                // update svg...
                cmp_MU_svgViewer.SheetId = currRow.SheetID;
            }
        }
    }
}