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

using System;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Galleries;
using CommunityServer.Galleries.Components;
using CA = ComponentArt.Web.UI;

namespace CommunityServer.ControlPanel.Photos
{
	/// <summary>
	///		Summary description for ManageCategoryDetailsControl.
	/// </summary>
	public class ManageCategoryDetailsControl : BaseGalleryControl
	{

		protected CA.TreeView TreeView1;   // using strongly typed name to avoid name conflict in asp.net 2.0

		#region Public Properties

		private int categoryID = CSContext.Current.GetIntFromQueryString("CategoryID",0);
		protected System.Web.UI.WebControls.Panel NoPhotosMessage;
	
		public virtual int CategoryID
		{
			get{return categoryID;}
			set
			{
				categoryID = value;
				if(pageHasLoaded)
				{
					TreeView1.Nodes.Clear();
					buildTree();
					TreeView1.DataBind();
				}
			}
		}

		#endregion

		bool pageHasLoaded = false;

		private bool HasCategoryIDFilter
		{
			get{return CategoryID > 0;}
		}

		private void Page_Load(object sender, EventArgs e)
		{
			AjaxManager.Register(this,"ManageCategoryDetailsControl");

			if(!Page.IsPostBack)
			{
				buildTree();
			}
			pageHasLoaded=true;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void buildTree()
		{

			GalleryThreadQuery query = new GalleryThreadQuery();
			query.SectionID = CurrentGallery.SectionID;
			query.ApplicationPostType = GalleryPostType.Image;
			query.SortBy = GalleryThreadSortBy.ThreadDate;
			CSContext cntx = CSContext.Current;
			query.RequireSectionIsActive = !(cntx.User.IsGalleryAdministrator || cntx.User.IsAdministrator);

			query.PageSize = int.MaxValue;

			if(HasCategoryIDFilter)
				query.CategoryID = CategoryID;
			else
				query.UncategorizedOnly = true;

			query.SortOrder = SortOrder.Descending;

			ThreadSet ts = GalleryPosts.GetPictures(query, false);
			
			foreach(GalleryPost post in ts.Threads)
			{
					CA.TreeViewNode newNode = CreateNode(post);
					TreeView1.Nodes.Add(newNode);
			}

			if(ts.TotalRecords != 0)
				TreeView1.ReloadTemplates();
			else
			{
				TreeView1.Visible = false;
				NoPhotosMessage.Visible = true;
			}
		}

		private CA.TreeViewNode CreateNode(GalleryPost post)
		{
			CA.TreeViewNode node = new CA.TreeViewNode();
			node.Text = post.Subject; 
			//node.ImageUrl = post.ThumbnailURL; 
			node.Expanded = true;
			node.ID = post.PostID.ToString();
			node.Attributes.Add("PostID", post.PostID.ToString());
			node.Attributes.Add("ThumbnailURL", post.ThumbnailURL);
			node.Attributes.Add("ViewPictureURL", post.ViewPictureURL);
			node.Attributes.Add("Published", post.IsPostEnabled.ToString());
			node.Attributes.Add("Subject", post.Subject);
			node.Attributes.Add("SectionID", post.SectionID.ToString());
			
			node.TemplateId = "ItemTemplate";
			return node;
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=false)]
		public int MovePhoto(int postID, int newCategoryID, int oldCategoryID)
		{
			GalleryPost post = GalleryPosts.GetPicture(postID);
			ArrayList currentCategories = GalleryPosts.GetPictureCategories(post);
		
			if (newCategoryID != 0)
			{
				PostCategory newCategory = PostCategories.GetCategory(newCategoryID, CurrentGallery.SectionID, true);
				if(!currentCategories.Contains(newCategory))
					currentCategories.Add(newCategory);
			}
			
			ArrayList newCategories = new ArrayList();
			foreach(PostCategory pc in currentCategories)
			{
				if(pc.CategoryID != oldCategoryID)
					newCategories.Add(pc.Name); 
			}

			post.Categories = (string[]) newCategories.ToArray(typeof (string));

			GalleryPosts.UpdatePicture(post);
			
			return post.PostID;

		}


	}
}
