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

using System;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Allows a member to be deleted and content reassigned.
	/// </summary>
	public class DeleteUser : SecureTemplatedWebControl
	{
		CSContext csContext = CSContext.Current;
		User userToDelete = null;
		bool isModal = false;

		protected StatusMessage message;
		protected IText deleteUserName;
		protected TextBox reassignUserName;
		protected CheckBox assignToAnonymous;
		protected CheckBox assignToUser;
		protected IButton deleteUserButton;

		public DeleteUser() : base()
		{
			// do we have a user to delete?
			if (csContext.UserID <= 0)
				return;

			// do we have rights to delete a user?
			if (csContext.User.IsAdministrator || csContext.User.IsMembershipAdministrator)
				userToDelete = Users.GetUser(csContext.UserID, false);
		}

		protected override void AttachChildControls()
		{
			// we need a valid user
			if (userToDelete == null)
				return;

			// attach message control
			message = FindControl("StatusMessage") as StatusMessage;

			// attach existing username control; prepopulate
			deleteUserName = FindText("DeleteUserName");
			if (deleteUserName != null)
				deleteUserName.Text = userToDelete.Username;

			// attach reassign username control
			reassignUserName = FindControl("ReassignUserName") as TextBox;

			// attach assign to user control; configure
			assignToUser = FindControl("AssignToUser") as CheckBox;
			if (assignToUser != null)
				assignToUser.Text = ResourceManager.GetString("DeleteUser_AssignToUser");

			// attach assign to anonymous control; configure
			assignToAnonymous = FindControl("AssignToAnonymous") as CheckBox;
			if (assignToAnonymous != null)
				assignToAnonymous.Text = ResourceManager.GetString("DeleteUser_AssignToAnonymous");

			// attach button; configure; wire-up events
			deleteUserButton = FindButton("DeleteUserButton");
			if (deleteUserButton != null)
			{
				deleteUserButton.Text = ResourceManager.GetString("DeleteUser_Button");
				deleteUserButton.Visible = true;
				deleteUserButton.Click += new EventHandler(DeleteUser_Click);
			}
		}

		protected void DeleteUser_Click(Object sender, EventArgs e)
		{
			// only continue if all page-level validation succeeded
			if (!Page.IsValid)
				return;

			// only continue if we've got a valid user
			if (userToDelete == null)
				return;

			// assume failure
			DeleteUserStatus status = DeleteUserStatus.UnknownFailure;

			// get reassign username
			string targetUserName = string.Empty;
			if (reassignUserName != null)
				targetUserName = reassignUserName.Text.Trim();

			// should we re-assign content to the "anonymous" user
			bool useAnonymous = false;
			if (assignToAnonymous != null)
				useAnonymous = assignToAnonymous.Checked;

			// attempt to delete user and re-assign content
			if (useAnonymous)
				status = Users.DeleteUser(userToDelete);
			else
				status = Users.DeleteUser(userToDelete, targetUserName);

			// did we succeed?
			if (status == DeleteUserStatus.Success)
			{
				// close modal
				if (isModal)
				{
					Modal.ClosePage(this.Page);
					return;
				}

				// where do we go next
				string returnUrl = csContext.ReturnUrl;
				if(Globals.IsNullorEmpty(returnUrl))
					returnUrl = Globals.GetSiteUrls().ControlPanelMembershipHome;

				LeaveSecureConnection(returnUrl);
				return;
			}

			// delete didn't work; display error
			if (message != null)
			{
				message.Visible = true;
				message.Success = (status == DeleteUserStatus.Success);
				message.ResourceName = LookupResourceForStatus(status);
			}
		}

		public bool IsModal
		{
			get { return isModal; }
			set { isModal = value; }
		}

		private string LookupResourceForStatus(DeleteUserStatus status)
		{
			switch (status)
			{
				case DeleteUserStatus.Success:
					return "DeleteUser_Success";
				case DeleteUserStatus.InvalidReassignUserName:
					return "DeleteUser_InvalidReassignUserName";
				case DeleteUserStatus.UnknownFailure:
				default:
					return "DeleteUser_UnknownFailure";
			}
		}
	}
}
