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

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for PortalPictureListing.
	/// </summary>
	public class PortalPictureListing : GalleryThemedControl
	{
		private CSContext cSContext;
		
		#region Public Properties

		[DefaultValue( 3 )]
		private int count = 3;
		public int Count
		{
			get { return count; }
			set { count = value; }
		}

		[DefaultValue( GalleryThreadSortBy.ThreadDate )]
		private GalleryThreadSortBy sortBy = GalleryThreadSortBy.ThreadDate;
		public virtual 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; }
		}

		private int userID = -1;
		public int UserID
		{
			get { return userID; }
			set { userID = value; }
		}

		#endregion

		#region Child Controls

		private IText sectionTitle;
		private RepeaterPlusNone pictureList;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			sectionTitle = FindText( "SectionTitle" );
			pictureList = (RepeaterPlusNone)FindControl( "Pictures" );
			InitializeChildControls();
		}

		private void InitializeChildControls()
		{
			pictureList.ItemDataBound += new RepeaterItemEventHandler(pictureList_ItemDataBound);
			pictureList.NoneItemsDataBound += new RepeaterItemEventHandler(pictureList_NoneItemsDataBound);
		}

		#endregion

		#region Event Overrides

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

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

			if(IsParentInvisible(this))
				return;

			BindData();
		}

		#endregion

		private bool IsParentInvisible(Control control)
		{
			if(control.Parent == null)
				return false;
			else if(control.Parent.Visible == false)
				return true;
			else
				return IsParentInvisible(control.Parent);
		}

		private void BindData()
		{
			// Set the area title
			if(sectionTitle != null)
			{
				switch(SortBy)
				{
					case GalleryThreadSortBy.ThreadDate:
						sectionTitle.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_TitlePostDate" );
						break;
					case GalleryThreadSortBy.Rating:
						sectionTitle.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_TitleRating" );
						break;
					case GalleryThreadSortBy.Comments:
						sectionTitle.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_TitleReplies" );
						break;
					case GalleryThreadSortBy.Views:
						sectionTitle.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_TitleViews" );
						break;
				}
			}
			// Setup the query
			GalleryThreadQuery query = new GalleryThreadQuery();
			query.SectionID = CurrentGallery.SectionID;
			query.CategoryID = CSContext.Current.CategoryID;
			query.ApplicationPostType = GalleryPostType.Image;
			query.UserID = userID;
			query.PageSize = Count;
			query.SortBy = SortBy;
			query.SortOrder = SortOrder;
			query.PublishedFilter = GalleryPostPublishedFilter.Published;

			pictureList.DataSource = GalleryPosts.GetPictures(query).Threads;
			pictureList.DataBind();
		}

		private void pictureList_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			GalleryPost dataItem = e.Item.DataItem as GalleryPost;

			switch( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:

					GalleryImage pictureThumb = e.Item.FindControl("PictureThumb") as GalleryImage;
					HyperLink PictureNameLink = (HyperLink)e.Item.FindControl( "PictureName" );
					Literal fieldName = (Literal)e.Item.FindControl( "SortedName");
					Literal field = (Literal)e.Item.FindControl( "SortedField");

					if(pictureThumb != null)
					{
						pictureThumb.Visible = ShowPictures;
						if(pictureThumb.Visible == true)
						{
							pictureThumb.PostID = dataItem.PostID;
							pictureThumb.NavigateUrl = GalleryUrls.Instance().ViewPicture(dataItem.Section.ApplicationKey, cSContext.CategoryID, dataItem);
							pictureThumb.GalleryPost = dataItem;
							pictureThumb.DataBind();
						}
					}

					if (PictureNameLink != null) {
						if(dataItem.Subject != "") {
							dataItem.Subject = Globals.HtmlDecode( dataItem.Subject );
							if(dataItem.Subject.Length > 22)
								PictureNameLink.Text = dataItem.Subject.Substring(0, 22) + "...";
							else
								PictureNameLink.Text = dataItem.Subject;
						}
						else
							PictureNameLink.Text = ResourceManager.GetString( "Gallery_NoTitle" );

						PictureNameLink.NavigateUrl = GalleryUrls.Instance().ViewPicture(dataItem.Section.ApplicationKey, cSContext.CategoryID, dataItem);
					}

				switch(SortBy)
				{
					case GalleryThreadSortBy.ThreadDate:
						User user = cSContext.User;
						DateTime postDate = dataItem.PostDate;
						string dateFormat;

						if(!user.IsAnonymous)
						{
							postDate = user.GetTimezone(dataItem.PostDate);
							dateFormat = user.Profile.DateFormat;
						}
						else
							dateFormat = cSContext.SiteSettings.DateFormat;

						if (fieldName != null)
							fieldName.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_FieldPostDate" );

						if (field != null)
							field.Text = postDate.ToString(dateFormat) + " " + postDate.ToString(cSContext.SiteSettings.TimeFormat);

						break;

					case GalleryThreadSortBy.Rating:
						if (fieldName != null)
							fieldName.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_FieldRating" );

						if (field != null)
							field.Text = dataItem.RatingAverage.ToString("0.0#");

						break;

					case GalleryThreadSortBy.Comments:
						if (fieldName != null)
							fieldName.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_FieldReplies" );
						
						if (field != null)
							field.Text = dataItem.Replies.ToString();

						break;
					case GalleryThreadSortBy.Views:

						if (fieldName != null)
							fieldName.Text = ResourceManager.GetString( "Gallery_PortalPictureListing_FieldViews" );

						if (field != null)
							field.Text = dataItem.Views.ToString();

						break;
				}

					break;
			}
		}

		private void pictureList_NoneItemsDataBound(object sender, RepeaterItemEventArgs e)
		{

		}
	}
}
