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

using System;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel.Membership
{
	/// <summary>
	/// Summary description for UserRoles.
	/// </summary>
	public class UserRoles : BaseMembershipPage
	{

		#region Members

		protected PlaceHolder titlePlaceHolder;
		protected System.Web.UI.HtmlControls.HtmlSelect listAvailableRoles;
		protected System.Web.UI.HtmlControls.HtmlSelect listAssignedRoles;
		protected ResourceLinkButton btnSave;
		protected Label lblUserName;

		#endregion

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

			base.OnInit(e);
		}
		
		private void Page_Load(object sender, EventArgs e)
		{

			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}
	
		public void Bind()
		{

			btnSave.Text = ResourceManager.GetString( "Save" );

			if( CSContext.Current.UserID <= 0 ) 
			{
				btnSave.Enabled = false;
				return;
			}

			User user = Users.GetUser(CSContext.Current.UserID, false );

			Head.AddTitle(String.Concat("User Roles - ", user.Username), Context);
			lblUserName.Text = user.Username;

			// Lucian 4/7/04: Use clone here to prevent loading next time 
			// previous changed list from cache. Remove & RemoveAt seem to work 
			// directly on the cached list, so next time the list isn't the one should be.
			string[] userRoles = (string[])CommunityServer.Components.Roles.GetUserRoleNames( user.Username, false ).Clone();
			string[] availableRoles = (string[])CommunityServer.Components.Roles.GetRoleNames().Clone();

			StringCollection filter = new StringCollection();
			filter.AddRange(availableRoles);

			foreach(string role in userRoles) 
			{
				if(filter.Contains(role)) 
				{
					filter.Remove(role);
				}
			}

			availableRoles = new string[filter.Count];
			filter.CopyTo(availableRoles,0);

			string copyToAvailable = String.Format("copySelected({0},{1});", this.listAssignedRoles.ClientID, this.listAvailableRoles.ClientID);
			string copyToAssigned = String.Format("copySelected({0},{1});", this.listAvailableRoles.ClientID, this.listAssignedRoles.ClientID);

			foreach (string role in availableRoles)
			{	
				ListItem li = new ListItem(role, role);
				//li.Attributes.Add("ondblclick", copyToAssigned);
				listAvailableRoles.Items.Add(li);
			}
			foreach (string role in userRoles)
			{	
				ListItem li = new ListItem(role, role);
				//li.Attributes.Add("ondblclick", copyToAvailable);
				listAssignedRoles.Items.Add(li);
			}

			listAvailableRoles.Attributes.Add("ondblclick", copyToAssigned);
			listAssignedRoles.Attributes.Add("ondblclick", copyToAvailable);
		
		}
		
		private void btnSave_Click(object sender, EventArgs e) 
		{
			// Skip role processing if changes were not made
			if ((Globals.IsNullorEmpty(this.Request.Form["ChangesMade"])) || (this.Request.Form["ChangesMade"] != "true"))
			{
				Modal.ClosePage(this.Page);
				return;
			}

			User user = Users.GetUser(CSContext.Current.UserID, false );

			//Create a container for the users current roles
			StringCollection existingRoles = new StringCollection();
			existingRoles.AddRange( CommunityServer.Components.Roles.GetUserRoleNames( user.Username, false ) );

			//loop through users assigned roles
			if (!Globals.IsNullorEmpty(this.Request.Form["AssignedValues"]))
			{
				foreach(string role in this.Request.Form["AssignedValues"].Split(Convert.ToChar(",")))
				{
					//if the user is "STILL" in a role (previous and now), remove them from the temp collection
					if(existingRoles.Contains(role)) 
					{
						existingRoles.Remove(role); 
					}
					else 
					{
						//This role is not in the existing roles collection, so we add it
						CommunityServer.Components.Roles.AddUserToRole(user.Username, role);
					}
				}

			}


			//These are roles a user was in previously, but has been removed now.
			foreach(string role in existingRoles) 
			{
				CommunityServer.Components.Roles.RemoveUserFromRole(user.Username,role);
			}


			Modal.ClosePage(this.Page);

		}



	}
}
