using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SteamWare;
using System.Text;
using GMW_data;

namespace GMW_Term.WebUserControls
{
    public partial class mod_searchRes : System.Web.UI.UserControl
    {
        /// <summary>
        /// idxBlocco selezionato
        /// </summary>
        protected int idxBlocco
        {
            get
            {
                return memLayer.ML.IntSessionObj("IdxBlocco_sel");
            }
            set
            {
                memLayer.ML.setSessionVal("IdxBlocco_sel", value);
            }
        }
        /// <summary>
        /// udc selezionato
        /// </summary>
        protected string udc
        {
            get
            {
                return memLayer.ML.StringSessionObj("UDC4det_sel");
            }
            set
            {
                memLayer.ML.setSessionVal("UDC4det_sel", value);
            }
        }
        /// <summary>
        /// caricamento pagina
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                idxBlocco = -1;
                grViewMag.SelectedIndex = 0;
            }
            showData();
            registraScripts();
        }
        /// <summary>
        /// registro scripts gestione navigazione via arrow keys
        /// </summary>
        private void registraScripts()
        {
            // registro script x gestione keyUp/keyDown
            ClientScriptManager cs = Page.ClientScript;
            String csname = "keyScript";
            Type cstype = this.GetType();
            // Check to see if the startup script is already registered. 
            if (!cs.IsStartupScriptRegistered(cstype, csname))
            {
                string scriptKeyNav = @"function move(e) {
                var key = 0;
                if(window.event)
                    key = event.keyCode;
                else
                    key = e.keyCode;
                if(key == 38)
                    document.getElementById('{ctlName}_btnUp').click();
                if(key == 40)
                    document.getElementById('{ctlName}_btnDown').click();
                if(key == 37)
                    document.getElementById('{ctlName}_btnLeft').click();
                if(key == 39)
                    document.getElementById('{ctlName}_btnRight').click();
                if(key == 13)
                    document.getElementById('{ctlName}_btnRight').click();
                }
                document.onkeydown=move;
                ";
                // rif x codici: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
                scriptKeyNav = scriptKeyNav.Replace("{ctlName}", this.UniqueID.Replace("$", "_"));
                // registro script!
                cs.RegisterClientScriptBlock(cstype, csname, scriptKeyNav, true);
            }
        }
        /// <summary>
        /// mostra i dati corretti
        /// </summary>
        private void showData()
        {
            bool showBlocchi = true;
            bool showUdc = false;
            bool showDetUdc = false;
            // controllo se ho un idxBlocco sel
            if (idxBlocco > 0)
            {
                showBlocchi = false;
                showUdc = true;
            }
            // controllo se ho un UDC selezionato
            if (udc !="")
            {
                showBlocchi = false;
                showUdc = false;
                showDetUdc = true;
            }
            pnlBlocchi.Visible = showBlocchi;
            pnlUdc.Visible = showUdc;
            pnlDetailUdc.Visible = showDetUdc;
            // fix frecce... 
            if (!showDetUdc)
            {
                btnUp.Enabled = true;
                btnDown.Enabled = true;
                btnLeft.Enabled = true;
                btnRight.Enabled = true;
                btnUp.ImageUrl = "../Images/ArrowUp.png";
                btnDown.ImageUrl = "../Images/ArrowDown.png";
                btnLeft.ImageUrl = "../Images/ArrowLeft.png";
                btnRight.ImageUrl = "../Images/ArrowRight.png";
                showBottomBtn(false);
            }
            else // ...se showDetUdc --> mostro attivo solo ritorno!
            {
                btnUp.Enabled = false;
                btnDown.Enabled = false;
                btnLeft.Enabled = true;
                btnRight.Enabled = false;
                btnUp.ImageUrl = "../Images/ArrowUpDisab.png";
                btnDown.ImageUrl = "../Images/ArrowDownDisab.png";
                btnLeft.ImageUrl = "../Images/ArrowLeft.png";
                btnRight.ImageUrl = "../Images/ArrowRightDisab.png";
                // ...e mostro button x andare alle liste di prelievo
                showBottomBtn(true);
                btn1.Text = string.Format("{0} [Alt+{1}]", traduci("btnTermTakeListBarcode"), 1);
                btn1.AccessKey = "1";
                btn2.Text = string.Format("{0} [Alt+{1}]", traduci("btnTermSmartList"), 9);
                btn2.AccessKey = "9";
                btn3.Text = string.Format("{0} [Alt+{1}]", traduci("btnTermMenuMov"), 8);
                btn3.AccessKey = "8";
                checkLavoriInCorso();
            }
        }
        /// <summary>
        /// imposta visibilità buttons delle azioni di link
        /// </summary>
        /// <param name="doShow"></param>
        private void showBottomBtn(bool doShow)
        {
            btn1.Visible = doShow;
            btn2.Visible = doShow;
            btn3.Visible = doShow;
        }
        /// <summary>
        /// wrapper traduzione termini
        /// </summary>
        /// <param name="lemma"></param>
        /// <returns></returns>
        public string traduci(string lemma)
        {
            return user_std.UtSn.Traduci(lemma);
        }
        /// <summary>
        /// va alla pagina dei buttons principale
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnButtonsHome_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Search.aspx");
        }
        /// <summary>
        /// cambio index
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grViewMag_SelectedIndexChanged(object sender, EventArgs e)
        {
            reqDetailMag();
        }
        /// <summary>
        /// cambio index
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grViewUdc_SelectedIndexChanged(object sender, EventArgs e)
        {
            reqDetailUdc();
        }
        #region gestione frecce area MAG/Udc

        /// <summary>
        /// Mag: richiesto di salire di 1 riga
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUp_Click(object sender, ImageClickEventArgs e)
        {
            // controllo se sono in modalità Mag
            if (idxBlocco < 0)
            {
                MoveUpMag();
            }
            else // modalità UDC
            {
                MoveUpUdc();
            }
        }
        /// <summary>
        /// Mag: richiesto di scendere di 1 riga
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDown_Click(object sender, ImageClickEventArgs e)
        {
            if (idxBlocco < 0)
            {
                MoveDownMag();
            }
            else
            {
                MoveDownUdc();
            }
        }
        /// <summary>
        /// Mag: richiesto passare a dettaglio!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRight_Click(object sender, ImageClickEventArgs e)
        {
            // controllo se sono in modalità Mag
            if (idxBlocco < 0)
            {
                reqDetailMag();
            }
            else // modalità UDC
            {
                reqDetailUdc();
            }
        }
        /// <summary>
        /// Mag: richiesto passare a pag precedente
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLeft_Click(object sender, ImageClickEventArgs e)
        {
            // controllo se sono in modalità Mag
            if (idxBlocco < 0)
            {
                resetSearch();
            }
            else // modalità UDC
            {
                // controllo se c'è UDC e quindi richiesta dettaglio UDC
                if (udc != "")
                {
                    resetDetailUdc();
                }
                else
                {
                    resetUdc();
                }
            }
        }

        #endregion

        #region gestione operazioni spostamento/dettaglio su gridview

        /// <summary>
        /// sposta UP selezione blocco MAG
        /// </summary>
        private void MoveUpMag()
        {
            int i = grViewMag.SelectedIndex;
            // condizione 1: sono entro la pagina
            if (i > 0)
            {
                grViewMag.SelectedIndex = grViewMag.SelectedIndex - 1;
            }
            else
            {
                // condizione 2: ho altre pagine precedenti
                if (grViewMag.PageIndex > 0)
                {
                    grViewMag.PageIndex = grViewMag.PageIndex - 1;
                    // seleziono prima riga
                    grViewMag.SelectedIndex = grViewMag.PageSize - 1;
                }
            }
        }
        /// <summary>
        /// sposta DOWN selezione blocco MAG
        /// </summary>
        private void MoveDownMag()
        {
            int i = grViewMag.SelectedIndex;
            // condizione 1: sono entro la pagina
            if (i < grViewMag.Rows.Count - 1)
            {
                grViewMag.SelectedIndex = grViewMag.SelectedIndex + 1;
            }
            else
            {
                // condizione 2: ho altre pagine successive
                if (grViewMag.PageIndex < grViewMag.PageCount)
                {
                    grViewMag.PageIndex = grViewMag.PageIndex + 1;
                    // seleziono prima riga
                    grViewMag.SelectedIndex = 0;
                }
            }
        }
        /// <summary>
        /// richiesta dettaglio Mag
        /// </summary>
        private void reqDetailMag()
        {
            idxBlocco = Convert.ToInt32(grViewMag.SelectedDataKey["IdxBlocco"].ToString());
            grViewUdc.SelectedIndex = 0;
            grViewUdc.PageIndex = 0;
            showData();
        }
        /// <summary>
        /// reset a ricerca
        /// </summary>
        private void resetSearch()
        {
            Response.Redirect("~/Search.aspx");
        }
        /// <summary>
        /// sposta UP selezione blocco UDC
        /// </summary>
        private void MoveUpUdc()
        {
            int i = grViewUdc.SelectedIndex;
            // condizione 1: sono entro la pagina
            if (i > 0)
            {
                grViewUdc.SelectedIndex = grViewUdc.SelectedIndex - 1;
                //grViewUdc.DataBind();
            }
            else
            {
                // condizione 2: ho altre pagine precedenti
                if (grViewUdc.PageIndex > 0)
                {
                    grViewUdc.PageIndex = grViewUdc.PageIndex - 1;
                    // seleziono prima riga
                    grViewUdc.SelectedIndex = grViewUdc.PageSize - 1;
                }
            }
            grViewUdc.DataBind();
        }
        /// <summary>
        /// sposta DOWN selezione blocco UDC
        /// </summary>
        private void MoveDownUdc()
        {
            int i = grViewUdc.SelectedIndex;
            // condizione 1: sono entro la pagina
            if (i < grViewUdc.Rows.Count - 1)
            {
                grViewUdc.SelectedIndex = grViewUdc.SelectedIndex + 1;
                //grViewUdc.DataBind();
            }
            else
            {
                // condizione 2: ho altre pagine successive
                if (grViewUdc.PageIndex < grViewUdc.PageCount)
                {
                    grViewUdc.PageIndex = grViewUdc.PageIndex + 1;
                    // seleziono prima riga
                    grViewUdc.SelectedIndex = 0;
                }
            }
            grViewUdc.DataBind();
        }
        /// <summary>
        /// UDC: richiesto passare a dettaglio!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void reqDetailUdc()
        {
            udc = grViewUdc.SelectedDataKey["UDC"].ToString();
            showData();
        }
        /// <summary>
        /// reset ricerca UDC
        /// </summary>
        private void resetUdc()
        {
            idxBlocco = -1;
            grViewMag.SelectedIndex = 0;
            grViewMag.PageIndex = 0;
            grViewMag.DataBind();
            showData();
        }


        /// <summary>
        /// reset ricerca dettaglio UDC
        /// </summary>
        private void resetDetailUdc()
        {
            udc = "";
            grViewUdc.DataBind();
            showData();
        }
        /// <summary>
        /// Button 1: va a pagina UDC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn1_Click(object sender, EventArgs e)
        {
            Response.Redirect("UDC.aspx");
        }
        /// <summary>
        /// Button 2: va a smart list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn2_Click(object sender, EventArgs e)
        {
            Response.Redirect("SmartList.aspx");
        }
        /// <summary>
        /// Button 3: va a spostamenti
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn3_Click(object sender, EventArgs e)
        {
            Response.Redirect("MakeMov.aspx");
        }
        /// <summary>
        /// verifica se ci siano in corso smartList o LDP normali e disabilita il contrario
        /// </summary>
        private void checkLavoriInCorso()
        {
            bool hasSmartList = false;
            try
            {
                hasSmartList = (MagClass.magazzino.taElencoListePrelievo.getOpenByUserTipo(MagClass.magazzino.CodSoggCurrUser, "00-Smart").Rows.Count > 0);
            }
            catch
            { }
            if (hasSmartList)
            {
                // smartList attiva
                btn1.Enabled = false;
            }
            else if (memLayer.ML.isInSessionObject("CodListaAttiva"))
            {
                // LDP tradizionale attiva
                btn2.Enabled = false;
            }
            else
            {
                // niente attivo!
                btn1.Enabled = true;
                btn2.Enabled = true;
            }
        }
        #endregion
    }
}