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

using System;
using System.Web.UI.WebControls;
using CommunityServer.Galleries.Components;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Enables the management of email subscriptions to an individual Gallery
	/// </summary>
	public class EmailSubscriptionList : GalleryThemedControl
	{

		const string urlFormat = "<a href=\"{0}\">{1}</a>";

		/// <summary>
		/// Validates we only allow authenticated users to see this page
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			if(!Context.Request.IsAuthenticated)
			{
				Context.Response.Redirect(Globals.GetSiteUrls().Login);
				Context.Response.End();
			}
			base.OnInit (e);
		}

		#region Child Controls

		private RadioButtonList GlobalList = null;
		private CheckBoxList PostList = null;
		private IButton UnSubGlobal = null;
		private IButton UnSubAll = null;
		private IButton UnSub = null;
		private ResourceControl NoSubscriptionsMessage = null;

		#endregion

		#region Skin

		/// <summary>
		/// Wire up controls
		/// </summary>
		protected override void AttachChildControls()
		{
			GlobalList = FindControl("GlobalList") as RadioButtonList;
			PostList = FindControl("PostList") as CheckBoxList;
			UnSubGlobal = FindButton("UnSubGlobal");
			UnSubAll = FindButton("UnSubAll");
			UnSub = FindButton("UnSub");
			NoSubscriptionsMessage  = FindControl("NoSubscriptionsMessage") as ResourceControl;

			UnSubGlobal.Click +=new EventHandler(UnSubGlobal_Click);
			UnSubAll.Click +=new EventHandler(UnSubAll_Click);
			UnSub.Click +=new EventHandler(UnSub_Click);

			this.SetPageTitle(ResourceManager.GetString("Gallery_Email_Subscriptions"));
			UnSubGlobal.Text = ResourceManager.GetString("Gallery_Email_GlobalButton");
			UnSubAll.Text = ResourceManager.GetString("Gallery_Email_UnSubAllButton");
			UnSub.Text = ResourceManager.GetString("Gallery_Email_UnSubButton");
		}

		#endregion

		#region Event Overrides

		//Wire up databinding
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if(!Page.IsPostBack)
				DataBind();
		}

		public override void DataBind()
		{
			base.DataBind ();
			BindData();
		}

		#endregion

		private void BindData()
		{
			PostSet ps = GalleryPosts.GetTrackedThreadsByUser(CurrentGallery.SectionID,CurrentUser.UserID);
			PostList.Items.Clear();
			foreach(GalleryPost post in ps.Posts)
			{
				PostList.Items.Add(new ListItem(BuildUrl(post), post.ThreadID.ToString()));
			}
			SetMessage();

			GlobalList.Items.Clear();
			GlobalList.Items.Add(new ListItem(ResourceManager.GetString("Gallery_Email_Never"), EmailSubscriptionType.None.ToString()));
			GlobalList.Items.Add(new ListItem(ResourceManager.GetString("Gallery_Email_NewPosts"), EmailSubscriptionType.Thread.ToString()));
			GlobalList.Items.Add(new ListItem(ResourceManager.GetString("Gallery_Email_NewPostsandComments"), EmailSubscriptionType.Post.ToString()));

			EmailSubscriptionType subType = ThreadTracking.GetSectionSubscriptionType(CurrentGallery.SectionID, CurrentUser.UserID);

			ListItem li = GlobalList.Items.FindByValue(subType.ToString());
			if(li != null)
				li.Selected = true;

		}

		private void SetMessage()
		{
			bool HasTrackedItems = (PostList.Items.Count > 0);
			
			NoSubscriptionsMessage.Visible = !HasTrackedItems;

			if(!HasTrackedItems)
			{
				UnSub.Attributes.Add("disabled","disabled");
				UnSubAll.Attributes.Add("disabled","disabled");
			}


		}


		private void UnSubGlobal_Click(object sender, EventArgs e)
		{

			EmailSubscriptionType subType = (EmailSubscriptionType)Enum.Parse(typeof(EmailSubscriptionType),GlobalList.SelectedValue);
			ThreadTracking.SetSectionSubscriptionType(CurrentGallery.SectionID,CurrentUser.UserID,subType);

		}

		private void UnSubAll_Click(object sender, EventArgs e)
		{
			if(PostList.Items.Count > 0)
			{
				ThreadTracking.RemoveThreadTracking(CurrentGallery.SectionID,CurrentUser.UserID);
				PostList.Items.Clear();
				SetMessage();
			}
		}

		private void UnSub_Click(object sender, EventArgs e)
		{
			if(PostList.Items.Count > 0)
			{
				//walk the list backwards
				for(int i = PostList.Items.Count -1; i >= 0; i--)
				{
					ListItem li = PostList.Items[i];
					if(li.Selected)
					{
						ThreadTracking.ReverseThreadTrackingOptions(Int32.Parse(li.Value), CurrentUser.UserID);
						PostList.Items.Remove(li);
					}
				}
				SetMessage();
			}

		}

		private string BuildUrl(GalleryPost post)
		{
			return string.Format(urlFormat,GalleryUrls.Instance().ViewPicture(CurrentGallery.ApplicationKey, post) , post.Subject);
		}



	}
}
