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

using System;
using System.Web.Caching;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Summary description for WeblogCommentRssHandler.
	/// </summary>
	public class WeblogCommentRssHandler : BaseWeblogSyndicationHandler
	{
		public WeblogCommentRssHandler()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		protected int PostID;

		protected override string CacheKey
		{
			get { return "CommentRss-PostID:" + PostID.ToString(); }
		}

		protected override void ProcessFeed()
		{
			PostID = Int32.Parse(Context.Request.QueryString["PostID"]);

			base.ProcessFeed ();
		}

        protected override int CacheTime
        {
            get
            {
                return 5;
               
            }
        }

        protected override CacheItemPriority Priority
        {
            get
            {
                return CacheItemPriority.Low;
            }
        }


		protected override CachedFeed BuildFeed()
		{
            BlogThreadQuery query = new BlogThreadQuery();
			query.PostID = PostID;
			query.ReturnFullThread = true;
			query.SectionID = this.CurrentWeblog.SectionID;
			query.PageSize = BlogConfig.IndividualPostCount;

			PostSet ps =  WeblogPosts.GetPosts(query,true);

			ps.Organize();

			WeblogPost we = ps.ThreadStarter as WeblogPost;

			//If we do not have a post, there is not much we can do
			if(we == null)
			{
				Context.Response.End();
				return null;
			}




			DateTime dt = (ps.Replies != null &&  ps.Replies.Count > 0) ? ((WeblogPost)(ps.Replies[ps.Replies.Count -1])).PostDate : we.PostDate;

			WeblogCommentRssWriter writer = new WeblogCommentRssWriter(we,ps.Replies,this.CurrentWeblog,this.BaseUrl);
			return new CachedFeed(dt,dt.ToString(),writer.GetXml());
		}
	}
}
