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_OtherItemsCart : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                fixVisibility();
            }
        }

        protected bool showBtns
        {
            get
            {
                return otherItemOk && cartOk;
            }
        }
        protected bool otherItemOk
        {
            get
            {
                return OtherItemID > 0;
            }
        }
        protected bool cartOk
        {
            get
            {
                return CartID > 0;
            }
        }

        public int numMax
        {
            get
            {
                int answ = 0;
                int.TryParse(hfNumAll.Value, out answ);
                return answ;
            }
            set
            {
                hfNumAll.Value = value.ToString();
            }
        }

        private void fixVisibility()
        {
            // controllo visualizazione in base ai dati presenti
            cmp_KS_currOI.Visible = otherItemOk;
            cmp_KS_currCart.Visible = showBtns;
            if (showBtns)
            {
                cmp_KS_OkoiOI.Visible = false;
            }
            else
            {
                cmp_KS_OkoiOI.Visible = otherItemOk;
            }
            // se ho bin e cart --> mostro buttons calcolati!
            lbtAddAll.Visible = false;
            if (showBtns)
            {
                var tabOKOI = DLMan.taOKOI_Sum.getByKey(OtherItemID, CartID);
                if (tabOKOI.Count > 0)
                {
                    numMax = tabOKOI[0].NumItems - tabOKOI[0].NumPicked;
                    lbtAddAll.Visible = true;
                }
            }
        }

        public void doUpdate()
        {
            fixVisibility();
            if (otherItemOk && !cartOk)
            {
                cmp_KS_OkoiOI.doUpdate();
            }
        }

        /// <summary>
        /// Cart selezionato
        /// </summary>
        public int CartID
        {
            get
            {
                return cmp_KS_currCart.CurrId;
            }
            set
            {
                cmp_KS_currCart.CurrId = value;
            }
        }
        /// <summary>
        /// Matrix  Cart 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;
            }
        }
        /// <summary>
        /// OtherItem selezionato
        /// </summary>
        public int OtherItemID
        {
            get
            {
                return cmp_KS_currOI.CurrId;
            }
            set
            {
                cmp_KS_currOI.CurrId = value;
            }
        }
        /// <summary>
        /// Matrix OtherItem selezionato
        /// </summary>
        public string OtherItemDtmx
        {
            get
            {
                return cmp_KS_currOI.Dtmx;
            }
            set
            {
                cmp_KS_currOI.Dtmx = value;
                cmp_KS_OkoiOI.Dtmx = value;
            }
        }
        /// <summary>
        /// Descrizione OtherItem selezionato
        /// </summary>
        public string OtherItemDesc
        {
            get
            {
                return cmp_KS_currOI.Description;
            }
            set
            {
                cmp_KS_currOI.Description = value;
            }
        }

        protected void lbtAddAll_Click(object sender, EventArgs e)
        {
            DLMan.taPLog.acquireOtherItems(OtherItemID, CartID, numMax, currIpAddress, user_std.UtSn.userNameAD);
            lgInfo($"cmp_KS_OtherItemsCart | taPLog.acquireOtherItems | OtherItemID : {OtherItemID} | CartID: {CartID} | numMax: {numMax} | user: {user_std.UtSn.userNameAD} | IP: {currIpAddress}");
            // resetto
            CartID = 0;
            OtherItemID = 0;
            // sollevo evento
            raiseEvent();
        }
    }
}