using System;
using System.Collections.Generic;
using System.IO;
using System.Web;

using Bitrix;
using Bitrix.Components;
using Bitrix.Configuration;
using Bitrix.IO;
using Bitrix.Security;
using Bitrix.Services;
using Bitrix.Services.Js;
using Bitrix.UI;
using Bitrix.UI.Popup;

namespace Bitrix.Main.Components
{
	public class SystemPublicBreadCrumbComponent : BXComponent
	{
		public BXPublicMenuItem[] BreadCrumb
		{
			get
			{
				return Results.Get<BXPublicMenuItem[]>("BreadCrumb");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Parameters.GetBool("ShowOnIndexPage", false) && IsRootPage(new Uri(BXUri.ToAbsoluteUri(Page.Request.RawUrl))))
				return;

			BXPublicMenuItem[] crumbs = BXPublicMenu.Menu.GetBreadCrumbByUri(Page.Request.RawUrl);
			if (crumbs != null && crumbs.Length > 0)
			{
				string rootItemTitle = Parameters.GetString("RootItemTitle");
				if (!String.IsNullOrEmpty(rootItemTitle))
					crumbs[0].Title = rootItemTitle;
			}

			Results["BreadCrumb"] = crumbs;
			IncludeComponentTemplate();
		}

		private bool IsRootPage(Uri requestURI)
		{
			string siteUri = VirtualPathUtility.ToAbsolute("~/" + BXSite.Current.Directory);
			return
				(
					String.Equals(requestURI.AbsolutePath, siteUri, StringComparison.OrdinalIgnoreCase) ||
					String.Equals(requestURI.AbsolutePath, siteUri + "default.aspx", StringComparison.OrdinalIgnoreCase)
				);
		}

		protected override void PreLoadComponentDefinition()
		{
			Title = GetMessage("Title");
			Description = GetMessage("Description");
			Icon = "component.gif";

			Group = new BXComponentGroup("system_menu", GetMessage("Group"), 100, BXComponentGroup.Utility);

			BXCategory mainCategory = BXCategory.Main;
			ParamsDefinition["RootItemTitle"] = new BXParamText(GetMessageRaw("Component.Parameter.Title.RootItemTitle"), BXLoc.GetModuleMessage("main", "PublicMenu.RootItem.Title"), mainCategory);
			ParamsDefinition["ShowOnIndexPage"] = new BXParamYesNo(GetMessageRaw("Component.Parameter.Title.ShowOnIndexPage"), false, mainCategory);

		}
	}
	public class SystemPublicBreadCrumbTemplate : BXComponentTemplate<SystemPublicBreadCrumbComponent>
	{
	}
}

#region Compatibility Issue
public partial class PublicBreadCrumbComponent : Bitrix.Main.Components.SystemPublicBreadCrumbComponent
{
	//NESTED CLASSES
	public class Template : BXComponentTemplate<PublicBreadCrumbComponent>
	{
	}
}
#endregion