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

namespace CommunityServer.ControlPanel.Photos
{
	/// <summary>
	/// Summary description for PostEditor.
	/// </summary>
	public class PostEditor : BaseGalleryPage
	{

		#region Child Controls

		protected MPContent DescriptionRegion;
		protected MPContent TaskRegion;
		protected Literal RegionTitle;
		protected ResourceControl RegionDescription;
		protected LinkButton SaveButton;
		protected MPContainer MPContainer;
		protected PostEditControl PostEditControl1;
		protected ControlPanelSelectedNavigation SelectedNavigation1;

		#endregion

		protected int PostID;

		protected CSContext context = null;

		private void Page_Load(object sender, EventArgs e)
		{
			SaveButton.Text = ResourceManager.GetString("Save");

			context = CSContext.Current;
			PanelPage.RegisterControlForByPass(SaveButton);
	
			PostID = context.PostID;
			if(PostID > 0)
			{
				SelectedNavigation1.SelectedNavItem="postlist";
				RegionTitle.Text  = string.Format(Components.ResourceManager.GetString("CP_Photos_PostEdit_EditPost"), PostID) ;
			}
			else
			{
				SelectedNavigation1.SelectedNavItem="photo";
				RegionTitle.Text = Components.ResourceManager.GetString("CP_Photos_PostEdit_NewPost");
			}
			
			RegionTitle.DataBind();
		}

		#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

		protected override void Authorize()
		{
			//make sure the user is authenticated
			base.Authorize ();
			
			//the user needs to be the post owner or moderator or above to edit a post
			Permissions.AccessCheck( this.CurrentGallery , Permission.Post, CSContext.Current.User );
		}

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

			int cid = PostEditControl1.Save();

			if (!Page.IsValid || cid == -2)
				return;

			if(context.ReturnUrl != null && context.ReturnUrl.Length > 0)
				context.Context.Response.Redirect(context.ReturnUrl);
			else
				context.Context.Response.Redirect(Globals.AppendQuerystring( GalleryUrls.Instance().ControlPanel_Photos_PostList, String.Format("sectionid={0}&cid={1}", CurrentGallery.SectionID.ToString(), cid.ToString())));

		}


	}
}
