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

using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{

	public class NavigationMenu : GalleryThemedControl
	{
		private CSContext cSContext;

		#region Child Controls

		private Repeater adminFunctionsRepeater;
		private Repeater navigationRepeater;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			adminFunctionsRepeater = (Repeater)FindControl( "AdminFunctionsRepeater" );
			navigationRepeater = (Repeater)FindControl( "NavigationRepeater" );
			InitializeChildControls();
		}

		private void InitializeChildControls()
		{
			cSContext = CSContext.Current;

			if(adminFunctionsRepeater != null)
				adminFunctionsRepeater.ItemDataBound += new RepeaterItemEventHandler(adminFunctionsRepeater_ItemDataBound);
			if(navigationRepeater != null)
				navigationRepeater.ItemDataBound += new RepeaterItemEventHandler(navigationRepeater_ItemDataBound);

			//set the page titles
			string pageTitle = "";
			if(cSContext.CategoryID != -1)
				pageTitle += PostCategories.GetCategory(cSContext.CategoryID, CurrentGallery.SectionID).Name;
			if(cSContext.PostID != -1)
			{
				if(pageTitle.Length > 0)
					pageTitle += " : ";
				pageTitle += GalleryPosts.GetPicture(cSContext.PostID).Subject;
			}
		}

		#endregion

		#region Event Overrides

		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			if(!Page.IsPostBack)
				DataBind();
		}


		public override void DataBind()
		{
			base.DataBind();
			BindData();

		}

		#endregion

		private void BindData()
		{
			ArrayList navItems = new ArrayList();
			ArrayList adminItems = new ArrayList();
			User user = cSContext.User;

			PostCategory currentCategory = null;
			GalleryPost currentGalleryPost = null;
			string categoryName = null;
			string postName = null;

			if(cSContext.CategoryID != -1)
				currentCategory = PostCategories.GetCategory(cSContext.CategoryID, CurrentGallery.SectionID, CurrentUser.IsGalleryAdministrator);

			if(cSContext.PostID != -1)
				currentGalleryPost = GalleryPosts.GetPicture(cSContext.PostID);

			if(CurrentGallery != null)
				navItems.Add( CurrentGallery );
		
			//TODO: Iterate through all parent categories to root here
			if(currentCategory != null)
			{
				navItems.Add( currentCategory );
				categoryName = currentCategory.Name;
			}

			if(currentGalleryPost != null)
			{
				navItems.Add( currentGalleryPost );
				postName = currentGalleryPost.Subject;

				//update the number of times the post was viewed
				Views.AddWebCount(currentGalleryPost.PostID);
			}


			if(navigationRepeater != null)
			{
				this.SetPageTitle(categoryName, postName);
				navigationRepeater.DataSource = navItems;
				navigationRepeater.DataBind();
			}

			//Build the admin repeater

			if(CurrentGallery != null)
			{
				if( Permissions.ValidatePermissions( CurrentGallery, Permission.Post, user))
				{
					if(currentGalleryPost == null)
					{
						if(currentCategory != null)
							adminItems.Add( CreateLink( "GalleryNav_AddPicture", GalleryUrls.Instance().ControlPanel_Photos_NewPost(CurrentGallery.SectionID, currentCategory.CategoryID, cSContext.RawUrl)));
						else
							adminItems.Add( CreateLink( "GalleryNav_AddPicture", GalleryUrls.Instance().ControlPanel_Photos_NewPost(CurrentGallery.SectionID, cSContext.RawUrl)));
					}
					else
					{
						if(currentCategory != null)
							adminItems.Add( CreateLink( "GalleryNav_EditPicture", GalleryUrls.Instance().ControlPanel_Photos_EditPost(CurrentGallery.SectionID, currentCategory.CategoryID, currentGalleryPost.PostID, cSContext.RawUrl)));
						else
							adminItems.Add( CreateLink( "GalleryNav_EditPicture", GalleryUrls.Instance().ControlPanel_Photos_EditPost(CurrentGallery.SectionID, currentGalleryPost.PostID, cSContext.RawUrl)));
					}
					if(currentCategory == null)
					{
						adminItems.Add( CreateLink( "GalleryNav_ManageAlbums", GalleryUrls.Instance().ControlPanel_Photos_CategoryManager_Gallery(CurrentGallery.SectionID)));
					}
					else
					{
						
						adminItems.Add( CreateLink( "GalleryNav_ManageAlbums", GalleryUrls.Instance().ControlPanel_Photos_CategoryManager_Category(CurrentGallery.SectionID,  currentCategory.CategoryID )));
					}
	
					adminItems.Add( CreateLink( "GalleryNav_ControlPanel", GalleryUrls.Instance().ControlPanel_Photos_Gallery(CurrentGallery.SectionID ) ));
				}
			}

			if(adminFunctionsRepeater != null)
			{
				if(adminItems.Count > 0)
				{
					adminFunctionsRepeater.DataSource = adminItems;
					adminFunctionsRepeater.DataBind();
				}
				else
					adminFunctionsRepeater.Visible = false;
			}
		}

		private HyperLink CreateLink(string resourceName, string navigateUrl)
		{
			HyperLink link = new HyperLink();
			link.Text = ResourceManager.GetString( resourceName );
			link.NavigateUrl = navigateUrl;
			return link;
		}

		private void navigationRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			HyperLink navLink = e.Item.FindControl( "NavLink" ) as HyperLink;
			if(navLink == null)
				return;

			if(e.Item.DataItem is Gallery)
			{
				Gallery gallery = e.Item.DataItem as Gallery;
				navLink.Text = gallery.Name;
				navLink.NavigateUrl = GalleryUrls.Instance().ViewGallery( gallery.ApplicationKey );
			}
			else if(e.Item.DataItem is PostCategory)
			{
				PostCategory category = e.Item.DataItem as PostCategory;
				navLink.Text = category.Name;
				navLink.NavigateUrl = GalleryUrls.Instance().ViewCategory( CurrentGallery.ApplicationKey, category.CategoryID );
			}
			else if(e.Item.DataItem is GalleryPost)
			{
				GalleryPost galleryPost = e.Item.DataItem as GalleryPost;
				int categoryID = -1;
				if(cSContext.CategoryID != -1)
					categoryID = cSContext.CategoryID;
				if(galleryPost.Subject != string.Empty)
					navLink.Text = galleryPost.Subject;
				else
					navLink.Text = ResourceManager.GetString( "Gallery_NoTitle" );
				navLink.NavigateUrl = GalleryUrls.Instance().ViewPicture( CurrentGallery.ApplicationKey, categoryID, galleryPost );
			}
		}

		private void adminFunctionsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			HyperLink item = e.Item.DataItem as HyperLink;
			
			switch(e.Item.ItemType)
            {
				case ListItemType.Item:
				case ListItemType.AlternatingItem:

					if(item != null)
					{
						HyperLink adminLink = (HyperLink)e.Item.FindControl( "AdminLink" );
						adminLink.Text = item.Text;
						adminLink.NavigateUrl = item.NavigateUrl;
					}

					break;
            }
		}
	}
}
