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

using System;
using System.Collections;
using System.Collections.Specialized;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{


    /// <summary>
    /// Provides a forum specific implementation of SectionRepeater
    /// </summary>
    public class ForumRepeater : SectionRepeater
    {
		bool personalized = false;
		bool allowPersonalization = true;
		CSContext csContext = CSContext.Current;

        /// <summary>
        /// Returns the Valid Sections based on the Mode setting
        /// </summary>
        /// <returns></returns>
        protected override ArrayList GetSections()
        {
            ArrayList forums = null;

			//NOTE: ScottW >> Remove try/catch that through SectionNotFound excpetion on every exception.

            // Bind the repeater to the collection returned by the GetForums()
            //
                // Are we in normal mode or moderation mode?
                //
                switch ( this.Mode ) 
                {

                    case ControlUserMode.Moderator:
                        forums = Moderate.GetForumsByForumGroupID (GroupID);
                        break;

                    default:
                        // Do we have a forumID specified?
                        //
                        if ( (GroupID == -1) && (csContext.SectionID > 0) ) 
                        {

                            // Does the specified forum have any sub-forums?
                            //
                            Forum forum = (Forum) Forums.GetForum(csContext.SectionID);

                            if (forum != null && forum.Sections.Count > 0) 
                            {

                                // Get the forums for the named group
                                //
                                forum = Forums.GetForum(csContext.SectionID);

                                // Now get the sub-forums for the current forum
                                //
                                forums = forum.Sections;
                            }

                        } 
                        else if (GroupID == -1) 
                        {
                            return new ArrayList();
                        } 
                        else 
                        {
                            if ( Mode == ControlUserMode.Administrator) 
                            {
                                forums = Forums.GetForumsByForumGroupID(GroupID, false, IgnorePermissions, this.FlushSections);
                            } 
                            else 
                            {
                                forums = Forums.GetForumsByForumGroupID(GroupID, true, IgnorePermissions);
                            }
                        }
                        break;
                }

			// Apply user personalization
			return PersonalizeForumForUser( forums );
        }

		/// <summary>
		/// Users can specify a filter of sections they don't wish to see
		/// </summary>
		/// <param name="forums"></param>
		/// <returns></returns>
		private ArrayList PersonalizeForumForUser ( ArrayList forums ) 
		{
			// Return Cases
			if (null == forums)
				return forums;

			if (csContext.User.IsAnonymous)
				return forums;

			if (!AllowPersonalization || Context.Request["personalize"] == "true")
				return forums;

			// Get the user's filter list
			int[] filterList = csContext.User.SectionIgnoreFilter;

			// If there are no items to filter by, return
			if (filterList.Length <= 0)
				return forums;

			// Build a dictionary for faster lookups
			HybridDictionary sectionFilterLookupTable = new HybridDictionary();

			// Add the items to the lookup table
			for (int i = 0; i < filterList.Length; i++) 
			{
				if(!sectionFilterLookupTable.Contains(filterList[i]))
					sectionFilterLookupTable.Add(filterList[i], true);
			}

			// Perform filter
			ArrayList filteredForums = new ArrayList();
			foreach (Section s in forums) 
			{
				if (sectionFilterLookupTable[s.SectionID] == null) 
				{
					personalized = true;
					filteredForums.Add(s);
				}
		
			}

			// return filterd sections
			return filteredForums;
		}

		public bool Personalized 
		{
			get 
			{
				return 	personalized;
			}
		}

		public bool AllowPersonalization
		{
			get 
			{
				return allowPersonalization;
			}
			set 
			{
				allowPersonalization = value;
			}
		}

    }

}
