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

using System;
using System.Data.SqlTypes;
using CommunityServer.Components.Enumerations;

namespace CommunityServer.Components 
{
	/// <summary>
	/// Summary description for PostCategories.
	/// </summary>
	public class PostCategory : IComparable
	{

		#region Private Members

		private int _categoryID;
		private int _featuredPostID =-1;
		private int _sectionID;
		private string _name;
		private bool _isEnabled;
		private int _parentID;
		private string _description;
		private string _path;
		private DateTime _dateCreated = SqlDateTime.MinValue.Value;

		// Derived properties
		private int _totalThreads;
		private int _totalSubThreads;
		private Post _featuredPost;
		private DateTime _mostRecentPostDate = SqlDateTime.MinValue.Value;
		private DateTime _mostRecentSubPostDate = SqlDateTime.MinValue.Value;

		#endregion


		#region Constructor

		public PostCategory() 
		{ }

		#endregion


		#region Public Properties

		public int CategoryID
		{
			get { return _categoryID; }
			set { _categoryID = value; }
		}

		public int SectionID
		{
			get { return _sectionID; }
			set { _sectionID = value; }
		}

		public string Name
		{
			get { return _name; }
			set { _name = value; }
		}

		public bool IsEnabled
		{
			get { return _isEnabled; }
			set { _isEnabled = value; }
		}

		public int ParentID
		{
			get { return _parentID; }
			set { _parentID = value; }
		}

		public int FeaturedPostID
		{
			get { return _featuredPostID; }
			set
			{
				if(value < 0)
				{
					value = -1;
					_featuredPost = null;
				}
				_featuredPostID = value;
			}
		}

		public virtual bool HasFeaturedPost
		{
			get{return (_featuredPostID != -1);}
		}

		public virtual Post FeaturedPost
		{
			get{return _featuredPost;}
			set
			{
				if(value != null && value.PostID > 0)
				{
					FeaturedPostID = value.PostID;
					_featuredPost = value;
				}
				else
				{
					FeaturedPostID = -1;
					_featuredPost = null;
				}
			}
		}
		
		public string Description
		{
			get { return _description; }
			set { _description = value; }
		}

		public DateTime DateCreated
		{
			get { return _dateCreated; }
			set { _dateCreated = value; }
		}

		public int TotalThreads
		{
			get { return _totalThreads; }
			set { _totalThreads = value; }
		}

		public int TotalSubThreads
		{
			get { return _totalSubThreads; }
			set { _totalSubThreads = value; }
		}

		public DateTime MostRecentPostDate
		{
			get { return _mostRecentPostDate; }
			set { _mostRecentPostDate = value; }
		}

		public DateTime MostRecentSubPostDate
		{
			get { return _mostRecentSubPostDate; }
			set { _mostRecentSubPostDate = value; }
		}

		public string Path
		{
			get { return _path; }
			set { _path = value; }
		}


		#endregion


		#region IComparable Members

		private static PostCategorySortBy sortBy = PostCategorySortBy.Name;
		public static PostCategorySortBy SortBy
		{
			get{return sortBy;}
			set{sortBy = value;}
		}

		public int CompareTo(object obj)
		{
			if(!(obj is PostCategory))
				throw new ArgumentException("Specified object is not of type PostCategory");
			
			PostCategory category = (PostCategory)obj;
			switch(sortBy)
			{
				case PostCategorySortBy.Name:
					return _name.CompareTo(category._name);
				case PostCategorySortBy.NameDesc:
					return ((_name.CompareTo(category._name))*-1);
				case PostCategorySortBy.MostRecentPostDate:
					return _mostRecentPostDate.CompareTo(category._mostRecentPostDate);
				case PostCategorySortBy.MostRecentPostDateDesc:
					return ((_mostRecentPostDate.CompareTo(category._mostRecentPostDate))*-1);
				case PostCategorySortBy.TotalThreads:
					return _totalThreads.CompareTo(category._totalThreads);
				case PostCategorySortBy.TotalThreadsDesc:
					return ((_totalThreads.CompareTo(category._totalThreads))*-1);
				default:
					goto case PostCategorySortBy.Name;
			}
		}

		#endregion
	}
}
