using AppData;
using SteamWare;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_MachSem : BaseUserControl
    {
        #region Public Properties

        public string currMachine
        {
            get
            {
                string answ = "";
                answ = memLayer.ML.QSS("mach");
                return answ;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    string url = $"{currPage}?mach={value}";
                    Response.Redirect(url);
                }
            }
        }

        public string currPage
        {
            get
            {
                string url = HttpContext.Current.Request.Url.PathAndQuery;
                if (url.Contains("?mach="))
                {
                    url = url.Substring(0, url.IndexOf("?mach="));
                }
                return url;
            }
        }

        public bool machSelected
        {
            get
            {
                return (!string.IsNullOrEmpty(currMachine));
            }
        }

        #endregion Public Properties

        #region Private Methods

        private void checkVisibility()
        {
            divSelect.Visible = !machSelected;
            divRuntime.Visible = machSelected;
            hfMachClass.Value = currMachine == "NE01" ? "dark" : "primary";
            lbtReset.Attributes.Remove("class");
            lbtReset.Attributes.Add("class", $"btn btn-block text-center btn-{hfMachClass.Value}");
        }

        private DS_App.StatusDecodeRow getState(List<NKC_SDK.MachineStatData> currState, string name)
        {
            // recupero dati
            DS_App.StatusDecodeRow thisState = null;
            try
            {
                var currInfo = currState.Where(x => x.Machine == currMachine).FirstOrDefault();
                if (currInfo != null)
                {
                    var lastState = currInfo.Records.Where(x => x.Station == name).OrderByDescending(x => x.DtRecord).FirstOrDefault();
                    if (lastState == null)
                    {
                        // aggiungo ND
                    }
                    else
                    {
                        // verifico la decodifica x ognuno
                        thisState = ComLib.checkStationDecode(name, lastState.Code);
                    }
                }
            }
            catch
            { }

            return thisState;
        }

        #endregion Private Methods

        #region Protected Methods

        protected void ddlMachine_SelectedIndexChanged(object sender, EventArgs e)
        {
            currMachine = ddlMachine.SelectedValue;
        }

        protected void lbtReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(currPage);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            hfMachine.Value = currMachine;
            checkVisibility();
            lbtReset.DataBind();
        }

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

        #endregion Protected Methods

        #region Public Methods

        public void doUpdate()
        {
            // verifico status delle 3 parti e mostro relativo semaforo...
            hfCode01.Value = "00";
            hfCss01.Value = "secondary";
            hfStat01.Value = "UNKNOWN";
            hfCode02.Value = "00";
            hfCss02.Value = "secondary";
            hfStat02.Value = "UNKNOWN";
            hfCode03.Value = "00";
            hfCss03.Value = "secondary";
            hfStat03.Value = "UNKNOWN";

            // leggo da redis lo status
            var currState = ComLib.prodMachStateDataGet();

            // traduco!
            DS_App.StatusDecodeRow state01 = getState(currState, "PRINTER");
            if (state01 != null)
            {
                hfCode01.Value = state01.Code;
                hfCss01.Value = state01.Css;
                hfStat01.Value = state01.Descript;
            }

            DS_App.StatusDecodeRow state02 = getState(currState, "NC_MACHINE");
            if (state02 != null)
            {
                hfCode02.Value = state02.Code;
                hfCss02.Value = state02.Css;
                hfStat02.Value = state02.Descript;
            }

            DS_App.StatusDecodeRow state03 = getState(currState, "UNLOADER");
            if (state03 != null)
            {
                hfCode03.Value = state03.Code;
                hfCss03.Value = state03.Css;
                hfStat03.Value = state03.Descript;
            }
        }

        #endregion Public Methods
    }
}