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

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Spam;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for RemoteOptions.
	/// </summary>
	public class RemoteOptionsPage : BaseBlogPage
	{

		#region Child Controls

		protected ResourceLinkButton SaveButton;
		protected Hyperlink NewButton;
		protected ConfigOKStatusMessage Status;
		protected ResourceControl CPDisabledMessage;
		protected TextBox PingServices;
		protected Repeater SettingsList;

		protected CheckBox OverrideSpamSetting;
		protected CheckBox OverridePingServices;
		protected TextBox SpamAutoModerate;
		protected TextBox SpamAutoDelete;
		protected Literal CurrentSpamSettings;
		protected Literal CurrentPingServices;
		protected PlaceHolder SpamSettingsPanel;
		protected PlaceHolder PingServicesPanel;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
			this.SettingsList.ItemDataBound +=new RepeaterItemEventHandler(this.SettingsList_ItemDataBound);
			this.OverridePingServices.CheckedChanged += new EventHandler(OverridePingServices_CheckedChanged);
			this.OverrideSpamSetting.CheckedChanged += new EventHandler(OverrideSpamSetting_CheckedChanged);
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}
		
		private void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{

				if (WeblogConfiguration.Instance().EnableCrossPosting)
				{
					NewButton.NavigateUrl = "javascript:Telligent_Modal.Open('RemoteBlogSettingsForm.aspx', 500, 325, null);";
				}
				else
				{
					NewButton.Visible = false;
					CPDisabledMessage.Visible = true;
				}

				// Load default Spam Config
				SpamConfiguration config = SpamConfiguration.Instance();
				CurrentSpamSettings.Text = string.Format("{0}{1}<br />{2}{3}", ResourceManager.GetString("Spam_ManageSpamRules_AutoModerate"), config.AutoModerateScore.ToString(), ResourceManager.GetString("Spam_ManageSpamRules_AutoDelete"), config.AutoDeleteScore.ToString());

				// Load section override Spam config
				if (CurrentWeblog.SpamAutoModerateScore <= 0 && CurrentWeblog.SpamAutoDeleteScore <= 0)
				{
					this.OverrideSpamSetting.Checked = false;
					this.SpamSettingsPanel.Visible = false;
				}
				else
				{
					this.OverrideSpamSetting.Checked = true;
					this.SpamSettingsPanel.Visible = true;
					SpamAutoModerate.Text = CurrentWeblog.SpamAutoModerateScore.ToString();
					SpamAutoDelete.Text = CurrentWeblog.SpamAutoDeleteScore.ToString();
				}

				// Load default Ping Urls
				WeblogConfiguration wc = WeblogConfiguration.Instance();
				if (wc.DefaultPingServices != null)
				{
					foreach (string url in wc.DefaultPingServices)
					{
						if (CurrentPingServices.Text.Length > 0)
							CurrentPingServices.Text += "; ";

						CurrentPingServices.Text += url;
					}
				}

				// Load section override Ping urls
				if (CurrentWeblog.PingUrls != null && CurrentWeblog.PingUrls.Length > 0)
				{
					this.OverridePingServices.Checked = true;
					this.PingServicesPanel.Visible = true;
					foreach (string url in CurrentWeblog.PingUrls)
					{
						if (PingServices.Text.Length > 0)
							PingServices.Text += "; ";
						PingServices.Text += url;
					}
				}
				else
				{
					this.OverridePingServices.Checked = false;
					this.PingServicesPanel.Visible = false;
				}

			}

			SettingsList.DataSource = CurrentWeblog.CrossPostingSettings;
			SettingsList.DataBind();

		}

		private void SaveButton_Click(object sender, EventArgs e)
		{
			if(Page.IsValid)
			{

				ArrayList PingUrlsArray = new ArrayList();
				if (this.OverridePingServices.Checked)
				{
					if (!Globals.IsNullorEmpty(PingServices.Text))
					{
						foreach (string url in PingServices.Text.Trim().Split(';'))
						{
							if (!Globals.IsNullorEmpty(url))
								PingUrlsArray.Add(url);
						}
					}
				}
				if (PingUrlsArray.Count > 0)
					CurrentWeblog.PingUrls = (string[])PingUrlsArray.ToArray(typeof(string));
				else
				{
					CurrentWeblog.PingUrls = null;
					PingServices.Text = string.Empty;
				}

				if (this.OverrideSpamSetting.Checked)
				{
					if (Globals.IsNullorEmpty(SpamAutoDelete.Text))
						SpamAutoDelete.Text = "0";
					if (Globals.IsNullorEmpty(SpamAutoModerate.Text))
						SpamAutoModerate.Text = "0";

					CurrentWeblog.SpamAutoDeleteScore = Globals.SafeInt(SpamAutoDelete.Text.Trim(), -1); 
					CurrentWeblog.SpamAutoModerateScore = Globals.SafeInt(SpamAutoModerate.Text.Trim(), -1); 
				}
				else
				{
					CurrentWeblog.SpamAutoDeleteScore = -1;
					CurrentWeblog.SpamAutoModerateScore= -1;
					SpamAutoDelete.Text = string.Empty;
					SpamAutoModerate.Text = string.Empty;
				}

				Weblogs.Update(CurrentWeblog);
				
				Status.Success = true;
				Status.Visible = true;
			}
		}

		private void SettingsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				RemoteMetaBlogSettings setting = e.Item.DataItem as RemoteMetaBlogSettings;
				if(setting != null)
				{
					Literal ID = e.Item.FindControl("ID") as Literal;
					if(ID != null)
						ID.Text = setting.ID.ToString();

					Literal Url = e.Item.FindControl("Url") as Literal;
					if(Url != null)
						Url.Text = setting.Url;

					Literal AppKey = e.Item.FindControl("AppKey") as Literal;
					if(AppKey != null)
						AppKey.Text = setting.AppKey;

					Literal Username = e.Item.FindControl("Username") as Literal;
					if(Username != null)
						Username.Text = setting.Username;
					
					Hyperlink Edit = e.Item.FindControl("Edit") as Hyperlink;
					if(Edit != null)
						Edit.NavigateUrl = String.Format("javascript:Telligent_Modal.Open('RemoteBlogSettingsForm.aspx?SettingID={0}', 500, 450, null);", setting.ID);

					Hyperlink Delete = e.Item.FindControl("Delete") as Hyperlink;
					if(Delete != null)
						Delete.NavigateUrl = String.Format("javascript:Telligent_Modal.Open('BlogCommand.aspx?Command={0}&SectionID={1}&SettingID={2}', 400, 300, null);", typeof(DeleteMetaBlogSettingCommand).FullName, CurrentWeblog.SectionID, setting.ID);

				}
			}

		}

		public void ServerValidation(object source, ServerValidateEventArgs args)
		{
			if (!this.OverrideSpamSetting.Checked)
				args.IsValid = true;
			else
			{
				try 
				{
					// Test whether the value entered is blank or an integer
					int i = int.Parse(args.Value);
					args.IsValid = true;
				}
				catch
				{
					args.IsValid = false;
				}
			}
		}

		private void OverridePingServices_CheckedChanged(object sender, EventArgs e)
		{
			this.PingServicesPanel.Visible = OverridePingServices.Checked;

		}

		private void OverrideSpamSetting_CheckedChanged(object sender, EventArgs e)
		{
			this.SpamSettingsPanel.Visible = OverrideSpamSetting.Checked;
		}
	}
}
