using System;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI.Common;

namespace Telerik.Web.Mvc
{
	public static class RadControlsHtmlHelpers
	{
		public static string RadControlCss<T>(this HtmlHelper htmlHelper, Action<T> initializeControl)
			where T : WebControl, ISkinnableControl, new()
		{
			return ControlRenderer.GetControlCssReferences(CreateControl(initializeControl));
		}

		public static string RadControl<T>(this HtmlHelper htmlHelper, Action<T> initializeControl)
			where T : WebControl, ISkinnableControl, new()
		{
			return ControlRenderer.GetControlHtml(CreateControl(initializeControl));
		}

		private static T CreateControl<T>(Action<T> initializeControl) where T : WebControl, ISkinnableControl, new()
		{
			var control = new T {
			                    	RegisterWithScriptManager = false,
			                    	EnableEmbeddedSkins = false,
			                    	EnableEmbeddedBaseStylesheet = false,
			                    	EnableEmbeddedScripts = false
			                    };
			var page = new Page();
			page.Controls.Add(control);
			
			initializeControl(control);

			return control;
		}

		public static string RadControlScripts<T>(this HtmlHelper htmlHelper, Action<T> initializeControl)
			where T : WebControl, ISkinnableControl, new()
		{
			return ControlRenderer.GetControlScripts(CreateControl(initializeControl));
		}

		public static string RadControlScripts<T>(this HtmlHelper htmlHelper)
			where T : WebControl, ISkinnableControl, new()
		{
			return RadControlScripts<T>(htmlHelper, delegate { });
		}

		public static string RadControlCss<T>(this HtmlHelper htmlHelper) where T : WebControl, ISkinnableControl, new()
		{
			return RadControlCss<T>(htmlHelper, delegate { });
		}

		public static string RadControl<T>(this HtmlHelper htmlHelper)
			where T : WebControl, ISkinnableControl, new()
		{
			return RadControl<T>(htmlHelper, delegate { });
		}
	}
}