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

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

namespace CommunityServer.Controls
{

	public enum ModalType
	{
		Button,
		Link,
		Image
	}

	public class Modal : Control, IModalButton
	{
		#region Properties

		private int _width;
		private int _height;
		private string _url;
		private string _callback;
		private string _text;
		private string _resourceFile;
		private string _resourceName;
		private string _image;
		private string _cssClass;

		public int Width
		{
			get{return _width;}
			set{_width = value;}
		}

		public int Height
		{
			get{return _height;}
			set{_height = value;}
		}

		public string Url
		{
			get{return _url;}
			set{_url = value;}
		}

		public string Text
		{
			get{return _text;}
			set{_text = value;}
		}

		public virtual string ResourceFile 
		{
			get{return _resourceFile;}
			set{_resourceFile = value;}
		}
		public string ResourceName 
		{
			get{return _resourceName;}
			set{_resourceName = value;}
		}
		public string Image
		{
			get{return _image;}
			set{_image = value;}
		}

		public string Callback
		{
			get
			{
				if(Globals.IsNullorEmpty(_callback))
					return "null";

				return _callback;
			}
			set{_callback = value;}
		}

		public string CssClass
		{
			get{return _cssClass;}
			set{_cssClass = value;}
		}

		private ModalType _mt = ModalType.Button;

		public ModalType ModalType
		{
			get{return _mt;}
			set {_mt = value;}
		}
	
		#endregion

        public static void ClosePage(Page page, string returnValue)
        {
            page.RegisterStartupScript("closepage",string.Format("<script language=\"javascript\">{0}</script>", Telligent.Web.UI.Modal.GetCloseScript(page, returnValue)));
        }

        public static void ClosePage(Page page)
        {
            page.RegisterStartupScript("closepage",string.Format("<script language=\"javascript\">{0}</script>", Telligent.Web.UI.Modal.GetCloseScript(page)));
        }

		protected override void OnPreRender(EventArgs e)
		{
			IModalButton mb = null;
			if(this.ModalType == ModalType.Button) 
				mb = new ModalButton();
			else if(this.ModalType == ModalType.Link)
				mb = new ModalLink();
			else if(this.ModalType == ModalType.Image)
				mb = new ModalImage();
			
			if (mb != null)
			{
				mb.Text = this.Text;
				mb.ResourceFile = this.ResourceFile;
				mb.ResourceName = this.ResourceName;
				mb.Image = this.Image;
				mb.Height = this.Height;
				mb.Width = this.Width;
				mb.Callback = this.Callback;
				mb.Url = this.Url;
				mb.CssClass = this.CssClass;
				this.Controls.Add((Control)mb);
			}

			base.OnPreRender (e);
		}
	}

    public class ModalHelper
    {
        private ModalHelper(){}

		public static string BuildHandler(Page page, string url, int width, int height, string callback, bool returnFalse)
		{
			if (page == null)
				page = System.Web.HttpContext.Current.Handler as Page;

			if (returnFalse)
				return Telligent.Web.UI.Modal.GetOpenScript(page, page.ResolveUrl(url), width, height, callback) + " return false;";
			else
				return Telligent.Web.UI.Modal.GetOpenScript(page, page.ResolveUrl(url), width, height, callback);
		}

		public static string BuildHandler(Page page, string url, int width, int height, string callback)
		{
			return BuildHandler(page, url, width, height, callback, true);
		}

		public static string BuildHandler(Page page, IModalButton modal)
		{
			return BuildHandler(page, modal.Url, modal.Width, modal.Height, modal.Callback, true);
		}

		public static string BuildHandler(Page page, IModalButton modal, bool returnFalse)
		{
			return BuildHandler(page, modal.Url, modal.Width, modal.Height, modal.Callback, returnFalse);
		}
    }


	#region ModalButtonControl
	public class ModalButton : System.Web.UI.HtmlControls.HtmlButton, IModalButton
	{
		protected override void Render(HtmlTextWriter writer)
		{
			
			this.Attributes.Add("onclick", ModalHelper.BuildHandler(this.Page, this, true));

			if(!Globals.IsNullorEmpty(this.CssClass))
				this.Attributes.Add("class", this.CssClass);

			if (!Globals.IsNullorEmpty(ResourceName))
				this.InnerText = ResourceManager.GetString( ResourceName, ResourceFile );
			else if(!Globals.IsNullorEmpty(Text))
				this.InnerText = Text;

			base.Render(writer);

			
		}

		#region Properties
		private int _width;
		private int _height;
		private string _url;
		private string _returnfunction;
		private string _text;
		private string _resourceFile;
		private string _resourceName;
		private string _image;
		private string _cssClass;

		public int Width
		{
			get{return _width;}
			set{_width = value;}
		}

		public int Height
		{
			get{return _height;}
			set{_height = value;}
		}

		public string Url
		{
			get{return _url;}
			set{_url = value;}
		}

		public string Text
		{
			get{return _text;}
			set{_text = value;}
		}
		public virtual string ResourceFile 
		{
			get{return _resourceFile;}
			set{_resourceFile = value;}
		}
		public string ResourceName 
		{
			get{return _resourceName;}
			set{_resourceName = value;}
		}
		public string Image
		{
			get{return _image;}
			set{_image = value;}
		}

		public string Callback
		{
			get
			{
				if(Globals.IsNullorEmpty(_returnfunction))
					return "null";

				return _returnfunction;
			}
			set{_returnfunction = value;}
		}

		public string CssClass
		{
			get{return _cssClass;}
			set{_cssClass = value;}
		}
		#endregion

	}

	#endregion

	#region ModalLinkControl
	public class ModalLink : System.Web.UI.HtmlControls.HtmlAnchor, IModalButton
	{
		protected override void Render(HtmlTextWriter writer)
		{
			this.HRef = "javascript:" + ModalHelper.BuildHandler(this.Page, this, false);

			if(!Globals.IsNullorEmpty(this.CssClass))
				this.Attributes.Add("class", this.CssClass);

            if(this.Page is CommunityServer.Components.CSConfirmationPage && ((CommunityServer.Components.CSConfirmationPage) this.Page).IsInEdit)
                this.Attributes.Add("onclick",CSConfirmationPage.ByPassFuncationName);

			if (!Globals.IsNullorEmpty(ResourceName))
				this.InnerText = ResourceManager.GetString( ResourceName, ResourceFile );
			else if(!Globals.IsNullorEmpty(Text))
				this.InnerText = Text;

			base.Render (writer);
		}

		#region Properties
		private int _width;
		private int _height;
		private string _url;
		private string _returnfunction;
		private string _text;
		private string _resourceFile;
		private string _resourceName;
		private string _image;
		private string _cssClass;

		public int Width
		{
			get{return _width;}
			set{_width = value;}
		}

		public int Height
		{
			get{return _height;}
			set{_height = value;}
		}

		public string Url
		{
			get{return _url;}
			set{_url = value;}
		}

		public string Text
		{
			get{return _text;}
			set{_text = value;}
		}

		public virtual string ResourceFile 
		{
			get{return _resourceFile;}
			set{_resourceFile = value;}
		}
		public string ResourceName 
		{
			get{return _resourceName;}
			set{_resourceName = value;}
		}
		public string Image
		{
			get{return _image;}
			set{_image = value;}
		}

		public string CssClass
		{
			get{return _cssClass;}
			set{_cssClass = value;}
		}

		public string Callback
		{
			get
			{
				if(Globals.IsNullorEmpty(_returnfunction))
					return "null";

				return _returnfunction;
			}
			set{_returnfunction = value;}
		}
		#endregion

	}
	#endregion

	#region ModalImageControl
	/// <summary>
	/// in this class the Text property defines the image title (hover test) property
	/// </summary>
	public class ModalImage : System.Web.UI.HtmlControls.HtmlInputImage, IModalButton
	{
		protected override void Render(HtmlTextWriter writer)
		{
			this.Attributes.Add("onclick", ModalHelper.BuildHandler(this.Page, this, true));

			if(!Globals.IsNullorEmpty(this.CssClass))
				this.Attributes.Add("class", this.CssClass);

			if(!Globals.IsNullorEmpty(Image))
				this.Src = Image;

			if (!Globals.IsNullorEmpty(ResourceName))
				this.Attributes["Title"] = ResourceManager.GetString( ResourceName, ResourceFile );
			else if(!Globals.IsNullorEmpty(Text))
				this.Attributes["Title"] = Text;

			base.Render(writer);
		}

		#region Properties
		private int _width;
		private int _height;
		private string _text;
		private string _resourceFile;
		private string _resourceName;
		private string _url;
		private string _image;
		private string _returnfunction;
		private string _cssClass;

		public int Width
		{
			get{return _width;}
			set{_width = value;}
		}

		public int Height
		{
			get{return _height;}
			set{_height = value;}
		}

		public string Url
		{
			get{return _url;}
			set{_url = value;}
		}

		public string Text
		{
			get {return _text;}
			set {_text=value;}
		}

		public virtual string ResourceFile 
		{
			get{return _resourceFile;}
			set{_resourceFile = value;}
		}
		public string ResourceName
		{
			get{return _resourceName;}
			set{_resourceName = value;}
		}
		public string Image
		{
			get{return _image;}
			set{_image = value;}
		}

		public string Callback
		{
			get
			{
				if(Globals.IsNullorEmpty(_returnfunction))
					return "null";

				return _returnfunction;
			}
			set{_returnfunction = value;}
		}

		public string CssClass
		{
			get{return _cssClass;}
			set{_cssClass = value;}
		}
		#endregion

	}
	#endregion

	#region IModalButton
	public interface IModalButton
	{
		int Width { get; set; }

		int Height { get; set; }

		string Url { get; set; }

		string Text { get; set; }
		string ResourceFile { get; set; }
		string ResourceName { get; set; }
		string Image { get; set; }

		string Callback { get; set; }

		string CssClass { get; set; }
	}
	#endregion
}
