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

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Files.Components;

namespace CommunityServer.Files.Controls
{
	public class BreadCrumb : Literal 
	{
		protected CSContext csContext = CSContext.Current;
		protected Queue crumbs = new Queue();
		protected HtmlAnchor anchor;

		#region Properties
	
		/// <summary>
		/// Controls whether or not the root element for the home is shown
		/// </summary>
		[
		System.ComponentModel.DefaultValue( false ),
		]
		public virtual Boolean ShowHome 
		{
			get 
			{
				Object state = ViewState["ShowHome"];
				if ( state != null ) 
				{
					return (Boolean)state;
				}
				return false;
			}
			set 
			{
				ViewState["ShowHome"] = value;
			}
		}

		/// <summary>
		///  Determines whether or not links are hook-ed up.
		/// </summary>
		[
		System.ComponentModel.DefaultValue( true ),
		]
		public virtual Boolean EnableLinks 
		{
			get 
			{
				Object state = ViewState["EnableLinks"];
				if ( state != null ) 
				{
					return (Boolean)state;
				}
				return true;
			}
			set 
			{
				ViewState["EnableLinks"] = value;
			}
		}

		#endregion


		protected override void CreateChildControls() 
		{

			// Do we need to add the home link?
			//
			if (ShowHome) 
				crumbs.Enqueue(GetAnchor(FileGalleryResourceManager.GetString("files"), FileGalleryUrls.Instance().Home));

			// Is a Forum Group ID specified?
			//
			if (csContext.GroupID > 0)
				AddFolderGroup(csContext.GroupID);

			// Is a PostID specified? (or do we have a folder?)
			//
			if (csContext.PostID > 0)
				AddEntry(csContext.PostID);
			else if (csContext.ApplicationKey != null)
				AddFolder(Folders.GetFolder(csContext.ApplicationKey, true).SectionID);	
			
			AddByUrl(csContext.CurrentUri.ToString());
		}

		protected void AddEntry(int entryID) 
		{

			// Get the Post
			//
			Entry p = Entries.GetEntry(entryID);

			// Get the forum
			//
			Folder f = (Folder) Folders.GetFolder(p.SectionID);

			// If folder is null, it is probably a post from another application... just return to avoid null references
			if(f == null)
				return;

			AddFolder(f.SectionID);

			crumbs.Enqueue(GetAnchor(p.Subject, FileGalleryUrls.Instance().ViewEntry(csContext.ApplicationKey, p.PostID)));
		}

		protected void AddFolder(int folderID) 
		{
			// Get the folder
			//
			Folder f = Folders.GetFolder(folderID, true);
			User user = csContext.User;

			// Only add the folder group if the user is allowed to see it
			//
			try 
			{
				Permissions.AccessCheck(f, Permission.View, user );

				// First add the forum group
				//
				AddFolderGroup(f.GroupID);

				crumbs.Enqueue(GetAnchor(f.Name, FileGalleryUrls.Instance().ViewFolder(csContext.ApplicationKey)));
			} 
			catch 
			{
			}
		}

		protected virtual void AddFolderGroup(int folderGroupID) 
		{

			// Get the forum group
			//
			Group g = Folders.GetFolderGroup(folderGroupID, true, true);

			crumbs.Enqueue(GetAnchor(g.Name, FileGalleryUrls.Instance().ViewFolderGroup(g.GroupID)));
		}

		protected virtual void AddByUrl(string url)
		{
			if (csContext.ApplicationKey != null)
			{
				if (url.IndexOf(FileGalleryUrls.Instance().EntryAdmin(csContext.ApplicationKey, -1)) != -1)
					crumbs.Enqueue(GetAnchor(FileGalleryResourceManager.GetString("Files_UploadFile"), ""));
				else if (url.IndexOf(FileGalleryUrls.Instance().EntryAdmin(csContext.ApplicationKey, csContext.PostID)) != -1)
					crumbs.Enqueue(GetAnchor(FileGalleryResourceManager.GetString("Files_EditFile"), ""));
				else if (url.IndexOf(FileGalleryUrls.Instance().DeleteEntry(csContext.ApplicationKey, csContext.PostID)) != -1)
					crumbs.Enqueue(GetAnchor(FileGalleryResourceManager.GetString("Files_DeleteFile"), ""));
				else if (csContext.CategoryID != -1)
					crumbs.Enqueue(GetAnchor(PostCategories.GetCategory(csContext.CategoryID, Folders.GetFolderID(csContext.ApplicationKey)).Name, ""));
			}
		}

		protected Control GetAnchor(string innerText, string href) 
		{
			anchor = new HtmlAnchor();
			anchor.InnerHtml = innerText;
			anchor.Attributes["class"] = "lnk3";
			anchor.HRef = href;

			return anchor;
		}

		protected override void Render(HtmlTextWriter writer) 
		{

			while(crumbs.Count > 0) 
			{
				HtmlAnchor a = (HtmlAnchor) crumbs.Dequeue();

				// Add title for hover over, 
				// but html decoded
				//
				a.Title = Globals.HtmlDecode( a.InnerHtml );

				if ((crumbs.Count > 0) && (a.InnerHtml.Length > 15))
					a.InnerHtml = a.InnerHtml.Substring(0,15) + "...";

				a.RenderControl(writer);

				if (crumbs.Count > 0)
					writer.Write(FileGalleryResourceManager.GetString("BreadCrumb_Seperator"));

			}
		}
	}
}
