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

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for RemoteMetaBlogSettingsForm.
	/// </summary>
	public class RemoteMetaBlogSettingsForm : BaseBlogPage
	{
		#region Child Controls

		protected ResourceLinkButton SaveButton;
		protected StatusMessage Status;

		protected TextBox Url;
		protected TextBox AppKey;
		protected TextBox Username;
		protected TextBox Password;
		protected TextBox AppendMessage;
		protected RadioButtonList MessageLocation;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}
		
		private void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{

				Guid settingID = CSContext.Current.GetGuidFromQueryString("SettingID");

				if (settingID != Guid.Empty)
				{
					RemoteMetaBlogSettings[] settings = CurrentWeblog.CrossPostingSettings;
					int i = GetSettingIndex(settingID, settings);
					if (i != -1)
					{
						this.Url.Text = settings[i].Url;
						this.AppKey.Text = settings[i].AppKey;
						this.Username.Text = settings[i].Username;
						this.Password.Text = settings[i].Password;
						this.AppendMessage.Text = settings[i].Message;
					}
				}

			}
		}

		private void SaveButton_Click(object sender, EventArgs e)
		{
			if(Page.IsValid)
			{
				Guid settingID = CSContext.Current.GetGuidFromQueryString("SettingID");
				RemoteMetaBlogSettings[] settings;

				if (settingID != Guid.Empty)
				{
					settings = CurrentWeblog.CrossPostingSettings;
					int i = GetSettingIndex(settingID, settings);
					if (i != -1)
					{
						settings[i].Url = this.Url.Text.Trim();
						settings[i].AppKey = this.AppKey.Text;
						settings[i].Username = this.Username.Text;
						settings[i].Password = this.Password.Text;
						settings[i].Message = this.AppendMessage.Text;
					}
				}
				else
				{
					// New settings
					settings = CurrentWeblog.CrossPostingSettings;
					int i = 0;
					if (settings == null)
					{
						settings = new RemoteMetaBlogSettings[1];
					}
					else
					{
						i = settings.Length;
						RemoteMetaBlogSettings[] newSettings = new RemoteMetaBlogSettings[i + 1];
						for (int j = settings.GetLowerBound(0); j <= settings.GetUpperBound(0); j++)
						{
							newSettings[j] = settings[j];
						}
						settings = newSettings;
					}
					RemoteMetaBlogSettings setting = new RemoteMetaBlogSettings();
					setting.ID = Guid.NewGuid();
					setting.Url = this.Url.Text.Trim();
					setting.AppKey = this.AppKey.Text;
					setting.Username = this.Username.Text;
					setting.Password = this.Password.Text;
					setting.Message = this.AppendMessage.Text;
					settings[i] = setting;
				}

				CurrentWeblog.CrossPostingSettings = settings;

				Weblogs.Update(CurrentWeblog);
				
				this.RegisterStartupScript("reloadgrid", "<script language=\"javascript\"> window.top.document.forms[0].submit();</script>");
				Modal.ClosePage(this);
			}
		}

		private int GetSettingIndex(Guid settingID, RemoteMetaBlogSettings[] settings)
		{
			// Find the given setting index
			for (int i = settings.GetLowerBound(0); i <= settings.GetUpperBound(0); i++)
			{
				if (settingID == settings[i].ID)
					return i;
			}
			return -1;
		}


	}
}
