//------------------------------------------------------------------------------
// <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.Galleries.Components;

namespace CommunityServer.Galleries.Controls 
{
    
	public class GalleryThreadSortDropDownList : DropDownList 
	{

		public GalleryThreadSortDropDownList()
		{
			Gallery gallery = Galleries.GetGallery();

			if(gallery != null)
				GenerateList(gallery);
		}

		public GalleryThreadSortDropDownList(Gallery gallery)
		{
			GenerateList(gallery);
		}
		public void GenerateList(Gallery gallery)
		{
			Items.Clear() ;
			// Add the sort items
			Items.Add(GenerateListItem(GalleryThreadSortBy.ThreadDate, SortOrder.Ascending));
			Items.Add(GenerateListItem(GalleryThreadSortBy.ThreadDate, SortOrder.Descending));
			Items.Add(GenerateListItem(GalleryThreadSortBy.PictureDate, SortOrder.Ascending));
			Items.Add(GenerateListItem(GalleryThreadSortBy.PictureDate, SortOrder.Descending));
			Items.Add(GenerateListItem(GalleryThreadSortBy.Subject, SortOrder.Ascending));
			Items.Add(GenerateListItem(GalleryThreadSortBy.Subject, SortOrder.Descending));
			Items.Add(GenerateListItem(GalleryThreadSortBy.Views, SortOrder.Ascending));
			Items.Add(GenerateListItem(GalleryThreadSortBy.Views, SortOrder.Descending));

			if(gallery.EnableComments)
			{
				Items.Add(GenerateListItem(GalleryThreadSortBy.Comments, SortOrder.Ascending));
				Items.Add(GenerateListItem(GalleryThreadSortBy.Comments, SortOrder.Descending));
			}

			if(gallery.EnableRatings)
			{
				Items.Add(GenerateListItem(GalleryThreadSortBy.Rating, SortOrder.Ascending));
				Items.Add(GenerateListItem(GalleryThreadSortBy.Rating, SortOrder.Descending));
			}
		}

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

		public GalleryThreadSortBy SelectedSortByValue 
		{
			get
			{
				string selectedValue = base.SelectedValue.Substring(0, base.SelectedValue.IndexOf("_"));
				return (GalleryThreadSortBy)Enum.Parse(typeof(GalleryThreadSortBy), 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( GalleryThreadSortBy.ThreadDate.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( GalleryThreadSortBy.ThreadDate.ToString() + "_" + SortOrder.Ascending.ToString() );

				newItem.Selected = true;
			}
		}

	}
}
