using AppData;
using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_kitReqSched : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                fixSchedOrd();
                fixVisibility();
            }
        }

        private void fixVisibility()
        {
            cmp_KR_cart.Visible = (grView.SelectedIndex >= 0);
        }
        protected int numRows
        {
            get
            {
                int answ = 0;
                int.TryParse(hfNumRows.Value, out answ);
                return answ;
            }
            set
            {
                hfNumRows.Value = value.ToString();
            }
        }
        public bool showFull
        {
            get
            {
                bool answ = false;
                bool.TryParse(hfShowFull.Value, out answ);
                return answ;
            }
            set
            {
                hfShowFull.Value = value.ToString();
                grView.DataBind();
            }
        }

        internal void doUpdate()
        {
            fixSchedOrd();
            grView.DataBind();
            fixVisibility();
            cmp_KR_cart.doUpdate();
            lblTitle.Focus();
        }

        private void fixSchedOrd()
        {
            numRows = DLMan.taPL.getToSchedule().Count * 10;
            DLMan.taPL.fixSched();
        }


        protected void lbtUp_Click(object sender, EventArgs e)
        {
            int PackListId = 0;
            LinkButton lbtBtn = (LinkButton)sender;
            if (lbtBtn != null)
            {
                int.TryParse(lbtBtn.CommandArgument, out PackListId);
                moveBatch(PackListId, -15);
            }
        }
        protected void lbtDown_Click(object sender, EventArgs e)
        {
            int PackListId = 0;
            LinkButton lbtBtn = (LinkButton)sender;
            if (lbtBtn != null)
            {
                int.TryParse(lbtBtn.CommandArgument, out PackListId);
                moveBatch(PackListId, 15);
            }
        }


        protected void lbtStartJob_Click(object sender, EventArgs e)
        {
            int PackListId = 0;
            LinkButton lbtBtn = (LinkButton)sender;
            if (lbtBtn != null)
            {
                int.TryParse(lbtBtn.CommandArgument, out PackListId);
                // avvio il job selezionato e indico status running
                DLMan.taPL.updateSchedPrior(PackListId, -10);
                DLMan.taPL.updateStatus(PackListId, 3);
                // fix grafico!
                doRefresh();
                raiseEvent();
            }
        }

        private void moveBatch(int PackListId, int step)
        {
            // aggiorno
            DLMan.taPL.updateSchedPrior(PackListId, step);
            doRefresh();
        }

        private void doRefresh()
        {
            // ricalcolo
            fixSchedOrd();
            grView.SelectedIndex = -1;
            grView.DataBind();
            lblTitle.Focus();
        }

        /// <summary>
        /// Verifica visibilità da posizione
        /// </summary>
        /// <param name="mode">first/last</param>
        /// <param name="currPrior">num priorità attuale</param>
        /// <returns></returns>
        public bool checkVisible(string mode, object currPrior)
        {
            bool answ = true;
            int numPrior = 0;
            int.TryParse(currPrior.ToString(), out numPrior);
            if (mode == "first")
            {
                answ = (numPrior != 10);
            }
            else
            {
                answ = (numPrior != numRows);
            }
            return answ;
        }

        /// <summary>
        /// Verifica visibilità da posizione
        /// </summary>
        /// <param name="mode">first/last</param>
        /// <param name="currPrior">num priorità attuale</param>
        /// <returns></returns>
        public bool checkStartEnabled(string mode, object currPrior)
        {
            bool answ = false;
            // start enabled: ha 2 condizioni: (1) che sia FALSO checkVisible
            if (!checkVisible(mode, currPrior))
            {
                // (2) che running siano zero,
                answ = DLMan.taPL.getRunning().Count == 0;
            }

            return answ;
        }

        protected void grView_SelectedIndexChanged(object sender, EventArgs e)
        {
            int PackListId = 0;
            int.TryParse(grView.SelectedValue.ToString(), out PackListId);
            // mostra dettaglio CART
            cmp_KR_cart.PackListID = PackListId;
            cmp_KR_cart.Visible = true;
        }
        /// <summary>
        /// comando reset
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtReset_Click(object sender, EventArgs e)
        {
            grView.SelectedIndex = -1;
            doUpdate();
        }
    }
}