//= require <mootools-core-1.3-full-nocompat>
//= require <puremvc-mootools-port>

var AccountSetupViewMediator = function()
{
	this.Extends = Mediator;

	this.accountSetupView = null;
	
	this.pendingNewAccountRequest = null;
	
	this.pendingUsernameCheck = null;
	
	/**
	 * Reference to the AsyncProxy instance registered to the Facade
	 * 
	 * @type AsyncProxy
	 */
	this.asyncProxy = null;
	
	this.initialize = function(viewComponent)
	{
		this.parent(AccountSetupViewMediator.NAME, viewComponent);
		this.accountSetupView = viewComponent;
	};
	
	this.listNotificationInterests = function()
	{
		return [AsyncProxy.NEW_ACCOUNT_RESULT, AsyncProxy.USERNAME_CHECK_RESULT];
	}
	
	this.handleNotification = function(note)
	{
		switch(note.getName())
		{
			case AsyncProxy.NEW_ACCOUNT_RESULT:
				this.newAccountResultHandler(note.getBody());
				break;
				
			case AsyncProxy.USERNAME_CHECK_RESULT:
				this.usernameCheckHandler(note.getBody());
				break;
		}
	}

	this.onRegister = function()
	{
		this.parent();
		this.accountSetupView.addEventListener("submit", this.accountSetupView_submitHandler, false, 0, this);
		this.asyncProxy = this.facade.retrieveProxy(AsyncProxy.NAME);
		
		this.accountSetupView.usernameInput.addEventListener("input", this.username_inputHandler, false, 0, this);
	};

	this.accountSetupView_submitHandler = function(event)
	{
		if (this.pendingRequestUid)
			return;
		this.accountSetupView.submitButton.enabled(false);
		var data = this.getFormInputaData();
		data.uid = UIDUtil.createUID();
		this.pendingNewAccountRequest = data.uid;
		this.asyncProxy.newAccountSetup(data);
	};
	
	this.username_inputHandler = function(event)
	{
		var value = event.data;
		if (value.length < 4)
		{
			this.accountSetupView.updateUsernameMessage("User name is too short.");
			this.accountSetupView.state("info");
		}
		else
		{
			this.pendingUsernameCheck = UIDUtil.createUID();
			var data = {uid:this.pendingUsernameCheck, username:value};
			this.asyncProxy.checkUsername(data);
		}
	};
	
	this.newAccountResultHandler = function(result)
	{
		var uid = result.uid;
		if (this.pendingNewAccountRequest != uid)
			return;
		
		this.accountSetupView.submitButton.enabled(true);
		var success = result.success;
		var message = result.message;
		
		if (success)
			this.accountSetupView.state("success");
		else
			this.accountSetupView.state("failed");
		
		this.accountSetupView.updateMessageText(message);
		
		this.pendingNewAccountRequest = null;
	};
	
	this.usernameCheckHandler = function(result)
	{
		var uid = result.uid;
		if (this.pendingUsernameCheck != uid)
			return;
		
		this.pendingUsernameCheck = null;
		this.accountSetupView.updateUsernameMessage(result.message);
		if (result.available)
			this.accountSetupView.state("ok");
		else
			this.accountSetupView.state("info");
	}
	
	this.getFormInputaData = function()
	{
		var username = this.accountSetupView.usernameInput.input().get("value");
		var firstName = this.accountSetupView.firstNameInput.input().get("value");
		var lastName = this.accountSetupView.lastNameInput.input().get("value");
		var emailAddress = this.accountSetupView.emailInput.input().get("value");
		var password = this.accountSetupView.passwordInput.input().get("value");
		
		return {username:username, firstName:firstName, lastName:lastName, emailAddress:emailAddress, password:password};
	};
}
AccountSetupViewMediator = new Class(new AccountSetupViewMediator());
AccountSetupViewMediator.NAME = "accountSetupViewMediator";
