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

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

namespace CommunityServer.Blogs.Controls
{
	/// <summary>
	/// Summary description for RecentPosts.
	/// </summary>
	public class RecentPosts : WeblogThemedControl
	{
		public RecentPosts()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        private Repeater postList = null;

	    protected override void AttachChildControls()
	    {
	        postList = FindControl("PostList") as Repeater;
            if(postList != null)
            {
                postList.ItemDataBound +=new RepeaterItemEventHandler(postList_ItemDataBound);

                BlogThreadQuery query = BlogThreadQuery.CreateNewSingleBlogQuery(CurrentWeblog.SectionID);
                query.BlogPostType = BlogPostType.Post;
                query.PostConfig = BlogPostConfig.IsAggregated;
                query.FirstPageOnly = true;

                //we want to make sure a query is the same as the home page query so we can reuse the cache
                query.PageSize = WeblogConfiguration.Instance().IndividualPostCount;

                ThreadSet ts = WeblogPosts.GetBlogThreads(query,true,false);
                if(ts == null || ts.Threads.Count == 0)
                {
                    this.Controls.Clear();
                    this.Visible = false;
                }
                else
                {
                    if(ts.Threads.Count > PostCount)
                    {
                        WeblogPost[] posts = new WeblogPost[PostCount];
                        ts.Threads.CopyTo(0,posts,0,PostCount);

                        postList.DataSource = posts;
                    }
                    else
                        postList.DataSource = ts.Threads;

                    postList.DataBind();
                }
            }
	    }

        [
        System.ComponentModel.DefaultValue( 5 ),
        ]
        public virtual Int32 PostCount 
        {
            get 
            {
                Object state = ViewState[ "PostCount" ];
                if ( state != null ) 
                {
                    return (Int32)state;
                }
                return 5;
            }
            set 
            {
                ViewState[ "PostCount" ] = value;
            }
        }

        [
        System.ComponentModel.DefaultValue( 100 ),
        ]
        public virtual Int32 PostLength 
        {
            get 
            {
                Object state = ViewState[ "PostLength" ];
                if ( state != null ) 
                {
                    return (Int32)state;
                }
                return 100;
            }
            set 
            {
                ViewState[ "PostLength" ] = value;
            }
        }

        private void postList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                WeblogPost post = e.Item.DataItem as WeblogPost;
                if(post != null)
                {
                    HyperLink link = e.Item.FindControl("Link") as HyperLink;
                    if(link != null)
                    {
                        link.NavigateUrl = BlogUrls.Instance().Post(post,this.CurrentWeblog);
                        link.Text = Formatter.MaxLength(post.Subject,PostLength);
                    }
                }
            }
        }
    }
}
