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

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

		#region Child Controls

		protected MPContent DescriptionRegion;
		protected MPContent TaskRegion;
		protected ResourceLinkButton SaveButton;
		protected SectionEditControl SectionEdit1;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected Literal RegionTitle;
		protected ResourceControl RegionDescription;
		protected MPContainer Mpcontainer1;
		protected ControlPanel.Controls.StatusMessage Status;

		#endregion

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

		private bool hasGroups
		{
			get {return GalleryGroups.GetGroups(true, true, true).Count <= 0; }
		}

		private void Page_Load(object sender, EventArgs e)
		{
			context = CSContext.Current;
			sectionID = context.GetIntFromQueryString("SectionID", -1);
			groupID = context.GetIntFromQueryString("GroupID", -1);
			
			if(hasGroups)
			{
				Status.Success = false;
				Status.Text = Components.ResourceManager.GetString("CP_PhotosAdmin_SectionEdit_NoGalleryGroups");
				Status.Visible = true;
				SectionEdit1.Visible = false;
				RegionDescription.Visible = false;
				SaveButton.Visible = false;
			}

			if(isNew)
			{
				SelectedNavigation1.SelectedNavItem="gallery";
				currentGallery = new Gallery();
				RegionTitle.Text = ResourceManager.GetString("CP_PhotosAdmin_SectionEdit_NewGallery");
				SectionEdit1.GroupID = groupID;
			}
			else
			{
				currentGallery = CommunityServer.Galleries.Galleries.GetGallery(sectionID);
				SelectedNavigation1.SelectedNavItem="galleries";
				RegionTitle.Text  = string.Format(ResourceManager.GetString("CP_PhotosAdmin_SectionEdit_EditGallery"), currentGallery.Name) ;
			}

			RegionTitle.DataBind();

			SectionEdit1.SectionID = sectionID;

			
			if(!Page.IsPostBack)
				SectionEdit1.Gallery = currentGallery;

		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new EventHandler(this.Page_Load);

		}


		#endregion

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

			if(!SectionEdit1.Save())
				return;

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

	}
}
