//------------------------------------------------------------------------------
// <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.Discussions.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.ControlPanel.Controls;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Forums
{
	/// <summary>
	/// Summary description for Forums.
	/// </summary>
	public class SectionEdit : BaseForumPage
	{

		#region Child Controls

		protected SectionEditControl SectionEdit1;
		protected Literal RegionTitle;
		protected CommunityServer.ControlPanel.Controls.ResourceLinkButton SaveButton;
		protected CommunityServer.ControlPanel.Controls.ResourceLinkButton DeleteButton;

		#endregion

		private CSContext context;
		private int sectionID;
		private bool isNew
		{
			get{return sectionID <= 0;}
		}


		override protected void OnInit(EventArgs e)
		{
			this.SaveButton.Click += new EventHandler(SaveButton_Click);
			this.DeleteButton.Click += new EventHandler(DeleteButton_Click);
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}


		private void Page_Load(object sender, EventArgs e)
		{
			context = CSContext.Current;

			if( !context.User.IsForumAdministrator ) 
				throw new CSException(CSExceptionType.AdministrationAccessDenied);

			sectionID = context.GetIntFromQueryString("SectionID", -1);

			if (isNew)
			{
				SectionEdit1.Section = new Forum();
				RegionTitle.Text = Components.ResourceManager.GetString("CP_Forums_SectionEdit_New");
			}
			else
			{
				SectionEdit1.Section = Discussions.Components.Forums.GetForum(sectionID, false, true);
				RegionTitle.Text  = string.Format(ResourceManager.GetString("CP_Forums_SectionEdit_Edit"), SectionEdit1.Section.Name);
			}
			
			SectionEdit1.SectionID = sectionID;
			SectionEdit1.ParentSectionID = SectionEdit1.Section.ParentID;
			SectionEdit1.GroupID = SectionEdit1.Section.GroupID;
			DeleteButton.Visible = SectionEdit1.AllowDelete;

		}

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

			if(!SectionEdit1.Save())
				return;

			if(CSContext.Current.ReturnUrl != null && context.ReturnUrl.Length > 0)
				context.Context.Response.Redirect(context.ReturnUrl);
			else
				context.Context.Response.Redirect(ForumUrls.Instance().ControlPanel_ForumList);

		}

		private void DeleteButton_Click( Object sender, EventArgs e ) 
		{
			SectionEdit1.Delete();

			if(context.ReturnUrl != null && context.ReturnUrl.Length > 0)
				context.Context.Response.Redirect(context.ReturnUrl);
			else
				context.Context.Response.Redirect(ForumUrls.Instance().ControlPanel_ForumList);
		}

	}
}
