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

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Files;
using CommunityServer.Files.Components;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;
using Style = CommunityServer.Controls.Style;

namespace CommunityServer.ControlPanel.Files
{
	/// <summary>
	/// Summary description for CommentEditor.
	/// </summary>
	public class CommentEditor : BaseFilesPage
	{

		#region Member Variables

		protected MPContainer MPContainer;
		protected PanelTabControl PanelTabControl1;
		protected CA.TabStrip CommentEditorTabs;
		protected LinkButton Save;
		protected TextBox ModeratedBy;
		protected Editor Comment;
		protected TextBox Notes;
		protected TextBox OriginalContent;
		protected CheckBox Approved;
		protected TextBox Title;
		protected Style ControlPanelStyle;
		protected MPContent bcr;

		private EntryComment currentPost = null;

		#endregion

		
		override protected void OnInit(EventArgs e)
		{
			this.Save.Click += new EventHandler(this.Save_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}
		

		private void Page_Load(object sender, EventArgs e)
		{
			int postid = -1;

			CSContext cntx = CSContext.Current;
			if(cntx.QueryString["cid"] != null)
			{
				try
				{
					postid = Int32.Parse(Context.Request.QueryString["cid"]);
				}
				catch{}
			}

			if(postid > 0)
			{
				cntx.PostID = postid;
				currentPost = EntryComments.GetComment(postid, false);
			}

			if ( !Page.IsPostBack ) 
			{
				this.DataBind();
				Save.Text = ResourceManager.GetString( "FeedbackEditor_Save" );
				Comment.EnableHtmlModeEditing = true;
				
				if (Approved != null)
					Approved.Text = ResourceManager.GetString( "FeedbackEditor_Approved" );

			}

			Head.AddTitle("Comment Editor", this.Context);
		}


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

			CSContext cntx = CSContext.Current;

			if(ModeratedBy != null)
				ModeratedBy.Text = cntx.User.DisplayName;

			if(Title != null)
				Title.Text = currentPost.Subject;

			if(Comment != null)
				Comment.Text = currentPost.Body;

			if(Notes != null)
				Notes.Text = currentPost.GetExtendedAttribute("Notes");

			if(Approved != null)
				Approved.Checked = currentPost.IsApproved;

			if(OriginalContent != null)
			{
				// If this is the first time accessing the OriginalContent attribute, set it
				string origContent = currentPost.GetExtendedAttribute("OriginalContent");
				if (origContent == String.Empty)
				{
					origContent = currentPost.Body;
					cntx.Post = currentPost;
				}
            
				OriginalContent.Text = origContent;

			}

			// Stick the post in CSContext to it's available to the skin file
			cntx.Post = currentPost;
        
		}


		/// <summary>
		/// Provides a hook for custom logic in the skin file to easily save additional data to the post.
		/// </summary>
		protected void SaveAdditionalContent(EntryComment post) 
		{

			Hashtable ExtendedAttribsToSave = CSContext.Current.Context.Items["ExtendedAttribsToSave"] as Hashtable;
			// If the ExtendedAttribsToSave hashtable exists in HTPPContext,
			//  loop through each item and add them to the post's Extended Attributes
			if (ExtendedAttribsToSave != null)
			{
				if (ExtendedAttribsToSave != null)
				{
					foreach (object key in ExtendedAttribsToSave.Keys)
					{
						post.SetExtendedAttribute(Convert.ToString(key), Convert.ToString(ExtendedAttribsToSave[key]));
					}
				}

			}
		}
	
	

		#region Private Methods

		private void SaveContent()
		{
			// Retrieve post from database to edit
			currentPost = EntryComments.GetComment(currentPost.PostID, false);

			// Retrieve user from database so the current user won't override the original
			if (currentPost.User == null)
			{
				string username = Globals.IsNullorEmpty(currentPost.Username) ? "" : currentPost.Username;
				currentPost.User = Users.GetUser(currentPost.UserID, username, false, false);
			}

			// De-Encode fields that will get re-HTML encoded when saved
			currentPost.Subject = Globals.HtmlDecode(currentPost.Subject);

			// Update post content
			if (Title != null)
				currentPost.Subject = Title.Text;

			if (Comment != null)
				currentPost.Body = Comment.Text;
                
			if (Notes != null)
				currentPost.SetExtendedAttribute("Notes", Notes.Text);

			if (OriginalContent != null)
				currentPost.SetExtendedAttribute("OriginalContent", OriginalContent.Text);

			if (Approved != null)
				currentPost.IsApproved = Approved.Checked;

			currentPost.PostType = PostContentType.HTML;
			User user = CSContext.Current.User;
			currentPost.SetExtendedAttribute("ModeratedBy", user.Username);
             
			// Provide a hook for any child class to easily save stuff
			SaveAdditionalContent(currentPost);

			EntryComments.UpdateComment(currentPost, user.UserID);
		}

		#endregion

		#region Button Click Handlers

		private void Save_Click(object sender, EventArgs e) 
		{
			SaveContent();

			Modal.ClosePage(Page) ;

		}


		#endregion

	}
}
