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_KS_Items : BaseUserControl
    {
        /// <summary>
        /// Cart selezionato
        /// </summary>
        public int CartID
        {
            get
            {
                return cmp_KS_currCart.CurrId;
            }
            set
            {
                cmp_KS_currCart.CurrId = value;
            }
        }
        /// <summary>
        /// Matrix Bin selezionato
        /// </summary>
        public string CartDtmx
        {
            get
            {
                return cmp_KS_currCart.Dtmx;
            }
            set
            {
                cmp_KS_currCart.Dtmx = value;
            }
        }
        /// <summary>
        /// Descrizione Cart selezionato (ord ext code)
        /// </summary>
        public string CartDesc
        {
            get
            {
                return cmp_KS_currCart.Description;
            }
            set
            {
                cmp_KS_currCart.Description = value;
            }
        }
        public int ItemID
        {
            get
            {
                return cmp_KS_currItm.CurrId;
            }
            set
            {
                cmp_KS_currItm.CurrId = value;
                // se 0 --> annullo validità
                if (value == 0)
                {
                    ItemValid = false;
                }
            }
        }
        /// <summary>
        /// Matrix Bin selezionato
        /// </summary>
        public string ItemDtmx
        {
            get
            {
                return cmp_KS_currItm.Dtmx;
            }
            set
            {
                cmp_KS_currItm.Dtmx = value;
            }
        }
        protected bool ItemValid
        {
            get
            {
                bool answ = false;
                bool.TryParse(hfItemValid.Value, out answ);
                return answ;
            }
            set
            {
                hfItemValid.Value = value.ToString();
            }
        }
        protected bool itemOk
        {
            get
            {
                return ItemID > 0;
            }
        }
        protected bool cartOk
        {
            get
            {
                return CartID > 0;
            }
        }
        protected bool showBtns
        {
            get
            {
                return itemOk && cartOk;
            }
        }
        public bool NeedSecOp
        {
            get
            {
                bool answ = false;
                bool.TryParse(hfNeedSecOp.Value, out answ);
                return answ;
            }
            set
            {
                hfNeedSecOp.Value = value.ToString();
            }
        }
        public bool NeedPaint
        {
            get
            {
                bool answ = false;
                bool.TryParse(hfNeedPaint.Value, out answ);
                return answ;
            }
            set
            {
                hfNeedPaint.Value = value.ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                doUpdate();
            }
        }
        /// <summary>
        /// Veriofico item sia valido...
        /// </summary>
        private void checkItemValidated()
        {
            bool showConfirmItem = false;
            // se NON è già validato...
            if (!ItemValid)
            {
                showConfirmItem = NeedPaint || NeedSecOp;
                ItemValid = !showConfirmItem;
            }
            divItemValid.Visible = showConfirmItem;
            // moastro messaggi errore secondo caso
            lblNeedSecOp.Visible = NeedSecOp;
            lblNeedPaint.Visible = NeedPaint;
            fixVisibility();
        }
        private void fixVisibility()
        {
            // controllo visualizazione in base ai dati presenti
            cmp_KS_currItm.Visible = itemOk;
            cmp_KS_currCart.Visible = showBtns;
            // se ho item e cart --> mostro buttons calcolati!
            lbtAddItem.Visible = false;
            if (showBtns)
            {
                // verifico SE item e cart corrispondono x mostrare button
                var tabCarts = DLMan.taCR.getByItemID(ItemID);
                if (tabCarts != null)
                {
                    if (tabCarts.Count > 0)
                    {
                        if (tabCarts[0].CartID == CartID)
                        {
                            lbtAddItem.Visible = true;
                        }
                    }
                }
            }
        }
        public void doUpdate()
        {
            // effettua controlli vari
            checkItemValidated();
        }

        protected void lbtConfItemValid_Click(object sender, EventArgs e)
        {
            ItemValid = true;
            checkItemValidated();
        }

        protected void lbtCancelItemValid_Click(object sender, EventArgs e)
        {
            ItemValid = false;
            checkItemValidated();
        }

        protected void lbtAddItem_Click(object sender, EventArgs e)
        {
            DLMan.taPLog.acquireItems(ItemID, CartID, currIpAddress, user_std.UtSn.userNameAD);
            lgInfo($"cmp_KS_Items | taPLog.acquireItems | ItemID : {ItemID} | CartID: {CartID} | user: {user_std.UtSn.userNameAD} | IP: {currIpAddress}");
            // resetto
            CartID = 0;
            ItemID = 0;
            doUpdate();
            // sollevo evento
            raiseEvent();
        }
    }
}