//------------------------------------------------------------------------------
// <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.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceLabel = CommunityServer.Controls.ResourceLabel;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Tools
{
	/// <summary>
	/// Summary description for MassEmailingAdmin.
	/// </summary>
	public class MassEmailingAdmin : BaseToolsPage
	{
		#region Members

		protected StatusMessage Status;
		protected RoleDropDownList RoleList;
		protected TextBox Subject;
		protected Editor EditorMessage;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceControl RegionTitle;
		protected MPContent DescriptionRegion;
		protected ResourceControl Resourcecontrol1;
		protected ResourceLabel Resourcelabel1;
		protected ResourceLabel Resourcelabel2;
		protected ResourceLabel Resourcelabel3;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected LinkButton SendButton;


		#endregion

		override protected void OnInit(EventArgs e)
		{
			this.SendButton.Click += new EventHandler(SendButton_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}

		protected void Bind()
		{
			this.SendButton.Text = ResourceManager.GetString( "Send" );
		}

		#region Command Handlers

		private void SendButton_Click(object sender, EventArgs e) 
		{
			CSContext cntx = CSContext.Current;
			MailPost post = new MailPost();
			post.Subject = Subject.Text;
			post.Body = EditorMessage.Text;
			post.PostType = PostContentType.HTML;
			post.FormattedBody = Transforms.FormatPostText( EditorMessage.Text, post.PostType );			
			post.PostDate = DateTime.Now;
			post.User = cntx.User;
			post.Username = cntx.User.Username;

			Send( post );

			Subject.Text = "";
			EditorMessage.Text = "";
			RoleList.ClearSelection();

			Status.ResourceName = "MassEmailing_StatusSuccess";
			Status.Visible = true;
			Status.Success = true;
		}

		#endregion

		private void InitializeComponent()
		{
		
		}

		#region Command Implementations

		protected virtual void Send( Post post ) 
		{
			Emails.UsersInRole( new Guid(RoleList.SelectedItem.Value), post );
		}

		#endregion

	}
}
