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

using System;
using CommunityServer.Components;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;

namespace CommunityServer.ControlPanel.Controls
{

	/// <summary>
	/// Renders a Tab + Submenus based on the tab configuration file.
	/// </summary>
	public class ControlPanelNavigationSidebar : ControlPanelTabDrivenTemplatedWebControl
	{
		public ControlPanelNavigationSidebar() : base()
		{
		}

		#region Private Members
		
		private CA.NavBar _navBar;

		#endregion

		#region Skin Methods

		protected override void AttachChildControls()
		{
			_navBar = (CA.NavBar) FindControl("NavBar");
			if (_navBar != null)
				BuildNavBar();
		}

		/// <summary>
		/// Creates the markup for the current TabCollection
		/// </summary>
		/// <returns></returns>
		protected void BuildNavBar()
		{
			_navBar.Items.Clear();
			CA.NavBarItem parentItem, childItem;

			//Get the tab data
			TabCollection tc = GetTabs();
			SelectedState state;
			String url;
			
			//Walk the parents
			foreach(Tab parent in tc.Tabs)
			{
				//Is this parent enabled? Check Permissions
				if(parent.IsValid(Context))
				{
					parentItem = new CA.NavBarItem();
					parentItem.Text = GetText(parent);

					url = FormatLink(parent);
					if (parent.IsJavaScript)
						parentItem.ClientSideCommand = parent.JavaScript;
					else if (!Globals.IsNullorEmpty(url))
					{
						if (parent.IsModal)
							parentItem.ClientSideCommand = String.Format("Telligent_Modal.Open('{0}', {1}, {2}, {3});", url, parent.ModalWidth, parent.ModalHeight, Globals.IsNullorEmpty(parent.ModalCallback) ? "null" : parent.ModalCallback);
						else
							parentItem.NavigateUrl = url;

						parentItem.Text = GetText(parent, url);
					}

					parentItem.LookId = "TopItemLook";
					parentItem.SelectedLookId = "TopSelectedItemLook";
					_navBar.Items.Add(parentItem);

					//Check to see if/how this is tab is selected
					state = GetState(parent);
					if (state == SelectedState.Selected)
					{
						if (!Globals.IsNullorEmpty(parentItem.NavigateUrl))
						{
							parentItem.LookId = "TopSelectedItemLook";
							_navBar.SelectedItem = parentItem;
						}
						else
							parentItem.Expanded = true;
					}
					else if (state == SelectedState.ChildSelected)
						parentItem.Expanded = true;
					else
						parentItem.Expanded = false;

					//Is there any child tabs? Do we want to display them?
					if(parent.HasChildren)
					{
						foreach(Tab child in parent.Children)
						{
							//Make sure this tab is OK to display
							if(child.IsValid(Context) && child.Enabled && IncludeLink(child) && IsNotFiltered(child))
							{
								childItem = new CA.NavBarItem();
								childItem.Text = GetText(child);

								url = FormatLink(child);
								if (child.IsJavaScript)
									childItem.ClientSideCommand = child.JavaScript;
								else if (!Globals.IsNullorEmpty(url))
								{
									if (child.IsModal)
										childItem.ClientSideCommand = String.Format("Telligent_Modal.Open('{0}', {1}, {2}, {3});", url, child.ModalWidth, child.ModalHeight, Globals.IsNullorEmpty(child.ModalCallback) ? "null" : child.ModalCallback);
									else
										childItem.NavigateUrl = url;

									childItem.Text = GetText(child, url);
								}

								childItem.LookId = "Level2ItemLook";
								childItem.SelectedLookId = "Level2SelectedItemLook";
								parentItem.Items.Add(childItem);
	
								if (GetState(child) == SelectedState.Selected)
								{
									childItem.LookId = "Level2SelectedItemLook";
									_navBar.SelectedItem = childItem;
								}
							}
						}
					}
				}
			}
		}

		#endregion

		#region Public Properties

		private string _selected = null;
		public override string Selected
		{
			get
			{
				if(_selected == null)
					_selected = Context.Items["ControlPanelSelectedNavItem"] as string;

				return _selected;
			}
			set
			{
				_selected = value;
			}
		}

		private string fileName = "BlankNavBar.config";
		/// <summary>
		/// Just pass the file name pathed from the root of \ControlPanel\  the
		/// appropriate application url is automatically added
		/// </summary>
		public override string FileName
		{
			get
			{
				if(fileName.StartsWith("~") || fileName.StartsWith("/"))
					return fileName;
				else
					return string.Concat(SiteUrls.Instance().Locations["ControlPanel"],fileName); 
			}
			set
			{
				fileName = value;
			}
		}

		#endregion
	}
}

