using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using Bitrix.Components;
using Bitrix.DataTypes;
using Bitrix.IO;
using Bitrix.Services.Text;
using Bitrix.UI;
using Bitrix.Security;

namespace Bitrix.Forum.Components
{
    public partial class ForumMenuComponent : BXComponent
    {
		public string ForumIndexUrl
		{
			get
			{
				return Parameters.GetString("ForumIndexUrl");
			}
			set
			{
				Parameters["ForumIndexUrl"] = value;
			}
		}
		public string UserProfileUrlTemplate
		{
			get
			{
				return Parameters.GetString("UserProfileUrlTemplate");
			}
			set
			{
				Parameters["UserProfileUrlTemplate"] = value;
			}
		}
		public string SearchUrl
		{
			get
			{
				return Parameters.GetString("SearchUrl");
			}
			set
			{
				Parameters["SearchUrl"] = value;
			}
		}
		public string ForumRulesUrl
		{
			get
			{
				return Parameters.GetString("ForumRulesUrl");
			}
			set
			{
				Parameters["ForumRulesUrl"] = value;
			}
		}
		public string ForumHelpUrl
		{
			get
			{
				return Parameters.GetString("ForumHelpUrl");
			}
			set
			{
				Parameters["ForumHelpUrl"] = value;
			}
		}

        private List<MenuItem> menuItems;
		public List<MenuItem> MenuItems
		{
			get
			{
				return menuItems;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			menuItems = new List<MenuItem>();

			if (!string.IsNullOrEmpty(ForumIndexUrl)) 
				menuItems.Add(new MenuItem(GetMessageRaw("UrlTitle.Forums"), ResolveTemplateUrl(ForumIndexUrl, null), "forums", ""));

			if (!string.IsNullOrEmpty(UserProfileUrlTemplate) && BXPrincipal.Current.Identity.IsAuthenticated)
			{
				BXParamsBag<object> replace = new BXParamsBag<object>();
				replace.Add("UserId", BXIdentity.Current.Id);
				menuItems.Add(new MenuItem(GetMessageRaw("UrlTitle.Profile"), ResolveTemplateUrl(UserProfileUrlTemplate, replace), "profile", ""));
			}

			if (!string.IsNullOrEmpty(SearchUrl))
				menuItems.Add(new MenuItem(GetMessageRaw("UrlTitle.Search"), ResolveTemplateUrl(SearchUrl, null), "search", ""));

			if (!string.IsNullOrEmpty(ForumRulesUrl))
				menuItems.Add(new MenuItem(GetMessageRaw("UrlTitle.Rules"), ResolveTemplateUrl(ForumRulesUrl, null), "rules", ""));

			if (!string.IsNullOrEmpty(ForumHelpUrl))
				menuItems.Add(new MenuItem(GetMessageRaw("UrlTitle.Help"), ResolveTemplateUrl(ForumHelpUrl, null), "help", ""));

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

			foreach (string param in new string[] { Parameters.GetString("ThemeCssFilePath"), Parameters.GetString("ColorCssFilePath") })
			{
				if (BXStringUtility.IsNullOrTrimEmpty(param))
					continue;

				string path = param;
				try
				{
					path = BXPath.ToVirtualRelativePath(path);
					if (BXSecureIO.FileExists(path))
						BXPage.RegisterStyle(path);
				}
				catch {}
			}
		}
        protected override void PreLoadComponentDefinition()
        {
            Title = GetMessageRaw("Title");
            Description = GetMessageRaw("Description");
            Icon = "images/icon.gif";
            Group = new BXComponentGroup("forum", GetMessageRaw("Group"), 100, BXComponentGroup.Communication);

			BXCategory urlCategory = BXCategory.UrlSettings;
            BXCategory mainCategory = BXCategory.Main;

			ParamsDefinition.Add("ThemeCssFilePath", new BXParamText(GetMessageRaw("Param.ThemeCssFilePath"), "~/bitrix/components/bitrix/forum/templates/.default/style.css", mainCategory));
			ParamsDefinition.Add("ColorCssFilePath", new BXParamText(GetMessageRaw("Param.ColorCssFilePath"), "~/bitrix/components/bitrix/forum/templates/.default/themes/default/style.css", mainCategory));

			ParamsDefinition.Add("ForumIndexUrl", new BXParamText(GetMessageRaw("Param.ForumIndexUrl"), "", urlCategory));
			ParamsDefinition.Add("UserProfileUrlTemplate", new BXParamText(GetMessageRaw("Param.USerProfileUrlTemplate"), "", urlCategory));
			ParamsDefinition.Add("SearchUrl", new BXParamText(GetMessageRaw("Param.SearchUrl"), "", urlCategory));
			ParamsDefinition.Add("ForumRulesUrl", new BXParamText(GetMessageRaw("Param.ForumRulesUrl"), "", urlCategory));
			ParamsDefinition.Add("ForumHelpUrl", new BXParamText(GetMessageRaw("Param.ForumHelpUrl"), "", urlCategory));
			
            //ParamsDefinition.Add("MaxWordLength", new BXParamText(GetMessageRaw("MaxWordLength"), "50", additionalSettingsCategory));
        }

		public class MenuItem
		{
			private string href;
			private string titleHtml;
			private string className;
			private string tooltipHtml;

			public string TooltipHtml
			{
				get
				{
					return tooltipHtml;
				}
				internal set
				{
					tooltipHtml = value;
				}
			}
			public string ClassName
			{
				get
				{
					return className;
				}
				internal set
				{
					className = value;
				}
			}
            public string TitleHtml
			{
				get
				{
					return titleHtml;
				}
				internal set
				{
					titleHtml = value;
				}
			}
			public string Href
			{
				get
				{
					return href;
				}
				internal set
				{
					href = value;
				}
			}


			internal MenuItem(string title, string url, string cssClass, string tooltip)
			{
				titleHtml = title != null ? HttpUtility.HtmlEncode(title) : null;
				href = url != null ? HttpUtility.HtmlEncode(url) : null;
				className = cssClass != null ? HttpUtility.HtmlEncode(cssClass) : null;
				tooltipHtml = tooltip != null ? HttpUtility.HtmlEncode(tooltip) : null;
			}
		}
    }

	public class ForumMenuTemplate : BXComponentTemplate<ForumMenuComponent>
    {
        protected override void Render(HtmlTextWriter writer)
        {
            StartWidth = "100%";
            if (IsComponentDesignMode)
            {
               
            }
            base.Render(writer);
        }
    }
}
