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

using System;
using System.Collections;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Summary description for AggregateRssHandler.
	/// </summary>
	public class AggregateRssHandler : BaseGallerySyndicationHandler
	{
		public AggregateRssHandler()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        protected Group CurrentGroup = null;

        protected bool HasGroup
        {
            get
            {
                return CurrentGroup != null;
            }
        }

        protected override void ProcessFeed()
        {
            int groupID = Globals.SafeInt(Context.Request.QueryString["GroupID"],-1);
            if(groupID > -1)
                CurrentGroup = GalleryGroups.GetGroup(groupID,true,false,false);

            base.ProcessFeed ();
        }

        protected override Section GetCurrentSection
        {
            get
            {
                return null;
            }
        }


	    protected override string CacheKey
	    {
	        get
	        {
                if(!HasGroup)
                    return string.Format("GalleryAggregate:{0}:IndividualRss",CSContext.Current.SiteSettings.SettingsID);
                else
                    return string.Format("GalleryAggregate:{0}:GroupID{1}",CSContext.Current.SiteSettings.SettingsID,CurrentGroup.GroupID);
	        }
	    }

	    protected override CachedFeed BuildFeed()
	    {
            GalleryThreadQuery query = new GalleryThreadQuery();
			query.ApplicationPostType = GalleryPostType.Image;
			query.IncludeCategories = true;
            query.PageIndex = 0;
            query.PageSize = GalleryConfiguration.Instance().AggregatePostCount;
			query.FirstPageOnly = false;
            query.SortBy = GalleryThreadSortBy.PictureDate;
            query.SortOrder = SortOrder.Descending;
            query.PublishedFilter = GalleryPostPublishedFilter.Published;
			//query.PostConfig = BlogPostConfig.IsCommunityAggregated | BlogPostConfig.IsAggregated;
			query.FilterByList = Sections.FilterByAccessControl(Galleries.GetGalleries(true,false),Permission.View);
           
            if(HasGroup)
                query.GroupID = CurrentGroup.GroupID;

            ThreadSet threads = GalleryPosts.GetPictures(query,true);

			// Filter out any posts that belong to a blog that is not community aggregated
			ArrayList posts = threads.Threads;

			BaseRssWriter writer = null;
            writer = new AggregateRssWriter(posts,this.CurrentGroup,this.BaseUrl);
           
            DateTime dt =  posts.Count > 0 ? ((GalleryPost)posts[0]).PostDate : DateTime.Now;
            
            return new CachedFeed(dt,null,writer.GetXml());
	    }
	}
}
