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

using System;
using System.Web.UI;
using CommunityServer.Components;
using System.IO;
using System.Text;
using System.Web.UI.WebControls;

namespace CommunityServer.Controls
{
	[ParseChildren(true)]
	public class CollapsableArea : Control
	{
		#region Properties

		private Control _header, _content;
		public virtual Control Header
		{
			get
			{
				return _header;
			}
			set
			{
				_header = value;
				this.Controls.Add(_header);
			}
		}

		public virtual Control Content
		{
			get
			{
				return _content;
			}
			set
			{
				_content = value;
				this.Controls.Add(_content);
			}
		}	

		public virtual bool IsExpanded
		{
			get
			{
				object state = ViewState["IsExpanded"];
				if (state == null)
					return true;
				else
					return (bool) state;
			}
			set
			{
				ViewState["IsExpanded"] = value;
			}
		}

		public virtual bool EnableHeaderDblClickToggle
		{
			get
			{
				object state = ViewState["EnableHeaderDblClickToggle"];
				if (state == null)
					return true;
				else
					return (bool) state;
			}
			set
			{
				ViewState["EnableHeaderDblClickToggle"] = value;
			}
		}

		public virtual string Parameter
		{
			get
			{
				object state = ViewState["Parameter"];
				if (state == null)
					return String.Empty;
				else
					return (string) state;
			}
			set
			{
				ViewState["Parameter"] = value;
			}
		}

		public virtual bool EnableCollapseExpand
		{
			get
			{
				object state = ViewState["EnableCollapseExpand"];
				if (state == null)
					return true;
				else
					return (bool) state;
			}
			set
			{
				ViewState["EnableCollapseExpand"] = value;
			}
		}

		HyperLink _toggleLink = null;
		public virtual HyperLink ToggleLink
		{
			get
			{
				return _toggleLink;
			}
			set
			{
				_toggleLink = value;
			}
		}	

		public virtual string ToggleCollapsedCssClass
		{
			get
			{
				object state = ViewState["ToggleCollapsedCssClass"];
				if (state == null)
					return String.Empty;
				else
					return (string) state;
			}
			set
			{
				ViewState["ToggleCollapsedCssClass"] = value;
			}
		}

		public virtual string ToggleExpandedCssClass
		{
			get
			{
				object state = ViewState["ToggleExpandedCssClass"];
				if (state == null)
					return String.Empty;
				else
					return (string) state;
			}
			set
			{
				ViewState["ToggleExpandedCssClass"] = value;
			}
		}

		public virtual string HeaderExpandedCssClass
		{
			get
			{
				object state = ViewState["HeaderExpandedCssClass"];
				if (state == null)
					return String.Empty;
				else
					return (string) state;
			}
			set
			{
				ViewState["HeaderExpandedCssClass"] = value;
			}
		}

		public virtual string HeaderCollapsedCssClass
		{
			get
			{
				object state = ViewState["HeaderCollapsedCssClass"];
				if (state == null)
					return String.Empty;
				else
					return (string) state;
			}
			set
			{
				ViewState["HeaderCollapsedCssClass"] = value;
			}
		}

		public virtual string HeaderProcessingCssClass
		{
			get
			{
				object state = ViewState["HeaderProcessingCssClass"];
				if (state == null)
					return String.Empty;
				else
					return (string) state;
			}
			set
			{
				ViewState["HeaderProcessingCssClass"] = value;
			}
		}

		public virtual bool EnableHeaderProcessingAnimation
		{
			get
			{
				object state = ViewState["EnableHeaderProcessingAnimation"];
				if (state == null)
					return true;
				else
					return (bool) state;
			}
			set
			{
				ViewState["EnableHeaderProcessingAnimation"] = value;
			}
		}

		#endregion

		#region Control Event Handlers

		protected override void OnInit(EventArgs e) 
		{
			AjaxManager.Register(this,"CollapsableAreaAJAX");

			base.OnInit(e);
		}

		protected override void OnPreRender(System.EventArgs e)
		{
			if (ToggleLink != null)
			{
				if (this.EnableCollapseExpand)
				{
					ToggleLink.NavigateUrl = "#";
					ToggleLink.Attributes.Add("onclick", String.Format("if ({0}.IsExpanded) {{ {0}.Collapse(); this.className='{1}'; }} else {{ {0}.Expand(); this.className='{2}' }}; return false;", this.ClientID, this.ToggleCollapsedCssClass, this.ToggleExpandedCssClass));

					if (this.IsExpanded)
						ToggleLink.CssClass = this.ToggleExpandedCssClass;
					else
						ToggleLink.CssClass = this.ToggleCollapsedCssClass;
				}
				else
					ToggleLink.Visible = false;
			}

			if (this.EnableCollapseExpand)
			{
				if (!this.Page.IsClientScriptBlockRegistered("CommunityServer.Controls.CollapsableArea"))
					this.Page.RegisterClientScriptBlock("CommunityServer.Controls.CollapsableArea", string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",this.ResolveUrl("~/utility/CollapsableArea.js")));

				if (!this.Page.IsStartupScriptRegistered(this.ClientID))
					Page.RegisterStartupScript(this.ClientID, String.Format("<script language=\"javascript\">window.{0}=new CollapsableArea('{0}','{1}',{2},{3},{4},{5},'{6}','{7}','{8}',{9});</script>", this.ClientID, this.Parameter.Replace("'", "\\'"), this.IsExpanded ? "1" : "0", this.EnableHeaderDblClickToggle ? "1" : "0", this.Collapse != null ? "1" : "0", this.Expand != null ? "1" : "0", this.HeaderExpandedCssClass, this.HeaderCollapsedCssClass, this.HeaderProcessingCssClass, this.EnableHeaderProcessingAnimation ? "1" : "0"));
			}

			base.OnPreRender(e);
		}

		#endregion

		#region Render Methods

		protected override void Render(HtmlTextWriter writer)
		{
			writer.Write("<div id=\"");
			writer.Write(this.ClientID);
			writer.Write("_Header\" class=\"");

			if (this.EnableCollapseExpand && !this.IsExpanded)
				writer.Write(this.HeaderCollapsedCssClass);
			else
				writer.Write(this.HeaderExpandedCssClass);
			
			writer.Write("\">");
			
			if (this.Header != null)
				this.Header.RenderControl(writer);

			writer.Write("</div><div id=\"");
			writer.Write(this.ClientID);
			writer.Write("_Content\"");

			if (this.EnableCollapseExpand && !this.IsExpanded)
				writer.Write(" style=\"display:none\"");

			writer.Write(">");

			if (this.Content != null && this.IsExpanded)
				this.Content.RenderControl(writer);

			writer.Write("</div>");
		}

		#endregion

		#region AJAX Methods

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual void Collapsed(string parameter)
		{
			this.Parameter = parameter;
			OnCollapse(new System.EventArgs());
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual string Expanded(string parameter, bool isLoaded)
		{
			this.Parameter = parameter;

			OnExpand(new System.EventArgs());

			if (!isLoaded && this.Content != null)
			{
				StringWriter stringWriter = new StringWriter();
				HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
				Content.RenderControl(htmlWriter);

				return stringWriter.ToString();
			}
			else
				return "";
		}

		#endregion

		#region Events

		public event EventHandler Collapse;
		private void OnCollapse(EventArgs e)
		{
			if(Collapse != null)
				Collapse(this, e);
		}

		public event EventHandler Expand;
		private void OnExpand(EventArgs e)
		{
			if(Expand != null)
				Expand(this, e);
		}

		#endregion
	}
}
