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

using CommunityServer.Components;

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Provides consistent/simple access to the various blog thread views
	/// </summary>
	public class BlogThreadQuery : ThreadQuery
	{

		public BlogThreadQuery() {}

        public static BlogThreadQuery CreateNewAggregateQuery()
        {
            BlogThreadQuery query = CommonQuery();
            
            //Disabled in CS 2.1 since it is not fully wired up.
            //CS 2.2 UI updates will make this easier to enforce
//            if(WeblogConfiguration.Instance().DisableSecurityFilter)
//            {
//                query.FilterByActive = true;
//            }
//            else
//            {
                query.FilterByList = Sections.FilterByAccessControl(Weblogs.GetWeblogs(true,false,false,true),Permission.View);
//            }
            return query;
        }

        public static BlogThreadQuery CreateNewSingleBlogQuery(int sectionid)
        {
            BlogThreadQuery query = CommonQuery();
            query.SectionID = sectionid;
            query.RequireSectionIsActive = false;

            return query;
        }

        private static BlogThreadQuery CommonQuery()
        {
            BlogThreadQuery query = new BlogThreadQuery();
            query.PublishedFilter = BlogPostPublishedFilter.Published;
            query.BlogThreadType = BlogThreadType.Recent;
            query.SortBy = BlogThreadSortBy.MostRecent;
            query.SortOrder = SortOrder.Descending;
            query.IncludeCategories = true;

            return query;
        }

        #region Query Properties

        /// <summary>
        /// Require posts be marked as Published/Active
        /// </summary>
        public BlogPostPublishedFilter PublishedFilter = BlogPostPublishedFilter.All;


		/// <summary>
        /// Types of post to filter by
        /// </summary>
		public BlogPostType BlogPostType = BlogPostType.Post;

        /// <summary>
        /// Type of thread (date, recent, etc)
        /// </summary>
        public BlogThreadType BlogThreadType = BlogThreadType.Recent;

        /// <summary>
        /// Column to sort by
        /// </summary>
        public BlogThreadSortBy SortBy = BlogThreadSortBy.MostRecent;

        /// <summary>
        /// Filter by specific PostConfigurations
        /// </summary>
        public BlogPostConfig PostConfig = BlogPostConfig.Empty;

        public BlogMirrorDisplayType BlogMirrorDisplayType = BlogMirrorDisplayType.AllBlogs;

        public bool FilterByActive = false;
		

        #endregion
		
		#region Has/Helpers
		/// <summary>
        /// Unquie key represeting the current query
        /// </summary>
        public string Key
        {
            get
            {
                return string.Format("B:{0}-P:{15}-PN:{16}-PI{1}-PS:{2}-C:{3}-DF:{4}-IP:{5}-BPT:{6}-BTT:{7}-BSB:{8}-SO:{9}-PC:{10}-IC:{11}-GID:{12}-F:{13}-IC:{14}-PAR:{17}-MPID:{18}-L:{19}-OT:{20}-T:{21}-DT{22}-Min:{23}-Max:{24}-Status:{25}",
                    SectionID,PageIndex,PageSize,CategoryID,DateFilter,PublishedFilter,BlogPostType,BlogThreadType,SortBy,SortOrder,PostConfig,IncludeCategories,GroupID,FilterKey,FirstPageOnly,PostID, PostName, ParentID, Globals.JoinIntArray(",", MultiplePostIDs), this.SectionID < 0 ? CSContext.Current.User.FilterLanguagesCacheKey : "", this.LogicallyOrTags.ToString(), this.FilterByTags ? System.String.Join("\n", this.Tags) : "", this.BlogMirrorDisplayType,MinimumSpamScore,MaximumSpamScore,this.PostStatus);

			}
        }

        #endregion

	}
}
