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

namespace CommunityServer.ControlPanel.Settings
{
	/// <summary>
	/// Summary description for Email.
	/// </summary>
	public class EmailSettings : BaseSettingsPage
	{

		#region Members

		protected YesNoRadioButtonList EnableEmail;
		protected YesNoRadioButtonList EnableMailGateway;
		protected TextBox SmtpServer;
		protected TextBox EmailEncoding;
		protected YesNoRadioButtonList SmtpServerUsingNtlm;
		protected YesNoRadioButtonList SmtpServerRequiredLogin;
		protected TextBox SmtpServerUserName;
		protected TextBox SmtpServerPassword;
		protected TextBox EmailThrottle;
		protected TextBox SmtpPortNumber;
		protected RequiredFieldValidator SmtpServerUserNameValidator;
		protected RequiredFieldValidator SmtpServerPasswordValidator;
		protected RequiredFieldValidator EmailThrottleValidator;

		protected StatusMessage Status;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceControl RegionTitle;
		protected MPContent DescriptionRegion;
		protected ResourceControl Resourcecontrol27;
		protected HelpIcon Helpicon13;
		protected ResourceControl ResourceControl136;
		protected HelpIcon Helpicon5;
		protected ResourceControl lblEmailThrottle;
		protected HelpIcon Helpicon10;
		protected ResourceControl ResourceControl24;
		protected HelpIcon Helpicon1;
		protected ResourceControl ResourceControl138;
		protected ResourceControl Resourcecontrol1;
		protected HelpIcon Helpicon2;
		protected ResourceControl ResourceControl140;
		protected HelpIcon Helpicon3;
		protected ResourceControl ResourceControl142;
		protected HelpIcon Helpicon4;
		protected ResourceControl ResourceControl144;
		protected HelpIcon Helpicon6;
		protected ResourceControl Resourcecontrol2;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected ResourceLinkButton SaveButton;

		protected Control EnableMailGateway_Row;

		#endregion

		override protected void OnInit(EventArgs e)
		{

			this.SaveButton.Click += new EventHandler(SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);

			SmtpServerUserNameValidator.EnableClientScript = false;
			SmtpServerPasswordValidator.EnableClientScript = false;

			base.OnInit(e);
		}


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


		protected void Bind()
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
			Status.Visible = false;
			this.SaveButton.Text = ResourceManager.GetString( "Save" );
			this.SaveButton.Attributes["onclick"] = "EnableForSubmit();";

			EnableMailGateway_Row.Visible = Telligent.Registration.MailGateway.IsLicenseAvailable;

			EnableEmail.SelectedValue = settings.EnableEmail;
			EnableMailGateway.SelectedValue = settings.EnableMailGateway;
			SmtpServer.Text = settings.SmtpServer;
			EmailEncoding.Text = settings.EmailEncoding;
			SmtpServerUsingNtlm.SelectedValue = settings.SmtpServerUsingNtlm;
			SmtpServerRequiredLogin.SelectedValue = settings.SmtpServerRequiredLogin;
			SmtpServerUserName.Text = settings.SmtpServerUserName;
			SmtpServerPassword.Text = settings.SmtpServerPassword;
			SmtpPortNumber.Text = settings.SmtpPortNumber;
			EmailThrottle.Text = settings.EmailThrottle.ToString();

		}

		private void SaveButton_Click(object sender, EventArgs e) 
		{
			SiteSettings settings = CSContext.Current.SiteSettings;

			SaveSettings( settings );

			if(Page.IsValid)
			{
				Status.Success = true;
				Status.ResourceName = "Admin_SiteSettings_StatusSuccess";

				DataBind();
			}
			else
			{
				Status.Success = false;
				Status.ResourceName = "Admin_SiteSettings_StatusFailed";
			}

			Status.Visible = true;
		}

		private void InitializeComponent()
		{
		
		}

		protected virtual void SaveSettings( SiteSettings settings ) 
		{

			settings.EnableEmail = EnableEmail.SelectedValue;
			settings.EnableMailGateway = EnableMailGateway.SelectedValue;
			settings.SmtpServer = SmtpServer.Text;
			settings.EmailEncoding = EmailEncoding.Text;
			settings.SmtpServerUsingNtlm = SmtpServerUsingNtlm.SelectedValue;
			settings.SmtpServerRequiredLogin = SmtpServerRequiredLogin.SelectedValue;
			settings.SmtpServerUserName = SmtpServerUserName.Text;
			settings.SmtpServerPassword = SmtpServerPassword.Text;
			settings.SmtpPortNumber = SmtpPortNumber.Text.Trim();

			if(settings.SmtpServerRequiredLogin)
			{
				if(settings.SmtpServerUserName.Trim() == string.Empty)
					SmtpServerUserNameValidator.IsValid = false;
				else
					SmtpServerUserNameValidator.IsValid = true;

				if(settings.SmtpServerPassword.Trim() == string.Empty)
					SmtpServerPasswordValidator.IsValid = false;
				else
					SmtpServerPasswordValidator.IsValid = true;
			}
			else
			{
				SmtpServerUserNameValidator.IsValid = true;
				SmtpServerPasswordValidator.IsValid = true;
			}

			try 
			{ 
				settings.EmailThrottle = Int32.Parse(EmailThrottle.Text); 
				if(Int32.Parse( EmailThrottle.Text ) < -1)
					EmailThrottleValidator.IsValid = false;
			}
			catch { EmailThrottleValidator.IsValid = false; }


			if(Page.IsValid)
				SiteSettingsManager.Save(settings);

		}


	}
}
