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

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{
    // *********************************************************************
    //  ThreadSplit
    //
    /// <summary>
    /// This sever control is used to split a thread into a new thread.
    /// </summary>
    /// 
    // ********************************************************************/ 
    public class QuickThreadSplit : TemplatedWebControl
    {
        #region Child Controls

        TextBox subject;
        Label hasReplies;
        Label postedBy;
        Label body;
        ForumListBox moveTo;
        CheckBox sendEmail;
        IButton cancelMove;
        IButton move;
        RequiredFieldValidator moveToValidator;

        CSContext csContext = CSContext.Current;
        ForumPost postToMove = null;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "Moderation/View-ThreadSplit.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            // Get the post we want to move
            //
            postToMove = Posts.GetPost(csContext.PostID, csContext.User.UserID);

            // Do an access check
            //
            Permissions.AccessCheck( postToMove.Section, Permission.Moderate, csContext.User, postToMove );

            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            // Display subject
            //
            if (null != subject) 
                subject.Text = postToMove.Subject;

            // Has Replies?
            //
            if (null != hasReplies) 
            {               
                if (postToMove.Replies > 0) 
                    hasReplies.Text = ResourceManager.GetString("Yes") + " (" + postToMove.Replies + ") ";
                else 
                    hasReplies.Text = ResourceManager.GetString("No");
            }

            // Posted By
            //
            if (null != postedBy)
				postedBy.Text = postToMove.User.DisplayName + " ";

            // Body
            //
            if (null != body) 
                body.Text = postToMove.FormattedBody;
            
            // Cancel
            //
            if (null != cancelMove) 
            {
                cancelMove.Text = ResourceManager.GetString("MovePost_CancelMove");
            }

            // Move Post
            //
            if (null != move) 
                move.Text = ResourceManager.GetString("ThreadSplit_SplitThread");

            // Send email
            //
            if (sendEmail != null)
                sendEmail.Text = ResourceManager.GetString("ThreadSplit_SendUserEmail");
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            subject = (TextBox) FindControl("Subject");
            hasReplies = (Label) FindControl("HasReplies");
            postedBy = (Label) FindControl("PostedBy");
            body = (Label) FindControl("Body");
            moveTo = (ForumListBox) FindControl("MoveTo");
            cancelMove = FindButton("CancelMove");
            move = FindButton("MovePost");
            moveToValidator = (RequiredFieldValidator) FindControl("MoveToValidator");
            sendEmail = (CheckBox) FindControl("SendUserEmail");
			
            InitializeChildControls();
        }

        private void InitializeChildControls() 
        {
            if (null != move) 
                move.Click += new System.EventHandler(SplitThread_Click);

			if (cancelMove != null)
				cancelMove.Click += new System.EventHandler(Cancel_Click);
        }
		        
        #endregion

        #region Event Handlers

        // *********************************************************************
        //  SplitThread_Click
        //
        /// <summary>
        /// Event handler for splitting a thread
        /// </summary>
        /// 
        // ***********************************************************************/
        private void SplitThread_Click(Object sender, EventArgs e) 
        {
            // Make sure their selection is a valid forum.
            if (moveTo.SelectedForum == 0)
            {
                moveToValidator.IsValid = false;
                return;
            }

            // Did the subject change?
            if (subject.Text != postToMove.Subject) 
            {
                postToMove.Subject = subject.Text;

                Posts.UpdatePost(postToMove, csContext.User.UserID);
            }

            // Split the thread
            Moderate.ThreadSplit(postToMove, moveTo.SelectedForum, csContext.User.UserID, sendEmail.Checked);

			Modal.ClosePage(this.Page, postToMove.PostID.ToString());
        }

		private void Cancel_Click(Object sender, EventArgs e)
		{
			Modal.ClosePage(this.Page, "-1");
		}

        #endregion
    }
}
