//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using CommunityServer.Components;
using TWC = Telligent.Web.UI;

namespace CommunityServer.Controls
{
	public abstract class ServerPopulatedPopupMenu : TemplatedWebControl
	{
		#region Child Controls
		
		protected TWC.PopupMenu menu;

		#endregion
		
		#region Skin

		protected override void AttachChildControls() 
		{
			menu = (TWC.PopupMenu) FindControl("Menu");
		}

		#endregion

		#region AJAX Methods

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual string ProcessPopulateMenuCallback(string parameterData)
		{
			PopulateMenu(parameterData);
			return menu.GetMenuItemsScript(menu.MenuItems);
		}

		#endregion

		#region Control Event Handlers

		protected override void OnInit(System.EventArgs e)
		{
			base.OnInit(e);

			AjaxManager.Register(this,"ServerPopulatedPopupMenuAJAX");
		}

		protected override void OnLoad(EventArgs e)
		{
            this.EnsureChildControls();

			if (this.PopulateMenuViaCallback)
			{
				menu.Visible = false;
				if (this.Context.Items[this.GetType().FullName + "_Menu"] == null)
				{
					this.Context.Items.Add(this.GetType().FullName + "_Menu", menu);
					menu.Visible = true;
					menu.OnCloseClientFunction = string.Format("new Function('eval(\\'window.{0}.ContextMenuClosed()\\')')", this.ClientID);
				}
				else
					menu = (TWC.PopupMenu) this.Context.Items[this.GetType().FullName + "_Menu"];
			}
			else
			{
				menu.OnCloseClientFunction = string.Format("new Function('eval(\\'window.{0}.ContextMenuClosed()\\')')", this.ClientID);
			}

			base.OnLoad(e);
		}

		protected override void OnPreRender(EventArgs e)
		{
			if (!this.Page.IsClientScriptBlockRegistered("CommunityServer.Controls.ServerPopulatedPopupMenu"))
				this.Page.RegisterClientScriptBlock("CommunityServer.Controls.ServerPopulatedPopupMenu", string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",this.ResolveUrl("~/utility/ServerPopulatedPopupMenu.js")));

			if (!this.Page.IsStartupScriptRegistered(this.ClientID))
				Page.RegisterStartupScript(this.ClientID, String.Format("<script language=\"javascript\">window.{0} = new ServerPopulatedPopupMenu('{0}', '{1}', '{2}', '{3}', {4}, {5});</script>", this.ClientID, this.menu.ClientID, this.InactiveButtonCssClass, this.ActiveButtonCssClass, this.CacheMenuByParameterString.ToString().ToLower(), this.PopulateMenuViaCallback.ToString().ToLower()));

			if (!this.PopulateMenuViaCallback)
				this.PopulateMenu(this.ParameterString);

			base.OnPreRender(e);
		}

		#endregion

		#region Helper Methods

		protected abstract void PopulateMenu(string parameterString);

		protected string GetOpenPopupMenuScript(string parameterString)
		{
			return string.Format("window.{0}.LoadMenu(this, event, '{1}');", this.ClientID, parameterString.Replace("'", "\\'"));
		}

		protected string GetOpenPopupMenuScript()
		{
			return GetOpenPopupMenuScript(this.ParameterString);
		}

		#endregion

		#region Properties

		protected string ParameterString 
		{ 
			get 
			{
				string state = (string) this.ViewState["ParameterString"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				ViewState["ParameterString"] = value;
			}
		}

//		protected string MenuParameterJavaScript
//		{
//			get
//			{
//				object state = this.ViewState["MenuParameterJavaScript"];
//				if (state == null)
//					return "null";
//				else
//					return (string) state;
//			}
//			set
//			{
//				ViewState["MenuParameterJavaScript"] = value;
//			}
//		}

		protected bool CacheMenuByParameterString
		{
			get
			{
				object state = this.ViewState["CacheMenuByParameterString"];
				if (state == null)
					return true;
				else
					return (bool) state;
			}
			set
			{
				ViewState["CacheMenuByParameterString"] = value;
			}
		}

		protected bool PopulateMenuViaCallback
		{
			get
			{
				object state = this.ViewState["PopulateMenuViaCallback"];
				if (state == null)
					return true;
				else
					return (bool) state;
			}
			set
			{
				ViewState["PopulateMenuViaCallback"] = value;
			}
		}

		public override string ClientID
		{
			get
			{
				EnsureChildControls();
				return this.menu.ClientID + "_ServerPopulatedPopupMenu";
			}
		}

		public virtual string InactiveButtonCssClass
		{
			get
			{
				string state = (string) this.ViewState["InactiveButtonCssClass"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["InactiveButtonCssClass"] = value;
			}
		}

		public virtual string ActiveButtonCssClass
		{
			get
			{
				string state = (string) this.ViewState["ActiveButtonCssClass"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["ActiveButtonCssClass"] = value;
			}
		}

		public virtual string InnerButtonCssClass
		{
			get
			{
				string state = (string) this.ViewState["InnerButtonCssClass"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["InnerButtonCssClass"] = value;
			}
		}

		protected string Text
		{
			get
			{
				string state = (string) this.ViewState["Text"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["Text"] = value;
			}
		}

		protected string ResourceName
		{
			get
			{
				string state = (string) this.ViewState["ResourceName"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["ResourceName"] = value;
			}
		}

		protected string ResourceFileName
		{
			get
			{
				string state = (string) this.ViewState["ResourceFileName"];
				if (state == null)
					return "";
				else
					return state;
			}
			set
			{
				this.ViewState["ResourceFileName"] = value;
			}
		}

		#endregion

		#region Render Method

		protected override void Render(HtmlTextWriter writer)
		{
			writer.Write("<a href=\"#\" onclick=\"return ");
			writer.Write(this.GetOpenPopupMenuScript());
			writer.Write("\" class=\"");
			writer.Write(this.InactiveButtonCssClass);
			writer.Write("\" style=\"");
			
			foreach (String key in this.Style.Keys)
			{
				writer.Write(key);
				writer.Write(":");
				writer.Write(this.Style[key]);
				writer.Write(";");
			}

			writer.Write("\"><span class=\"");
			writer.Write(this.InnerButtonCssClass);
			writer.Write("\">");

			if (!Globals.IsNullorEmpty(this.ResourceName))
				writer.Write(ResourceManager.GetString(this.ResourceName, this.ResourceFileName));
			else
				writer.Write(this.Text);	
			
			writer.Write("</span></a>");

			base.Render(writer);
		}

		#endregion
	}
}



