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

namespace NKC_WF.site
{
    public partial class DBG_QRCode_Unload : BasePage
    {
        #region Protected Properties

        protected string machine
        {
            get
            {
                return ddlPlaces.SelectedValue;
            }
        }

        protected int numRow
        {
            get
            {
                int answ = 10;
                int.TryParse(ddlNumRow.SelectedValue, out answ);
                return answ;
            }
        }

        protected int qrSize
        {
            get
            {
                int answ = 32;
                int.TryParse(ddlQrSize.SelectedValue, out answ);
                return answ;
            }
        }

        #endregion Protected Properties

        #region Public Properties

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

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

        #endregion Public Properties

        #region Private Methods

        /// <summary>
        /// Aggiorna componente principale e child components
        /// </summary>
        private void doUpdate()
        {
            imgQrLogin.ImageUrl = getLoginUrl();
            updateSize();
            setCurrData();
            // aggiorno child
            cmp_MU_bins.ShowQr = true;
            cmp_MU_bins.BatchId = BatchId;
            cmp_MU_carts.ShowQr = true;
            cmp_MU_carts.BatchId = BatchId;
            cmp_MU_Items.ShowQr = true;
            cmp_MU_Items.SheetID = SheetId;
        }

        /// <summary>
        /// Imposta dati correnti (Bunk / Sheet)
        /// </summary>
        private void setCurrData()
        {
            // recupero bunk corrente...
            DS_App.StackListRow currBunk = ComLib.getCurrBunk(machine);
            if (currBunk != null)
            {
                BatchId = currBunk.BatchID;
                DS_App.SheetListRow currSheet = ComLib.getCurrSheet(BatchId, machine);
                if (currSheet != null)
                {
                    SheetId = currSheet.SheetID;
                }
            }
        }

        private void updateSize()
        {
            imgQrLogin.Height = qrSize * 2;
            cmp_MU_bins.qrSize = qrSize;
            cmp_MU_carts.qrSize = qrSize;
            cmp_MU_Items.qrSize = qrSize;
            cmp_MU_Items.numRow = numRow;
        }

        #endregion Private Methods

        #region Protected Methods

        protected void ddlNumRow_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateSize();
        }

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

        protected void ddlQrSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateSize();
        }

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

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

        #endregion Protected Methods

        #region Public Methods

        /// <summary>
        /// restituisce URL immagine QRCode
        /// </summary>
        /// <returns></returns>
        public string getLoginUrl()
        {
            string baseUrl = $"{memLayer.ML.CRS("matrixUrl")}/HOME/QR_site/JSON?val=";
            string payload = "{'baseUrl':'{0}','parameters':['" + memLayer.ML.CRS("BaseUrl") + "']}";
            string answ = $"{baseUrl}{payload}";
            return answ;
        }

        #endregion Public Methods
    }
}