using AppData;
using NKC_SDK;
using SteamWare;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http.Headers;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NKC_WF.WebUserControls
{
    public enum KitAdvMode
    {
        NA = 0,
        PauseKit,
        PartsReorder,
        PartsReload,
        PartInvalidate,
        CartInvalidate
    }

    public partial class cmp_kittingSmartAdv : BaseUserControl
    {
        #region Protected Fields

        protected bool enableInvalidCart = true;
        protected bool enableInvalidPart = true;
        protected bool enablePause = false;
        protected bool enableReload = true;
        protected bool enableReorder = false;

        #endregion Protected Fields

        #region Protected Properties

        /// <summary>
        /// Sheet selezionato...
        /// </summary>
        protected KitAdvMode ActiveMode
        {
            set
            {
                hfActiveMode.Value = value.ToString();
            }
            get
            {
                KitAdvMode answ = KitAdvMode.NA;
                try
                {
                    answ = (KitAdvMode)Enum.Parse(typeof(KitAdvMode), hfActiveMode.Value);
                }
                catch
                { }
                return answ;
            }
        }

        /// <summary>
        /// ID univoco da IP
        /// </summary>
        protected string DeviceId
        {
            set
            {
                hfDeviceId.Value = value;
            }
            get
            {
                return hfDeviceId.Value;
            }
        }

        #endregion Protected Properties

        #region Private Methods

        private void cmp_kitReqRunning_eh_doRefresh(object sender, EventArgs e)
        {
            cmp_kitReqSched.doUpdate();
        }

        private void cmp_kitReqSched_eh_doRefresh(object sender, EventArgs e)
        {
            cmp_kitReqRunning.doUpdate();
        }

        private void cmp_kittingSmartAdvInvalid_eh_doRefresh(object sender, EventArgs e)
        {
        }

        private void cmp_kittingSmartAdvInvalid_eh_doReset(object sender, EventArgs e)
        {
            ActiveMode = KitAdvMode.NA;
            cmp_kittingSmartAdvInvalid.resetShowData();
            updateCurrData();
        }

        #endregion Private Methods

        #region Protected Methods

        protected void lbtKitInvalidateCart_Click(object sender, EventArgs e)
        {
            ActiveMode = KitAdvMode.CartInvalidate;
            updateCurrData();
        }

        protected void lbtKitInvalidatePart_Click(object sender, EventArgs e)
        {
            ActiveMode = KitAdvMode.PartInvalidate;
            updateCurrData();
        }

        protected void lbtKitReloadPart_Click(object sender, EventArgs e)
        {
            ActiveMode = KitAdvMode.PartsReload;
            updateCurrData();
        }

        protected void lbtKitReorder_Click(object sender, EventArgs e)
        {
            ActiveMode = KitAdvMode.PartsReorder;
            updateCurrData();
        }

        protected void lbtKitSusp_Click(object sender, EventArgs e)
        {
            ActiveMode = KitAdvMode.PauseKit;
            updateCurrData();
        }

        protected void lbtReset_Click(object sender, EventArgs e)
        {
            ActiveMode = KitAdvMode.NA;
            cmp_kittingSmartAdvInvalid.resetShowData();
            updateCurrData();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ActiveMode = (int)KitAdvMode.NA;
                updateCurrData();
            }
            cmp_kitReqRunning.eh_doRefresh += cmp_kitReqRunning_eh_doRefresh;
            cmp_kitReqSched.eh_doRefresh += cmp_kitReqSched_eh_doRefresh;
            cmp_kittingSmartAdvInvalid.eh_doRefresh += cmp_kittingSmartAdvInvalid_eh_doRefresh;
            cmp_kittingSmartAdvInvalid.eh_doReset += cmp_kittingSmartAdvInvalid_eh_doReset;
            cmp_kittingSmartAdvInvalid.PlaceCod = PlaceCod;
        }

        /// <summary>
        /// Aggiorna dati correnti (IP, batch, sheet...)
        /// </summary>
        protected void updateCurrData()
        {
            DeviceId = ComLib.GetIPAddress().Replace(".", "0").Replace(":", "0");
            // check button visibility + status attivo
            divAct01.Visible = false;
            divAct02.Visible = false;
            divActValid.Visible = false;
            lbtReset.Visible = false;
            lbtKitSusp.Visible = false;
            lbtKitReorder.Visible = false;
            lbtKitReloadPart.Visible = false;
            lbtKitInvalidatePart.Visible = false;
            lbtKitInvalidateCart.Visible = false;
            switch (ActiveMode)
            {
                case KitAdvMode.PauseKit:
                    lbtReset.Visible = true;
                    divAct01.Visible = enablePause;
                    break;

                case KitAdvMode.PartsReorder:
                    lbtReset.Visible = true;
                    divAct02.Visible = enableReorder;
                    // fixme... sistemare PLID
                    cmp_partMIA_byPLID.PLID = 40;
                    cmp_partMIA_byPLID.doUpdate();
                    break;

                case KitAdvMode.PartsReload:
                    lbtReset.Visible = true;
                    cmp_kittingSmartAdvInvalid.currMode = KSAdMode.RevalidPart;
                    divActValid.Visible = enableReload;
                    break;

                case KitAdvMode.PartInvalidate:
                    lbtReset.Visible = true;
                    cmp_kittingSmartAdvInvalid.currMode = KSAdMode.InvalidPart;
                    divActValid.Visible = enableInvalidPart;
                    break;

                case KitAdvMode.CartInvalidate:
                    lbtReset.Visible = true;
                    cmp_kittingSmartAdvInvalid.currMode = KSAdMode.InvalidCart;
                    divActValid.Visible = enableInvalidCart;
                    break;

                case KitAdvMode.NA:
                default:
                    lbtKitSusp.Visible = enablePause;
                    lbtKitReorder.Visible = enableReorder;
                    lbtKitReloadPart.Visible = enableReload;
                    lbtKitInvalidatePart.Visible = enableInvalidPart;
                    lbtKitInvalidateCart.Visible = enableInvalidCart;
                    break;
            }
        }

        #endregion Protected Methods
    }
}