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

using System;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Files.Components;

namespace CommunityServer.Files.Controls
{

	public class FolderListing : FileGalleryTemplatedWebControl
	{

		#region Child Controls

		private Repeater GroupRepeater;

		#endregion

		#region Public Properties

		[DefaultValue( 10 )]
		public virtual Int32 ItemsPerPage
		{
			get
			{
				Object state = ViewState["ItemsPerPage"];
				if(state != null)
					return (Int32)state;
				return 10;
			}
			set
			{ ViewState["ItemsPerPage"] = value; }
		}

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			GroupRepeater = (Repeater)FindControl( "GroupRepeater" );
			GroupRepeater.ItemCreated += new RepeaterItemEventHandler(GroupRepeater_ItemCreated);
			GroupRepeater.ItemDataBound += new RepeaterItemEventHandler(GroupRepeater_ItemDataBound);
		}

		#endregion

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.DataBind();
		}

		public override void DataBind()
		{
			base.DataBind ();
			BindGalleries();
		}

		private void BindGalleries()
		{
			GroupRepeater.DataSource = Folders.GetFolderGroups(true, false, false);
			GroupRepeater.DataBind();
		}

		private void GroupRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
		{
			Group group = e.Item.DataItem as Group;

			if(group == null)
				return;

			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
					FolderRepeater folderRepeater = e.Item.FindControl( "FolderRepeater" ) as FolderRepeater;
					if (folderRepeater != null)
					{
						folderRepeater.Mode = ControlUserMode.GalleryUser;
						folderRepeater.Paginate = true;
						folderRepeater.ItemsPerPage = ItemsPerPage;
						if(Page.Request.QueryString[group.GroupID.ToString() + "page"] != null)
							folderRepeater.CurrentPage = int.Parse(Page.Request.QueryString[group.GroupID.ToString() + "page"]);
						folderRepeater.DataBinding += new EventHandler(folderRepeater_DataBinding);
						folderRepeater.ItemDataBound += new RepeaterItemEventHandler(galleryRepeater_ItemDataBound);
					}
					break;
			}
		}

		private void folderRepeater_DataBinding(object sender, EventArgs e)
		{
			FolderRepeater SectionList = sender as FolderRepeater;
			RepeaterItem container = (RepeaterItem)SectionList.NamingContainer;
			Group group = container.DataItem as Group;
			SectionList.GroupID = group.GroupID;

			if(SectionList.TotalFolders > ItemsPerPage)
			{
				IText more = TextControlHelper.Create(container.FindControl("More"));
				if(more != null)
					more.Visible = true;
			}
		}

		private void GroupRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			Group group = e.Item.DataItem as Group;
			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
					HyperLink groupTitle = (HyperLink)e.Item.FindControl( "GroupTitle" );
					if (groupTitle != null)
					{
						groupTitle.Text = group.Name;
						groupTitle.NavigateUrl = FileGalleryUrls.Instance().ViewFolderGroup(group.GroupID);
					}
					break;
			}
		}

		private void galleryRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			Folder folder = e.Item.DataItem as Folder;
			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Header:
					FolderRepeater repeater = sender as FolderRepeater;
					PagingControl pagination = (PagingControl)e.Item.FindControl("Pagination");
					if((pagination != null) && (repeater != null))
					{
						// Setup the pagination
						pagination.CurrentPage = repeater.CurrentPage;
						pagination.TotalPages = (int)Math.Ceiling( (double)repeater.TotalFolders / (double)ItemsPerPage );
						pagination.PrefixText = "page";
						pagination.QueryName = repeater.GroupID.ToString() + "page";
						pagination.NavigateUrl = Page.Request.RawUrl;
						pagination.DataBind();
					}
					break;
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
					HyperLink name = (HyperLink)e.Item.FindControl( "Name" );

					if (name != null)
					{
						name.Text = folder.Name;
						name.NavigateUrl = FileGalleryUrls.Instance().ViewFolder( folder.ApplicationKey );
					}

					break;
			}
		}
	}
}
