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

using System;
using System.Collections;

namespace CommunityServer.Components {

    /// <summary>
    /// Summary description for UserSet.
    /// </summary>
    [Serializable]
    public class PostSet {

        ArrayList posts = new ArrayList();
        ArrayList replies = null;
        Post post =null;
        int totalRecords = 0;
		Hashtable postsByParentID = null;

        public int TotalRecords {
            get {
                return totalRecords;
            }
            set {
                totalRecords = value;
            }
        }

        public ArrayList Posts {
            get {
                return posts;
            }
        }

        public bool HasResults {
            get {
                if (posts.Count > 0)
                    return true;
                return false;
            }
        }

        public Post ThreadStarter
        {
            get{return post;}
        }

        public ArrayList Replies
        {
            get{ return replies;}
        }

        public bool Organize()
        {
            if(replies == null)
            {
                replies = new ArrayList();
                foreach(Post p in posts)
                {
                    if(p.PostLevel != 1)
                    {
                        replies.Add(p);
                    }
                    else
                    {
                        post = p;
                    }
                }
            }
            //No need to use the organized posts if the main
            //partent can not be found
            return ThreadStarter != null;
        }

		public Post GetTopPost()
		{
			return (Post) PostsByParentID[0];
		}

		public ArrayList GetPostsByParent(int parentID)
		{
			ArrayList p = (ArrayList) PostsByParentID[parentID];
			if (p == null)
				return new ArrayList();
			else
				return p;
		}

		public Hashtable PostsByParentID
		{
			get
			{
				if (postsByParentID == null)
				{
					postsByParentID = new Hashtable();

					ArrayList posts;
					int parent;
					foreach (Post p in this.Posts)
					{
						if (p.ParentID == p.PostID)
							parent = 0;
						else
							parent = p.ParentID;

						posts = (ArrayList) postsByParentID[parent];
						if (posts == null)
						{
							posts = new ArrayList();
							postsByParentID[parent] = posts;
						}
						posts.Add(p);
					}
				}

				return postsByParentID;
			}
		}
        
    }
}
