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

using System;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Controls
{
	/// <summary>
	/// Summary description for AgedPosts.
	/// </summary>
	public class AgedPosts : WeblogBaseTemplatedWebControl
	{
		Repeater posts = null;
		int postCount = 0;

		#region Properties
		private int _postsToDisplay  = 6;
		public int PostsToDisplay
		{
			get
			{
				return _postsToDisplay;
			}
			set
			{
				_postsToDisplay = value;
			}
		}

		private int _postsToDisplayFull = 1;
		public int PostsToDisplayFull
		{
			get
			{
				return _postsToDisplayFull;
			}
			set
			{
				_postsToDisplayFull = value;
			}
		}

		private int _fullLength = 1500;
		public int FullLength
		{
			get
			{
				return _fullLength;
			}
			set
			{
				_fullLength = value;
			}
		}

		private int _postsToTruncate  = 2;
		public int PostsToTruncate
		{
			get
			{
				return _postsToTruncate;
			}
			set
			{
				_postsToTruncate = value;
			}
		}

		private int _truncateLength  = 500;
		public int TruncateLength
		{
			get
			{
				return _truncateLength;
			}
			set
			{
				_truncateLength = value;
			}
		}

		string _applicationKey = null;
		public string ApplicationKey 
		{
			get
			{
				return _applicationKey;
			}
			set
			{
				_applicationKey = value;
			}
		}
		string _fullSectionHeader = null;
		public string FullSectionHeader
		{
			get
			{
				return _fullSectionHeader;
			}
			set
			{
				_fullSectionHeader = value;
			}
		}
		string _truncatedSectionHeader = null;
		public string TruncatedSectionHeader
		{
			get
			{
				return _truncatedSectionHeader;
			}
			set
			{
				_truncatedSectionHeader = value;
			}
		}
		string _linkSectionHeader = null;
		public string LinkSectionHeader
		{
			get
			{
				return _linkSectionHeader;
			}
			set
			{
				_linkSectionHeader = value;
			}
		}

		#endregion

		protected override void AttachChildControls()
		{
			posts = FindControl("posts") as Repeater;

			posts.ItemDataBound += new RepeaterItemEventHandler(posts_ItemDataBound);

			BlogThreadQuery query = BlogThreadQuery.CreateNewAggregateQuery();

			query.BlogPostType = BlogPostType.Post;
			
			if (!Globals.IsNullorEmpty(this.ApplicationKey))
			{
				Weblog blog = Weblogs.GetWeblog(this.ApplicationKey);
				query.SectionID = blog.SectionID;
			}

			query.IncludeCategories = false;
			query.PageIndex = 0;
			query.PageSize = _postsToDisplay;
			query.FirstPageOnly = true;
			query.PostConfig = BlogPostConfig.IsCommunityAggregated | BlogPostConfig.IsAggregated;

			ThreadSet ts = WeblogPosts.GetBlogThreads(query, true, true);

			if (posts == null)
				return;
			posts.DataSource = ts.Threads;
			posts.DataBind();
		}

		private void posts_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				WeblogPost post = e.Item.DataItem as WeblogPost;

				HyperLink titleLink = e.Item.FindControl("TitleLink") as HyperLink;
				if (titleLink != null)
				{
					titleLink.Text = post.Subject;
					titleLink.NavigateUrl = BlogUrls.Instance().Post(post);
					titleLink.ToolTip = post.Subject;
				}

				HyperLink User = e.Item.FindControl("User") as HyperLink;
				if (User != null)
				{
					User.Text = Users.GetUser(post.AuthorID, false).DisplayName;
					User.NavigateUrl = SiteUrls.Instance().UserProfile(post.AuthorID);
				}

				Literal Posted = e.Item.FindControl("Posted") as Literal;
				if (Posted != null) 
				{
					Posted.Text = Formatter.FormatAgoDate(post.PostDate);
				}

				PlaceHolder SectionHeader = e.Item.FindControl("SectionHeader") as PlaceHolder;
				if (postCount == 0 && this.PostsToDisplayFull > 0)
				{
					Literal SectionTitle = e.Item.FindControl("SectionTitle") as Literal;
					if (SectionTitle != null)
					{
						SectionTitle.Text = this.FullSectionHeader;
					}
				}
				if (postCount == this.PostsToDisplayFull && this.PostsToTruncate > 0)
				{
					Literal SectionTitle = e.Item.FindControl("SectionTitle") as Literal;
					if (SectionTitle != null)
					{
						SectionTitle.Text = this.TruncatedSectionHeader;
					}
				}
				if (postCount == this.PostsToDisplayFull + this.PostsToTruncate)
				{
					Literal SectionTitle = e.Item.FindControl("SectionTitle") as Literal;
					if (SectionTitle != null)
					{
						SectionTitle.Text = this.LinkSectionHeader;
					}
				}

				Literal body = e.Item.FindControl("body") as Literal;
				HyperLink readMore = e.Item.FindControl("ReadMore") as HyperLink;
				if(body != null)
				{
					if (postCount < _postsToDisplayFull + _postsToTruncate)
					{
						string bodyText = string.Empty;
                
						if (postCount < _postsToDisplayFull)
						{
							bodyText = Formatter.RemoveHtml(post.FormattedBody, _fullLength);
						}
						else
						{
							bodyText = Formatter.RemoveHtml(post.FormattedBody, _truncateLength);
						}

						if (bodyText.Length < post.FormattedBody.Length)
						{
							bodyText += "...";
							readMore.Visible = true;
							readMore.NavigateUrl = BlogUrls.Instance().Post(post);
						}
						else
						{
							readMore.Visible = false;
						}

						body.Text = bodyText;
					}
					else
					{
						readMore.Visible = false;
					}
				}

				postCount++;
			}
		}
	}
}
