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

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Files.Components;
using CommunityServer.Controls;

namespace CommunityServer.Files.Controls
{

	public class DeleteEntry : FileGalleryTemplatedWebControl
	{
		CSContext csContext = CSContext.Current;

		#region Child Controls

		private IText Name;
		private IText Description;
		private IText FormTitle;

		private IButton Delete;
		private IButton Cancel;

		#endregion

		#region Public Properties

		[DefaultValue( "" )]
		public virtual string ApplicationKey
		{
			get
			{
				Object state = ViewState["ApplicationKey"];
				if(state != null)
					return (string)state;
				return "";
			}
			set { ViewState["ApplicationKey"] = value; }
		}

		[DefaultValue( 0 )]
		public virtual Int32 CategoryID
		{
			get
			{
				Object state = ViewState["CategoryID"];
				if(state != null)
					return (Int32)state;
				return 0;
			}
			set { ViewState["CategoryID"] = value; }
		}

		[DefaultValue( 0 )]
		public virtual Int32 EntryID
		{
			get
			{
				Object state = ViewState["PictureID"];
				if(state != null)
					return (Int32)state;
				return 0;
			}
			set { ViewState["PictureID"] = value; }
		}

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			Name = FindText( "EntryName" );
			Description = FindText( "EntryDescription" );
			FormTitle = FindText("FormTitle");

			Delete = FindButton( "EntryDelete" );
			Cancel = FindButton( "EntryCancel" );

			InitializeChildControls();
		}

		private void InitializeChildControls() 
		{
			if (Delete != null)
				Delete.Click += new EventHandler(Delete_Click);

			if (Cancel != null)
			{
				Cancel.Click += new EventHandler(Cancel_Click);
				Cancel.CausesValidation = false;
			}
		}

		#endregion

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			ApplicationKey = csContext.ApplicationKey;
			CategoryID = csContext.CategoryID;
			EntryID = csContext.PostID;

			Folder folder = Folders.GetFolder(ApplicationKey);
			Entry entry = Entries.GetEntry(EntryID);
			Permissions.AccessCheck( folder, Permission.Post, csContext.User );
			if (csContext.User.Username != entry.Username || csContext.User.IsAnonymous || (folder.IsModerated && csContext.User.ModerationLevel != ModerationLevel.Unmoderated))
				throw new CSException(CSExceptionType.AccessDenied, "You do not have access to delete this file.");
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.DataBind();
		}

		public override void DataBind()
		{
			base.DataBind();

			Entry entry = Entries.GetEntry(CurrentFolder.SectionID, EntryID);

			if (Name != null)
				Name.Text = entry.Subject;

			if (Description != null)
				Description.Text = entry.FormattedBody;

			if (FormTitle != null)
				FormTitle.Text = FileGalleryResourceManager.GetString("Delete") + " " + entry.Subject;

			// Set some resource labels
			if (Delete != null)
				Delete.Text = FileGalleryResourceManager.GetString("Delete");

			if (Cancel != null)
				Cancel.Text = FileGalleryResourceManager.GetString("Cancel");
		}

		private void Delete_Click(object sender, EventArgs e)
		{
			Entries.Delete(Entries.GetEntry(csContext.ApplicationKey, csContext.PostID));

			// Finishing stuff
			if(!Globals.RedirectSiteUrl())
				Page.Response.Redirect(FileGalleryUrls.Instance().ViewFolder(ApplicationKey), true);
		}

		private void Cancel_Click(Object sender, EventArgs e)
		{
			if(!Globals.RedirectSiteUrl())
				csContext.Context.Response.Redirect( FileGalleryUrls.Instance().ViewFolder(ApplicationKey), true );
		}
	}
}
