using System;
using System.Collections;
using System.Configuration;
using System.Text;
using System.Web.UI;
using System.Web;
using B4Restore.Common.Business;
using B4Restore.Common.Web;
using DebriefingSoftware.Common;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace B4Restore.Common.Control.BaseDS
{
    public abstract class DSBasePage : Page
    {
        private readonly BaseTabHelper baseTabHelper = new BaseTabHelper();
        private bool _isLeftMenuVisible = true;
        private readonly WorkWithControl workWithControl = new WorkWithControl();
        public bool HideServerDropDown { get; set; }
        public WorkWithControl WorkWithControlInstance
        {
            get { return workWithControl; }
        }

        public virtual string TabID
        {
            get { return baseTabHelper.TabID; }
        }

        public virtual bool IsDetailView
        {
            get { return false; }
        }

        public bool IsLeftMenuVisible
        {
            get
            {
                return _isLeftMenuVisible;
            }
            set
            {
                _isLeftMenuVisible = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            //LoginController.CurrentLogin.MultiRoles.Contains()
            base.OnInit(e);

            QueryParams.Clear();
            NavigateParams.Clear();
        }

        protected override void OnPreRender(EventArgs e)
        {
            //if (this._isLeftMenuVisible)
            //{
            var script = "function jsIsLeftMenuVisible(){ return " + this._isLeftMenuVisible.ToString().ToLower() + ";}";
            ScriptManager.RegisterClientScriptBlock(
              this,
              GetType(), "jsIsLeftMenuVisibleScript", script,
              true);
            // };

            var script2 = "function jsIsDetailView(){ return " + this.IsDetailView.ToString().ToLower() + ";}";
            ScriptManager.RegisterClientScriptBlock(
              this,
              GetType(), "jsIsDetailViewScript", script2,
              true);


            if (!NavigationHistory.Current.Equals(Request.Url.PathAndQuery, StringComparison.OrdinalIgnoreCase)
                && !Request.Path.EndsWith("Default.aspx"))
            {
                //string sTitle = this.Title;
                var sTitle = string.Empty; // for now to sync with javascript code
                if (string.IsNullOrEmpty(sTitle))
                {
                    sTitle = Page.ToString().ToLower(); //	"ASP.pages_node_aspx"	string
                    sTitle = sTitle.Substring(10, sTitle.Length - 15);
                    var sUrl = Request.Url.PathAndQuery;
                    var iTabid = sUrl.IndexOf(UrlConst.TAB_ID);
                    var sTab = string.Empty;
                    if (iTabid > 0)
                    {
                        sTab = sUrl.Substring(iTabid + 6);
                        var iAnd = sTab.IndexOf('&');
                        if (iAnd > 0)
                            sTab = sTab.Substring(0, iAnd);
                    }
                    if (!string.IsNullOrEmpty(sTab))
                    {
                        sTitle += "-" + sTab;
                    }
                    var fstLetter = sTitle.Substring(0, 1);
                    sTitle = fstLetter.ToUpper() + sTitle.Substring(1);
                }
                NavigationHistory.Add(Request.Url.PathAndQuery, sTitle);

            }

            var script3 = "function jsGetNavigationHistoryPath(){ return '" + NavigationHistory.PathsString + "';}";
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "jsGetNavigationHistoryPathScript", script3, true);
            var script4 = "function jsGetNavigationHistoryTitle(){ return '" + NavigationHistory.TitlesString + "';}";
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "jsGetNavigationHistoryTitleScript", script4, true);
            base.OnPreRender(e);

        }

		public abstract string TabIdentifier { get; }

		protected override void CreateChildControls()
		{
			// Find the form control in which all asp.net (or custom) controls must be hosted.
			foreach (var c in this.Controls)
			{
				if (c is System.Web.UI.HtmlControls.HtmlForm)
				{
					ProcessForm(c as HtmlForm);
				}
			}
			base.CreateChildControls();
		}

		private static void ProcessForm(HtmlForm form)
		{
			// Find all the web controls, ie <asp:control_name />, and check process them.
			// You can also add logic here to check for user controls or custom user controls.
			foreach (var cc in form.GetControlListOnCreateNew<System.Web.UI.Control>(typeof(System.Web.UI.Control)))
			{
				if (cc is WebControl)
				{
					// Once you find the a web control, process any custom attributes you have specified.
					DSAttributeParser.ProcessControl(cc as WebControl);
				}
			}
		}
    }
}