using AppData;
using SteamWare;
using System;
using System.Web.UI;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_menuTop : BaseUserControl
    {
        public event EventHandler eh_doRefresh;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                checkAuth();
                searchVal = "";
                doSearch();
                updateTreeMenu();
            }
        }

        private void checkAuth()
        {
            // in primis SOLO SE non è permesso login anonymous...
            if (!enableAnonym)
            {
                if (!Page.User.Identity.IsAuthenticated)
                {
                    Response.Redirect("default");
                }
            }
        }

        /// <summary>
        /// imposta visibilità search globale
        /// </summary>
        public bool showSearch
        {
            get
            {
                return divSearch.Visible;
            }
            set
            {
                divSearch.Visible = value;
            }
        }
        /// <summary>
        /// Abilitazione esecuzione anonima
        /// </summary>
        public bool enableAnonym
        {
            get
            {
                bool answ = false;
                bool.TryParse(hfAnonym.Value, out answ);
                return answ;
            }
            set
            {
                hfAnonym.Value = value.ToString();
            }
        }

        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {
            doSearch();
        }

        private void doSearch()
        {
            // se searchVal !=""
            if (!string.IsNullOrEmpty(searchVal))
            {
                memLayer.ML.setSessionVal("valoreSearch", searchVal);
            }
            else
            {
                memLayer.ML.emptySessionVal("valoreSearch");
            }
            // se qualcuno ascolta sollevo evento nuovo valore...
            if (eh_doRefresh != null)
            {
                eh_doRefresh(this, new EventArgs());
            }
        }

        /// <summary>
        /// Valore ricerca attivo
        /// </summary>
        protected string searchVal
        {
            get
            {
                return txtSearch.Text.Trim();
            }
            set
            {
                txtSearch.Text = value.Trim();
            }
        }
        /// <summary>
        /// aggiornamento del menù
        /// </summary>
        private void updateTreeMenu()
        {
            // SOLO SE è permesso login anonymous --> nascondo menù!
            if (enableAnonym)
            {
                menu.Visible = false;
            }
            else
            {
                try
                {
                    if (string.IsNullOrEmpty(user_std.UtSn.mappaSito))
                    {
                        Response.Redirect("Default", true);
                    }
                    XmlMenu.Data = user_std.UtSn.mappaSito;
                    XmlMenu.XPath = "mainMenu/menu";
                    XmlMenu.DataBind();
                }
                catch
                {
                    Response.Redirect("default", true);
                }
            }
        }
        /// <summary>
        /// click su pagina corrente, fa update!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnUpdate_Click(object sender, EventArgs e)
        {
            // SOLO SE non è permesso login anonymous...
            if (!enableAnonym)
            {
                Response.Redirect("default");
            }
        }
        protected void lbtSearch_Click(object sender, EventArgs e)
        {
            doSearch();
        }
    }
}