using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.Drawing.Design;

namespace B4Restore.Common.Control
{
	[ToolboxData("<{0}:ActionButton runat=server></{0}:ActionButton>")]
	public class ActionButton : LinkButton
	{
		protected readonly Image _img = new Image();
		protected readonly LinkButton _cmd = new LinkButton();

		public ActionButton()
			: base()
		{
			SetupDefaults();
		}

		[Localizable(true), Bindable(true), Category("Appearance"), DefaultValue(""), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true)]
		public virtual string AlternateText
		{
			get
			{
				var text = this.ViewState["AlternateText"] as String;
				return text ?? string.Empty;
			}
			set { this.ViewState["AlternateText"] = value; }
		}

		[DefaultValue(""), Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), UrlProperty, Bindable(true), Category("Appearance")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true)]
		public virtual string ImageUrl
		{
			get
			{
				var text = this.ViewState["ImageUrl"] as String;
				return text ?? string.Empty;
			}
			set { this.ViewState["ImageUrl"] = value; }
		}

		[Bindable(true), Category("Appearance"), DefaultValue(""), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true)]
		public virtual string ImageCssClass
		{
			get
			{
				var text = this.ViewState["ImageCssClass"] as String;
				return text ?? string.Empty;
			}
			set { this.ViewState["ImageCssClass"] = value; }
		}

		[Localizable(true), PersistenceMode(PersistenceMode.InnerDefaultProperty), Bindable(true), Category("Appearance"), DefaultValue(""), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true)]
		public override string Text
		{
			get
			{
				var text = this.ViewState["Text"] as String;
				return text ?? string.Empty;
			}
			set
			{
				// original implementation
				//if (this.HasControls())
				//{
				//    this.Controls.Clear();
				//}
				this.ViewState["Text"] = value;
			}

		}

		[DefaultValue(""), UrlProperty("*.aspx"), Editor("System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Themeable(false), Category("Behavior")]
		public virtual string Href
		{
			get
			{
				var text = this.ViewState["Href"] as String;
				return text ?? string.Empty;
			}
			set { this.ViewState["Href"] = value; }
		}

		protected virtual void SetupDefaults()
		{
			this.CausesValidation = true;
			this.CssClass = "CommandButton";

			this.ImageCssClass = "ma actionImgTextButton";
		}

		protected virtual string ImageUrlDefault { get { return String.Empty; } }

		//protected virtual string GetImageUrl()
		//{
		//    return ImageFileName;
		//    if (Page != null)
		//        return Page.Server.MapPath(ImageFileName);
		//    else
		//        return ResolveUrl(ImageFileName);
		//}

		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			if (!String.IsNullOrEmpty(this.Href))
			{
				if (this.Page != null)
				{
					this.Page.VerifyRenderingInServerForm(this);
				}
				string str = ActionButton.EnsureEndWithSemiColon(this.OnClientClick);
				if (base.HasAttributes)
				{
					string str2 = base.Attributes["onclick"];
					if (str2 != null)
					{
						str = str + ActionButton.EnsureEndWithSemiColon(str2);
						base.Attributes.Remove("onclick");
					}
				}
				if (str.Length > 0)
				{
					writer.AddAttribute(HtmlTextWriterAttribute.Onclick, str);
				}
				bool isEnabled = base.IsEnabled;
				if (this.Enabled && !isEnabled)
				{
					writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
				}

				if (isEnabled && (this.Page != null))
				{
					writer.AddAttribute(HtmlTextWriterAttribute.Href, this.Href);
				}
			}
			else
				base.AddAttributesToRender(writer);
		}

		protected override void CreateChildControls()
		{
			if (!Visible)
				return;

			Controls.Clear();

			this._img.ID = String.Format("{0}${1}", this.ID, "imgActionButton");
			this.Controls.Add(_img);
		}

		protected override void RenderChildren(HtmlTextWriter writer)
		{
			if (String.IsNullOrEmpty(this.Text))
			{
				foreach (System.Web.UI.Control ctrl in Controls)
					ctrl.RenderControl(writer);
			}
			else
				writer.Write(this.Text);
		}

		protected override void OnPreRender(EventArgs e)
		{
			this.ImageUrl = ImageUrlDefault;

			base.OnPreRender(e);

			this._img.AlternateText = this.AlternateText;
			this._img.ToolTip = this.ToolTip;
			this._img.CssClass = this.ImageCssClass;
			this._img.ImageUrl = this.ImageUrl;

			if (!String.IsNullOrEmpty(this.CommandName))
				this.Href = String.Empty;
		}

		public override void RenderControl(HtmlTextWriter writer)
		{
			writer.WriteLine();
			var text = this.Text; this.Text = null;
			base.RenderControl(writer);
			writer.WriteLine();

			this.Text = text;

			if (!String.IsNullOrEmpty(this.Text))
			{
				//var id = this.ID; this.ID = String.Format("{0}$0", id);
				base.RenderControl(writer);
				//this.ID = id;

				writer.Write("&nbsp;");
			}
		}

		internal static string EnsureEndWithSemiColon(string value)
		{
			if (value != null)
			{
				int length = value.Length;
				if ((length > 0) && (value[length - 1] != ';'))
				{
					return (value + ";");
				}
			}
			return value;
		}


	}
}
