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

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

namespace CommunityServer.Blogs.Controls
{
	/// <summary>
	/// Enables the management of email subscriptions to an individual weblog
	/// </summary>
	public class EmailSubscriptionList : WeblogThemedControl
	{
		public EmailSubscriptionList()
		{
			
		}

		/// <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 Private Members
		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

		/// <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("Weblog_Email_Subscriptions"));
			UnSubGlobal.Text = ResourceManager.GetString("Weblog_Email_GlobalButton");
			UnSubAll.Text = ResourceManager.GetString("Weblog_Email_UnSubAllButton");
			UnSub.Text = ResourceManager.GetString("Weblog_Email_UnSubButton");
		}

		const string urlFormat = "<a href=\"{0}\">{1}</a>";
		private string BuildUrl(WeblogPost post)
		{
			return string.Format(urlFormat,BlogUrls.Instance().Post(post,CurrentWeblog), post.Subject);
		}

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

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

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

            PostSet ps = WeblogPosts.GetTrackedThreadsByUser(CurrentWeblog.SectionID,CurrentUser.UserID);
            PostList.Items.Clear();
            foreach(WeblogPost 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("Weblog_Email_Never"), EmailSubscriptionType.None.ToString()));
            GlobalList.Items.Add(new ListItem(ResourceManager.GetString("Weblog_Email_NewPosts"), EmailSubscriptionType.Thread.ToString()));
            GlobalList.Items.Add(new ListItem(ResourceManager.GetString("Weblog_Email_NewPostsandComments"), EmailSubscriptionType.Post.ToString()));

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

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

        }


		private void SetMessage()
		{
			if (PostList.Items.Count > 0)
			{
				NoSubscriptionsMessage.Visible = false;
			}
			else
			{
				NoSubscriptionsMessage.Visible = true;
				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(CurrentWeblog.SectionID,CurrentUser.UserID,subType);

		}

		private void UnSubAll_Click(object sender, EventArgs e)
		{
			if(PostList.Items.Count > 0)
			{
				ThreadTracking.RemoveThreadTracking(CurrentWeblog.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();
			}

		}

	}
}
