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

using System.Collections;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Provides access to Groups of Weblogs
	/// </summary>
	public class WeblogGroups
	{
        /// <summary>
        /// Private. Can not be instantiated
        /// </summary>
		private WeblogGroups()
		{}

        public static bool IsPublic(int groupID)
        {
            ArrayList list =  Weblogs.GetWeblogsByGroupID(groupID,true,true,false);
            return Sections.FilterByAccessControl(list,Permission.View,Users.GetAnonymousUser(true)).Count > 0;
        }

		/// <summary>
		/// Returns the specificied Group.
		/// </summary>
		public static Group GetWeblogGroup(int groupID, bool cacheable, bool ignorePermissions, bool flush)
		{
            ArrayList blogGroups = GetWeblogGroups(cacheable,ignorePermissions,flush);
            foreach(Group fg in blogGroups)
            {
                if(fg.GroupID == groupID)
                    return fg;
            }
            return null;
		}
        /// <summary>
        /// Returns the specificied Group.
        /// </summary>
        public static Group GetWeblogGroup(int groupID, bool cacheable, bool ignorePermissions)
        {
			return GetWeblogGroup(groupID, cacheable, ignorePermissions, false);
        }

        /// <summary>
        /// Returns a list of Weblog Groups the specific user has access to (access is 
        /// defined as one of the blog is visible to the user)
        /// </summary>
        public static ArrayList GetWeblogGroups(bool cacheable, bool ignorePermissions, bool flush)
        {
            CSContext csContext = CSContext.Current;
            ArrayList blogGroups = new ArrayList();
            Hashtable blogGroupsTable;

            // Get the WeblogGroups as a hashtable
            //
            blogGroupsTable = GetWeblogGroups(csContext, cacheable, ignorePermissions,flush);

            // Hide weblog groups based on the user's cookie
            //
            blogGroupsTable = Groups.HideGroups(blogGroupsTable);

            // Convert the hashtable to an arraylist
            //
            foreach(Group fg in blogGroupsTable.Values)
                blogGroups.Add( fg );

            // Sort the weblog groups
            //
            blogGroups.Sort();

            return blogGroups;
        }

        /// <summary>
        /// Internal method for looking up all of the blog group data.
        /// </summary>
        private static Hashtable GetWeblogGroups(CSContext csContext, bool cacheable, bool ignorePermissions, bool flush) 
        {
            int settingsID = CSContext.Current.SiteSettings.SettingsID;
            string cacheKey = string.Format("AllBlogGroups{0}-Cacheable:{1}-IgnorePermissions:{2}",settingsID,cacheable,ignorePermissions);

			if(flush)
			{
				csContext[cacheKey] = null;
				CSCache.Remove(cacheKey);
			}

            Hashtable unfilteredWeblogs;
            Hashtable blogGroups;

            // Evict item from cache if not cacheable
            //
            if (!cacheable)
                CSCache.Remove(cacheKey);

			unfilteredWeblogs = csContext[cacheKey] as Hashtable;
			if(unfilteredWeblogs != null)
				return unfilteredWeblogs;

            // Get the raw weblog groups
            //
            unfilteredWeblogs = CSCache.Get(cacheKey) as Hashtable;
            if ( unfilteredWeblogs == null ) 
            {
                unfilteredWeblogs =  CommonDataProvider.Instance().GetGroups(ApplicationType.Weblog, false);

                if (cacheable)
                    CSCache.Insert(cacheKey,unfilteredWeblogs,CSCache.MinuteFactor * 3);

            } 

            // Are we ignoring permissions?
            //
			if (ignorePermissions)
			{
				csContext[cacheKey] = unfilteredWeblogs;
				return unfilteredWeblogs;
			}

            // We need to create a new hashtable
            //
            blogGroups = new Hashtable();

            // Filter the list of forum groups to only show weblog groups
            // this user is allowed to see, i.e. if all the weblog in this
            // group are private and the user is not allowed to see them
            //
            foreach (Group group in unfilteredWeblogs.Values) 
            {

                // Get the associated weblogs
                //
                if (!cacheable) 
                    blogGroups.Add(group.GroupID, group);
                else if (Weblogs.GetWeblogsByGroupID(group.GroupID,cacheable, ignorePermissions,false).Count > 0)
                    blogGroups.Add(group.GroupID, group);

            }

			csContext[cacheKey] = blogGroups;

            return blogGroups;

        }

		/// <summary>
		/// Returns true if the Group has Sections
		/// </summary>
		public static bool HasChildSections(int groupID)
		{
			return Weblogs.GetWeblogsByGroupID(groupID,true,true,true).Count > 0;
		}

	}
}
