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

using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	public class AggregatePortalPictureScroller : ContentScroller
	{
		private CSContext csContext;

		#region Event Handlers

		protected override void OnLoad(EventArgs e) 
		{
			base.OnLoad(e);
			csContext = CSContext.Current;
			DataBind();

		}

		protected override void Render(HtmlTextWriter writer)
		{
			writer.Write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td><div class=\"");
			writer.Write(this.LeftNavigationCssClass);
			writer.Write("\" onclick=\"");
			writer.Write(this.ClientID);
			writer.Write(".MovePreviousPage();\">&nbsp;</div></td><td width=\"100%\">");

			base.Render(writer);
	
			writer.Write("</td><td><div class=\"");
			writer.Write(this.RightNavigationCssClass);
			writer.Write("\" onclick=\"");
			writer.Write(this.ClientID);
			writer.Write(".MoveNextPage();\">&nbsp;</div></td></tr></table>");
		}

		#endregion

		#region Properties

		private int imageQuality = 70;
		public int ImageQuality
		{
			get { return imageQuality; }
			set { imageQuality = value; }
		}

		[DefaultValue( null )]
		private string applicationKey = null;
		public string ApplicationKey
		{
			get { return applicationKey; }
			set { applicationKey = value; }
		}

		[DefaultValue( -1 )]
		private int categoryID = -1;
		public int CategoryID
		{
			get { return categoryID; }
			set { categoryID = value; }
		}

		[DefaultValue( GalleryThreadSortBy.ThreadDate )]
		private GalleryThreadSortBy sortBy = GalleryThreadSortBy.ThreadDate;
		public GalleryThreadSortBy SortBy
		{
			get { return sortBy; }
			set { sortBy = value; }
		}

		private SortOrder sortOrder = SortOrder.Descending;
		public SortOrder SortOrder
		{
			get { return sortOrder; }
			set { sortOrder = value; }
		}

		[DefaultValue( true )]
		private bool showPictures = true;
		public bool ShowPictures
		{
			get { return showPictures; }
			set { showPictures = value; }
		}

		[DefaultValue( false )]
		private bool showPictureNames = false;
		public bool ShowPictureNames
		{
			get { return showPictureNames; }
			set { showPictureNames = value; }
		}

		[DefaultValue( false )]
		private bool showPictureDetails = false;
		public bool ShowPictureDetails
		{
			get { return showPictureDetails; }
			set { showPictureDetails = value; }
		}

		[DefaultValue( true )]
		private bool showPictureAuthors = true;
		public bool ShowPictureAuthors
		{
			get { return showPictureAuthors; }
			set { showPictureAuthors = value; }
		}

		public string LeftNavigationCssClass
		{
			get
			{
				string state = (string) ViewState["LeftNavigationCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["LeftNavigationCssClass"] = value;
			}
		}

		public string RightNavigationCssClass
		{
			get
			{
				string state = (string) ViewState["RightNavigationCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["RightNavigationCssClass"] = value;
			}
		}

		public string ImageAreaCssClass
		{
			get
			{
				string state = (string) ViewState["ImageAreaCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["ImageAreaCssClass"] = value;
			}
		}

		public string ImageCssClass
		{
			get
			{
				string state = (string) ViewState["ImageCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["ImageCssClass"] = value;
			}
		}

		public string ImageDetailsCssClass
		{
			get
			{
				string state = (string) ViewState["ImageDetailsCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["ImageDetailsCssClass"] = value;
			}
		}

		public string ImageNameCssClass
		{
			get
			{
				string state = (string) ViewState["ImageNameCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["ImageNameCssClass"] = value;
			}
		}

		public string ImageAuthorCssClass
		{
			get
			{
				string state = (string) ViewState["ImageAuthorCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["ImageAuthorCssClass"] = value;
			}
		}

		public int MaximumNumberOfImagesToDisplay
		{
			get
			{
				object state = ViewState["MaximumNumberOfImagesToDisplay"];
				if (state == null)
					return 20;
				else
					return (int) state;
			}
			set
			{
				ViewState["MaximumNumberOfImagesToDisplay"] = value;
			}
		}

		public int ImageHeight
		{
			get
			{
				object state = ViewState["ImageHeight"];
				if (state == null)
					return 40;
				else
					return (int) state;
			}
			set
			{
				ViewState["ImageHeight"] = value;
			}
		}

		public int ImageWidth
		{
			get
			{
				object state = ViewState["ImageWidth"];
				if (state == null)
					return 40;
				else
					return (int) state;
			}
			set
			{
				ViewState["ImageWidth"] = value;
			}
		}

		#endregion

		#region ContentScroller Implementation

		private string _contentTemplate = null;
		protected override string ContentTemplate 
		{
			get
			{
				if (_contentTemplate == null)
				{
					int index = 0;

					StringBuilder t = new StringBuilder();
					t.Append("<div class=\"");
					t.Append(this.ImageAreaCssClass);
					t.Append("\">");
					
					if (this.ShowPictures)
					{
						t.Append("<div class=\"");
						t.Append(this.ImageCssClass);
						t.Append("\">{");
						t.Append(index);
						index++;
						t.Append("}</div>");
					}

					if (this.ShowPictureNames)
					{
						t.Append("<div class=\"");
						t.Append(this.ImageNameCssClass);
						t.Append("\">{");
						t.Append(index);
						index++;
						t.Append("}</div>");
					}

					if (this.ShowPictureAuthors)
					{
						t.Append("<div class=\"");
						t.Append(this.ImageAuthorCssClass);
						t.Append("\">{");
						t.Append(index);
						index++;
						t.Append("}</div>");
					}
					
					if (this.ShowPictureDetails)
					{
						t.Append("<div class=\"");
						t.Append(this.ImageDetailsCssClass);
						t.Append("\">{");
						t.Append(index);
						index++;
						t.Append("}</div>");
					}

					t.Append("</div>");

					_contentTemplate = t.ToString();
				}

				return _contentTemplate;
			}
		}

		private string[,] _scrollerData;
		protected override string[,] ScrollerData 
		{ 
			get
			{
				return _scrollerData;
			}
		}

		#endregion

		#region DataBind

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

			// Setup the query
			GalleryThreadQuery query = new GalleryThreadQuery();

			if(applicationKey != null)
				query.SectionID = Galleries.GetGallery(applicationKey).SectionID;
			else
				query.FilterByList = Galleries.GetGalleries();

			query.CategoryID = categoryID;
			query.PageSize = this.MaximumNumberOfImagesToDisplay;
			query.SortBy = sortBy;
			query.SortOrder = sortOrder;
			query.ApplicationPostType = GalleryPostType.Image;

			ArrayList pictures = GalleryPosts.GetPictures(query, true, true).Threads;
			
			if (pictures != null)
			{
				int index = 0;
				if (this.ShowPictures)
					index++;

				if (this.ShowPictureNames)
					index++;

				if (this.ShowPictureAuthors)
					index++;
					
				if (this.ShowPictureDetails)
					index++;

				_scrollerData = new string[pictures.Count, index];

				GalleryPost picture;
				for (int i = 0; i < pictures.Count; i++)
				{
					index = 0;
					picture = (GalleryPost) pictures[i];

					if (this.ShowPictures)
					{
						_scrollerData[i, index] = "<a href=\"" + GalleryUrls.Instance().ViewPicture(picture.Section.ApplicationKey, picture) + "\"><img src=\"" + GetPictureUrl(picture) + "\" border=\"0\" /></a>";
						index++;
					}

					if (this.ShowPictureNames)
					{
						_scrollerData[i, index] = "<a href=\"" + GalleryUrls.Instance().ViewPicture(picture.Section.ApplicationKey, picture) + "\">" + picture.Subject + "</a>";
						index++;
					}

					if (this.ShowPictureAuthors)
					{
						_scrollerData[i, index] = ResourceManager.GetString("Gallery_CommentListing_PostedBy") + " <a href=\"" + SiteUrls.Instance().UserProfile(picture.AuthorID) + "\">" + picture.User.DisplayName + "</a>";
						index++;
					}

					if (this.ShowPictureDetails)
					{
						#region Show Details

						switch(this.SortBy)
						{
							case GalleryThreadSortBy.ThreadDate:
						
								User user = csContext.User;
								DateTime postDate = picture.PostDate;
								//string dateFormat;

								if(!user.IsAnonymous)
								{
									postDate = user.GetTimezone(picture.PostDate);
								//	dateFormat = user.Profile.DateFormat;
								}
								//else
								//	dateFormat = csContext.SiteSettings.DateFormat;

								_scrollerData[i, index] = ResourceManager.GetString("Gallery_PortalPictureListing_FieldPostDate") + postDate.ToString(csContext.SiteSettings.TimeFormat);

								break;

							case GalleryThreadSortBy.Rating:

								_scrollerData[i, index] = ResourceManager.GetString("Gallery_PortalPictureListing_FieldRating") +  picture.RatingAverage.ToString("0.0#");

								break;

							case GalleryThreadSortBy.Comments:
						
								_scrollerData[i, index] = ResourceManager.GetString("Gallery_PortalPictureListing_FieldReplies") + picture.Replies.ToString();

								break;

							case GalleryThreadSortBy.Views:
						
								_scrollerData[i, index] = ResourceManager.GetString("Gallery_PortalPictureListing_FieldViews") + picture.Views.ToString();

								break;
						}

						index++;
						#endregion
					}
				}
				
			}
			else
				_scrollerData = new string[0,0];
		}

		private string GetPictureUrl(GalleryPost picture)
		{
			GalleryImageSettings settings = new GalleryImageSettings(this.ImageWidth, this.ImageHeight, ImageQuality, true);
			settings.GalleryImageType = GalleryImageType.Other;

			return ImageHandling.GetPictureUrl(picture, settings); 
		}

		#endregion		
	}
}
