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

var NovaAccountService = Class.create( {
	initialize: function() {
		
	},
  
  	isUserValid: function(email, password, deviceId, successCallback, errorCallback, sceneController)	{
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'isUserValid',
			parameters: {'email':email, 'password':password, "deviceId":deviceId},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	getAccountToken: function(onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'getAccountToken',
			parameters: {subscribe: false},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
  
	authenticateAccount: function(email, password, onSuccessCallback, onErrorCallback, sceneController ) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'authenticateAccount',
			parameters: {'email':email, 'password':password, 'application':'ASClient'},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	authenticateAccountFromSecurityQuestion: function(email, questionId, response, onSuccessCallback, onErrorCallback, sceneController ) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'authenticateAccountFromSecurityQuestion',
			parameters: {'email':email, 'questionId':questionId, 'response':response},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getAllSecurityQuestions: function(userLocale, successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
		method: 'getAllSecurityQuestions',
		parameters: {subscribe: false, locale:userLocale},
		onSuccess: successCallback,
		onFailure: errorCallback
		});
	},
	
	//TODO Get actual locale
	getAccountSecurityQuestions: function(email, locale, onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'getAccountSecurityQuestion',
			parameters: {"email": email, "locale":locale},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getAccountInfo: function(onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'getAccountInfo',
			parameters: {},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	updateAccountInfo: function(myProfile, onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'updateAccountInfo',
			parameters: {'firstName': myProfile.firstName, 
						'lastName':myProfile.lastName,
						'password':myProfile.password, 
						'email':myProfile.email,
						'languageCode':myProfile.languageCode,
						'countryCode': myProfile.countryCode },
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	changeAccountPassword: function(oldPassword, newPassword, onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'changeAccountPassword',
			parameters: {"oldPassword": oldPassword, "newPassword": newPassword},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	changePassword: function(newPassword, questionId, answer, idToken, isResetPassword, onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'changePassword',
			parameters: {"newPassword": newPassword, "questionId":questionId, "answer":answer, "idToken":idToken, "isResetPassword":isResetPassword},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	changeEmail: function(email, onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'changeEmail',
			parameters: {"email": email},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getLocale: function(successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.systemServiceIdentifier, {
			method: 'getPreferences',
			parameters: {"keys":["locale"]},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	resendVerificationEmail: function(onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'requestResendVerificationEmail',
			parameters: {},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	requestPasswordResetEmail: function(emailAddress, successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
		method: 'requestPasswordResetEmail',
		parameters: {"email": emailAddress, "subscribe": false},
		onSuccess: successCallback,
		onFailure: errorCallback
		});
	},
	
	getDeviceId: function (onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.deviceProfileServiceIdentifier, {
			method: 'getDeviceId',
			parameters: {},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getConnectionStatus: function (onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.connectionManagerServiceId, {
			method: 'getstatus',
			parameters: {"subscribe": true},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		}, true);
	}
	
	
 });

NovaAccountService.identifier = 'palm://com.palm.accountservices'; 
NovaAccountService.systemServiceIdentifier = 'palm://com.palm.systemservice';
NovaAccountService.deviceProfileServiceIdentifier = 'palm://com.palm.deviceprofile';
NovaAccountService.connectionManagerServiceId = 'palm://com.palm.connectionmanager';
