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

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Controls
{
	/// <summary>
	/// Summary description for Hyperlink.
	/// </summary>
	public class Hyperlink : HyperLink
	{

		/// <summary>
		/// Gets or sets the Text of the url from the resources file 
		/// (trys ControlPanleResources.xml first)
		/// </summary>
		[
			Bindable(true),
				Category( "Appearance" ),
				Description( "Gets or sets the text of the url from the ResourceFile." ),
				DefaultValue( "" ),
		]
		public String ResourceName
		{
			get 
			{
				Object state = ViewState["ResourceName"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "";
			}
			set 
			{
				ViewState["ResourceName"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the url from the named SiteURL 
		/// </summary>
		[
			Bindable(true),
				Category( "Appearance" ),
				Description( "Gets or sets the url from the specified siteUrl key." ),
				DefaultValue( "" ),
		]
		public String UrlName
		{
			get 
			{
				Object state = ViewState["UrlName"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "";
			}
			set 
			{
				ViewState["UrlName"] = value;
			}
		}

		/// <summary>
		/// Parameters to add to the URL
		/// </summary>
		[
			Bindable(true),
				Category( "Appearance" ),
				Description( "Parameters to add to the URL." ),
				DefaultValue( "" ),
		]
		public String UrlParameters
		{
			get 
			{
				Object state = ViewState["UrlParameters"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "";
			}
			set 
			{
				ViewState["UrlParameters"] = value;
			}
		}
		
		public bool HasParameters
		{
			get{return (ViewState["UrlParameters"] != null);}
		}

		public bool HasText
		{
			get{return (!Globals.IsNullorEmpty(this.Text) );}
		}

		public bool HasUrl
		{
			get{return (!Globals.IsNullorEmpty(this.NavigateUrl) );}
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			if(!this.HasText)
				this.Text = ResourceManager.GetString(ResourceName);
			
			if(!this.HasUrl)
			{
				string url = "";
				if(!this.HasParameters)
					url = FormatUrl(UrlName);
				else
					url = FormatUrl(UrlName,UrlParameters);
				
				this.NavigateUrl = ResolveUrl(url);
			}
			base.Render (writer);
		}



		/// <summary>
		/// Property parameters (string[])
		/// </summary>
		public string[] Parameters
		{
			get {  return UrlParameters.Split(','); }
			set {this.UrlParameters = String.Join(",",value);}
		}

		protected string FormatUrl(string name)
		{
			return Globals.GetSiteUrls().UrlData.FormatUrl(name);
		}

		protected string FormatUrl(string name, params object[] parameters)
		{
			return Globals.GetSiteUrls().UrlData.FormatUrl(name, parameters);
		}

	}
}
