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

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

namespace CommunityServer.Discussions.Controls
{
    /// <summary>
    /// This server control displays a list of Date formatting options.
    /// </summary>
	public class EmailNotificationDropDownList : DropDownList
	{
    	CSContext csContext = CSContext.Current;
        Label label = new Label();

        /// <remarks>
        /// Public constructor, internally populates the list of date formats.
        /// </remarks>
		public EmailNotificationDropDownList()
		{
            // Set the default text
            //
            //label.Text  = ResourceManager.GetString("EmailNotificationDropDownList_When");

            // Add datetime formats
            //
			Items.Add(new ListItem(ResourceManager.GetString("EmailNotificationDropDownList_NeverSend"), ((int)EmailSubscriptionType.None).ToString()));
			Items.Add(new ListItem(ResourceManager.GetString("EmailNotificationDropDownList_NewThreads"), ((int)EmailSubscriptionType.Thread).ToString()));
			Items.Add(new ListItem(ResourceManager.GetString("EmailNotificationDropDownList_NewPosts"), ((int)EmailSubscriptionType.Post).ToString()));

            // Set the selected index
            //
			ListItem item = Items.FindByValue(((int)ThreadTracking.GetSectionSubscriptionType(csContext.SectionID) & 7).ToString());
			if(item != null)
				Items.FindByValue(((int)ThreadTracking.GetSectionSubscriptionType(csContext.SectionID) & 7).ToString()).Selected = true;
			else
				Items.FindByValue(((int)ThreadTracking.GetSectionSubscriptionType(csContext.SectionID) & 3).ToString()).Selected = true;
            SelectedIndexChanged += new EventHandler(Subscription_Change);
        }

		protected override void Render(HtmlTextWriter writer)
		{
            // If the user is already authenticated we have no work to do
            if (!Page.Request.IsAuthenticated)
                return;

            label.RenderControl(writer);
            base.Render (writer);
        }


        // *********************************************************************
        //  Subscription_Change
        //
        /// <summary>
        /// User wants to change their subscription method
        /// </summary>
        /// 
        // ********************************************************************/ 
		private void Subscription_Change (Object sender, EventArgs e)
		{
			int subscriptionType = (int)ThreadTracking.GetSectionSubscriptionType(csContext.SectionID);
			subscriptionType &= ~7;
			subscriptionType |= int.Parse(SelectedItem.Value);
			ThreadTracking.SetSectionSubscriptionType(csContext.SectionID, (EmailSubscriptionType)subscriptionType);
        }

		public string Text
		{
			get { return label.Text; }
			set { label.Text = value; }
		}
    }
}
