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

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Components.Enumerations;

namespace CommunityServer.Controls 
{
    
	public class PostCategoriesSortDropDownList : DropDownList 
	{

		public PostCategoriesSortDropDownList()
		{
			GenerateList();
		}

		public void GenerateList()
		{

			Items.Clear() ;
			// Add the sort items
			Items.Add(GenerateListItem(PostCategorySortBy.Name, SortOrder.Ascending));
			Items.Add(GenerateListItem(PostCategorySortBy.NameDesc, SortOrder.Descending));
			Items.Add(GenerateListItem(PostCategorySortBy.MostRecentPostDate, SortOrder.Ascending));
			Items.Add(GenerateListItem(PostCategorySortBy.MostRecentPostDateDesc, SortOrder.Descending));
			Items.Add(GenerateListItem(PostCategorySortBy.TotalThreads, SortOrder.Ascending));
			Items.Add(GenerateListItem(PostCategorySortBy.TotalThreadsDesc , SortOrder.Descending));

		}

		private ListItem GenerateListItem(PostCategorySortBy sortBy, SortOrder sortOrder)
		{
			return new ListItem( ResourceManager.GetString("GalleryCategoriesSortDropDownList_" + sortBy.ToString() + "_" + sortOrder.ToString() ), sortBy.ToString() + "_" + sortOrder.ToString()); 
		}

		public PostCategorySortBy SelectedSortByValue 
		{
			get
			{
				string selectedValue = base.SelectedValue.Substring(0, base.SelectedValue.IndexOf("_"));
				return (PostCategorySortBy)Enum.Parse(typeof(PostCategorySortBy), selectedValue);
			}
			set 
			{ 
				// Deselect current item
				ListItem oldItem;
				ListItem newItem;

				oldItem = Items.FindByValue( base.SelectedValue );
				newItem = Items.FindByValue( value.ToString() + "_" + SelectedSortOrderValue.ToString() );

				if(oldItem != null)
					oldItem.Selected = false;

				if(newItem == null)
					newItem = Items.FindByValue( PostCategorySortBy.Name.ToString() + "_" + SortOrder.Ascending.ToString() );

				newItem.Selected = true;
			}
		}

		public SortOrder SelectedSortOrderValue 
		{
			get
			{
				string selectedValue = base.SelectedValue.Substring(base.SelectedValue.IndexOf("_")+1);
				return (SortOrder)Enum.Parse(typeof(SortOrder), selectedValue);
			}
			set 
			{ 
				// Deselect current item
				ListItem oldItem;
				ListItem newItem;

				oldItem = Items.FindByValue( base.SelectedValue );
				newItem = Items.FindByValue( SelectedSortByValue.ToString() + "_" + value.ToString() );

				if(oldItem != null)
					oldItem.Selected = false;

				if(newItem == null)
					newItem = Items.FindByValue( PostCategorySortBy.Name.ToString() + "_" + SortOrder.Ascending.ToString() );

				newItem.Selected = true;
			}
		}

	}
}
