//------------------------------------------------------------------------------
// <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 SectionSorter.
	/// </summary>
	public class SectionSorter : IComparer
	{
		private SectionSortBy _sortBy = SectionSortBy.SortOrder;
		private SortOrder _sortOrder = SortOrder.Ascending;

		public SectionSorter() : this(SectionSortBy.SortOrder, SortOrder.Ascending)
		{
		}

		public SectionSorter(SectionSortBy sortBy) : this(sortBy, SortOrder.Ascending)
		{
		}

		public SectionSorter(SectionSortBy sortBy, SortOrder sortOrder)
		{
			_sortBy = sortBy;
			_sortOrder = sortOrder;
		}

		public int Compare(object x, object y)
		{
			Section first = (_sortOrder == SortOrder.Ascending ? x : y) as Section;
			Section second = (_sortOrder == SortOrder.Ascending ? y : x) as Section;

			switch (_sortBy)
			{
				case SectionSortBy.LastPost:
					return first.MostRecentPostDate.CompareTo(second.MostRecentPostDate);
				case SectionSortBy.Name:
					return first.Name.CompareTo(second.Name);
				case SectionSortBy.Post:
					return first.TotalPosts.CompareTo(second.TotalPosts);
				case SectionSortBy.Thread:
					return first.TotalThreads.CompareTo(second.TotalThreads);
				default:
					return first.SortOrder.CompareTo(second.SortOrder);
			}
		}
	}
}
