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

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


namespace CommunityServer.Controls {

	// *********************************************************************
	//  StyleSkin
	//
	/// <summary>
	/// Encapsulated rendering of style based on the selected skin.
	/// </summary>
	// ********************************************************************/ 
	public class Style : LiteralControl {

		/// <summary>
		/// Property Media (string)
		/// </summary>
		[
		System.ComponentModel.DefaultValue( "screen" ),
		]
		public virtual String Media {
			get {
				Object state = ViewState["Media"];
				if ( state != null ) {
					return (String)state;
				}
				return "screen";
			}
			set {
				ViewState["Media"] = value;
			}
		}

		string appliation = string.Empty;
		public string Application {
			get {
				return appliation;
			}
			set {
				appliation = value;
			}
		}

		/// <summary>
		/// Property Href (string)
		/// </summary>
		public virtual String Href {
			get 
			{
				string state = (string) ViewState["Href"];
				if ( state != null )
				{
					if (state.StartsWith("/") || state.StartsWith(".") || state.StartsWith("~") || state.StartsWith("http://"))
						return ResolveUrl(state);
					else
						return ResolveUrl(Globals.GetSkinPath() + "/Style/" + state);
				}
				else
					return "";
			}
			set 
			{
				ViewState["Href"] = value;
			}
		}

		/// <summary>
		/// Property RelativePosition (StyleRelativePosition) 
		/// This is used when enqueue for rendering in the CS Head
		/// </summary>
		public virtual StyleRelativePosition RelativePosition 
		{
			get 
			{
				Object state = ViewState["RelativePosition"];
				if ( state != null ) 
					return (StyleRelativePosition)Enum.Parse(typeof(StyleRelativePosition),state.ToString()) ;
				else
					return StyleRelativePosition.Unspecified;
			}
			set 
			{
				ViewState["RelativePosition"] = value;
			}
		}

		/// <summary>
		/// Property Enqueue (Bool) 
		/// if true, the control does not render, but enques itself
		/// to be rendered in the CS:Head control
		/// </summary>
		public virtual bool Enqueue 
		{
			get 
			{
				Object state = ViewState["Enqueue"];
				if ( state != null ) 
					return bool.Parse(state.ToString()) ;
				else
					return false;
			}
			set 
			{
				ViewState["Enqueue"] = value;
			}
		}
      
        const string linkFormat = "<link rel=\"stylesheet\" href=\"{0}\" type=\"text/css\" media=\"{1}\" />";
		const string userStyleFormat = "<style type=\"text/css\"> body {{ font-size: {0}; }} </style>";

		protected override void OnPreRender(EventArgs e)
		{
			if(this.Enqueue)
			{
				Head.AddStyle(this.Href, this.Media, CSContext.Current.Context, this.RelativePosition)  ;
			}
		}

        protected override void Render(HtmlTextWriter writer)
        {
			if(this.Enqueue)
				return; //render in the cs:head

			if (!Globals.IsNullorEmpty(Href))
				writer.Write(linkFormat,Href,Media);
			else
			{
				// switch for font size
				CSContext csContext = CSContext.Current;
				if (!csContext.User.IsAnonymous)
					writer.Write(userStyleFormat, ResolveFontSize(csContext.User.Profile.FontSize));
				else
					writer.Write(userStyleFormat, ResolveFontSize(0));
			}
        }

		protected string ResolveFontSize(int size)
		{
			size *= 15;
			return (84 + size) + "%";
		}
	}
}