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

using System;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Blogs.Components;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for DeleteMetaBlogSettingCommand.
	/// </summary>
	public class DeleteMetaBlogSettingCommand : BlogCommand
	{
		public DeleteMetaBlogSettingCommand()
		{
		}

		public override string Message(BlogCommandPage page)
		{

			return ResourceManager.GetString("CP_Blogs_RemoteOptions_ConfirmDelete");
		}

		public override void Action(BlogCommandPage page)
		{
			CSContext cntx = CSContext.Current;
			int SectionID = cntx.SectionID;
			Guid settingID = cntx.GetGuidFromQueryString("SettingID");
			if(SectionID > 0)
			{
				Weblog blog = Weblogs.GetWeblog(SectionID);
				if(blog != null)
				{

					RemoteMetaBlogSettings[] settings = blog.CrossPostingSettings;
					RemoteMetaBlogSettings[] newSettings = new RemoteMetaBlogSettings[settings.Length - 1];

					int i = 0;
					int j = 0;
					foreach (RemoteMetaBlogSettings setting in settings)
					{
						if (settingID != setting.ID)
						{
							newSettings[i] = settings[j];
							i++;
						}
						j++;
					}

					blog.CrossPostingSettings = newSettings;
					Weblogs.Update(blog);
				}
			}
		}

		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;
		}


		public override string Title
		{
			get { return ResourceManager.GetString("CP_Blogs_RemoteOptions_Delete");}
		}

	}
}
