/* Copyright 2009 Palm, Inc.  All rights reserved. */

var ForgotPasswordAssistant = Class.create({
	
	initialize: function(sceneAssistant, myProfile) {
		this.myProfile = myProfile;
		this.sceneAssistant = sceneAssistant;
		this.controller = sceneAssistant.controller;
		this.attemptCount = 1;
	},
	
	setup : function(widget) {
		this.widget = widget;
		this.clearErrorMessages();
		
		$('questionText').update(this.myProfile.questionText);
						
		this.responseAttributes = {
			modelProperty: 'response',
			multiline: false,
			maxLength: 50, 
			focusMode: Mojo.Widget.focusSelectMode,
			textReplacement: false,
			changeOnKeyPress: true,
			requiresEnterKey: true,
			focus: true
		};
		this.responseModel = {
			'response' : '',
			disabled: false
		};
		
		this.controller.setupWidget('response', this.responseAttributes, this.responseModel);
		$('response').observe(Mojo.Event.propertyChange, this.responseTextChanged.bind(this));
		
		this.buttonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.buttonModel = {
			disabled: true,
			buttonLabel : $L('Done'),
			buttonClass: 'palm-button'
		};
		
		this.controller.setupWidget('submitAnswer', this.buttonAttr, this.buttonModel);
		Mojo.listen(this.controller.get('submitAnswer'), Mojo.Event.tap, this.submitAnswer.bindAsEventListener(this));
		
	},
	
	activate: function() {
		this.controller.get('response').mojo.focus();
	},
	
	toggleDisabled: function() {
		
       if(this.responseModel.response.strip().length > 0) {
		  	this.buttonModel.disabled = false;
			this.controller.modelChanged(this.buttonModel);
	   } else {
	   		this.buttonModel.disabled = true;
			this.controller.modelChanged(this.buttonModel);
	   }
    },
	
	responseTextChanged: function(event) {
	 	this.responseModel.response = event.value;
		this.controller.modelChanged(this.responseModel, this);
		this.toggleDisabled();
		if (event && Mojo.Char.isEnterKey(event.originalEvent.keyCode)) {
			// If the submit button is enabled then submit answer
			if (this.buttonModel.disabled == false) {
				this.submitAnswer();
				Event.stop(event);
			}
			else
			this.controller.get("response").mojo.focus.delay(0);
		}
	},
	
	clearErrorMessages: function() {
		$('noResponseMessage').hide();
		$('wrongResponse').hide();
	},
	
	submitAnswer: function() {
		
		this.response = this.responseModel.response;
		this.clearErrorMessages();
		if(this.response == "" || this.response == undefined) {
			$('noResponseMessage').show();
			return;
		}
		this.buttonModel.disabled = true;
		this.controller.modelChanged(this.buttonModel);
		this.attemptCount++;
		AppAssistant.accountService.authenticateAccountFromSecurityQuestion(
						this.myProfile.email,
						this.myProfile.questionId, 
						this.response, 
						this.authenticateSuccess.bind(this), 
						this.authenticateError.bind(this), 
						this.controller);
	},
	
	authenticateSuccess: function (response) {
		Mojo.Log.info("autheniticate successful: %o", $H(response));
		if(response.returnValue == true) {
			this.myProfile.idToken = response.idToken;
			this.widget.mojo.close();		
			this.controller.showDialog({
				template: 'account/change-password-dialog', //reset-password-dialog
				assistant: new ResetPasswordAssistant(this, this.myProfile) //ResetPasswordAssistant
			});	
		}
	},
	
	authenticateError: function(response){
		this.clearErrorMessages();
		Mojo.Log.error("changePassword error = %o", $H(response));
		if (this.attemptCount <= 3) {
			$('wrongResponse').show();
			this.controller.get('response').mojo.focus();
		} else {
			this.resetRequest = AppAssistant.accountService.requestPasswordResetEmail(this.myProfile.email, 
					this.sendEmailSuccess.bind(this), this.resetEmailFailure.bind(this), this.controller);
		}	
	},
	
	sendEmailSuccess:function(response) {
		this.widget.mojo.close();		
			this.controller.showDialog({
			template: 'account/reset-email-dialog',
			assistant: new ResetEmailAssistant(this)
		});	
	},
	
	resetEmailFailure: function(response) {
		//TODO What to do if we can't send email
		this.widget.mojo.close();
		Mojo.Controller.errorDialog($L("We could not send an email to reset your password. Visit palm.com/support for more help."));
	}
});