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

var NovaAccountService = Class.create( {
		
	initialize: function() {
		
	},
	
	turnOffScreenTransitions: function() {
		return new Mojo.Service.Request(NovaAccountService.displayServiceIdentifier, {
			method: 'setProperty',
			parameters: {"requestBlock":true, "client":Mojo.appName, "subscribe":true},
			onSuccess: function(response) {
				Mojo.Log.info("------------ Blocked dimming -----------%o", $H(response));
			},
			onFailure: function(response) {
				Mojo.Log.error("------------ Could not block dimming -----------%o", $H(response));
			}
		}, true);
	},
	
	getPreferences: function(successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.identifier, {
			method: 'getPreferences',
			parameters: {"appName":"ACCT_SRV"},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
  
	getDeviceProfile: function(successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.deviceProfileServiceIdentifier, {
			method: 'getDeviceProfile',
			parameters: {},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	getAllSecurityQuestions: function(userLocale, successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.identifier, {
			method: 'getAllSecurityQuestions',
			parameters: {locale:userLocale},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	getDbSchemaVersion: function(successCallback)	{
		return new Mojo.Service.Request(NovaAccountService.identifier, {
			method: 'getDbSchemaVersion',
			parameters: {'subscribe':true},
			onSuccess: successCallback,
			onFailure: function(response){
				Mojo.Log.error("error in getting db schema version %o", $H(response));
			}
		}, true);
	},
  
	isEmailAvailable: function(email, successCallback, errorCallback, sceneController)	{
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'isEmailAvailable',
			parameters: {'email':email},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	isDeviceInUse: function (email, deviceId, deviceModel, successCallback, errorCallback, sceneController)	{
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'isDeviceInUse',
			parameters: {'email':email, 'deviceId': deviceId, 'deviceModel':deviceModel},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	createAccount: function(Account, successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'createNovaAccount',
			parameters: {'password':Account.password, 'email':Account.email, 
						'firstName':Account.firstName, 'lastName':Account.lastName,
						'language':Account.language, 'country':Account.country,
						'questionId':Account.questionId, 'response':Account.response},
			onSuccess: successCallback,
			onFailure: errorCallback,
			onComplete: successCallback
		});
	},
	
	authenticateAccount: function(Account, onSuccessCallback, onErrorCallback, sceneController ) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'authenticateAccount',
			parameters: {'email':Account.email, 'password':Account.password, 'application':'ASClient', 
						'language':Account.language, 'country':Account.country, 'firstTime': true},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
		
	getPlatformType: function(onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.telephonyServiceIdentifier, {
			method: 'platformQuery',
			parameters: {},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getProvisioningStatus: function(onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.telephonyServiceIdentifier, {
			method: 'getProvisioningStatus',
			parameters: {"subscribe": true},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		}, true);
	},
	
	getNetworkStatus: function(onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.telephonyServiceIdentifier, {
			method: 'networkStatusQuery',
			parameters: {"subscribe": true},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		}, true);
	},
	
	getWanStatus: function(onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.wanIdentifier, {
			method: 'getstatus',
			parameters: {"subscribe": true},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		}, true);
	},
	
	provisionDevice: function(onSuccessCallback, onErrorCallback) {
		Mojo.Log.info("provision device - Activation");
		return new Mojo.Service.Request(NovaAccountService.provisionIdentifier, {
			method: 'Activation',
			parameters: {},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	cidcProvision: function(onSuccessCallback, onErrorCallback) {
		Mojo.Log.info("provision device - User");
		return new Mojo.Service.Request(NovaAccountService.provisionIdentifier, {
			method: 'User',
			parameters: {},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getSessionStatus: function(onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.provisionIdentifier, {
			method: 'SessionStatus',
			parameters: {"subscribe": true},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getSIMStatus: function (onSuccessCallback, onErrorCallback, subscribeParam) {
		var params = {};
		if(subscribeParam && subscribeParam === true) {
			params = {"subscribe":true};
		}
		return new Mojo.Service.Request(NovaAccountService.telephonyServiceIdentifier, {
			method: 'simStatusQuery',
			parameters: params,
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getFDNStatus: function (onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.telephonyServiceIdentifier, {
			method: 'fixedDialingQuery',
			parameters: {},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	isDataAvailable: function (onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.backupServiceIdentifier, {
			method: 'getDevices',
			parameters: {},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	migrateData: function (deviceId, onSuccessCallback, onErrorCallback, sceneController) {
		return new Mojo.Service.Request(NovaAccountService.backupServiceIdentifier, {
			method: 'migrateData',
			parameters: {"deviceID": deviceId, "subscribe":true},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		}, true);
	},
	
	
	restoreData: function (onSuccessCallback, onErrorCallback, sceneController) {
		return new Mojo.Service.Request(NovaAccountService.backupServiceIdentifier, {
			method: 'startRestore',
			parameters: {"subscribe":true},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		}, true);
	},
	
	getDeviceId: function (onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.deviceProfileServiceIdentifier, {
			method: 'getDeviceId',
			parameters: {},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getDeviceModel: function (onSuccessCallback, onErrorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.propsServiceIdentifier, {
			method: 'Get',
			parameters: {"key":"com.palm.properties.DMMODEL"},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getAccountSecurityQuestion: function(emailAddress, locale, successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
		method: 'getAccountSecurityQuestion',
		parameters: {"email": emailAddress, "locale":locale},
		onSuccess: successCallback,
		onFailure: errorCallback
		});
	},
	
	authenticateAccountFromSecurityQuestion: function(emailAddress, questionId, response, successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
		method: 'authenticateAccountFromSecurityQuestion',
		parameters: {"email": emailAddress, "questionId":questionId, "response":response},
		onSuccess: successCallback,
		onFailure: errorCallback
		});
	},
	
	requestPasswordResetEmail: function(emailAddress, successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
		method: 'requestPasswordResetEmail',
		parameters: {"email": emailAddress},
		onSuccess: successCallback,
		onFailure: errorCallback
		});
	},
	
	changeAccountPassword: function (oldPassword, newPassword, successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
		method: 'changeAccountPassword',
		parameters: {"oldPassword": oldPassword, "newPassword": newPassword},
		onSuccess: successCallback,
		onFailure: errorCallback
		});
	},
	
	changePassword: function (newPassword, idToken, isResetPassword, successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
		method: 'changePassword',
		parameters: {"newPassword": newPassword, "idToken": idToken, "isResetPassword": isResetPassword},
		onSuccess: successCallback,
		onFailure: errorCallback
		});
	},
	
	setLocale: function(languageCode, countryCode) {
		return new Mojo.Service.Request(NovaAccountService.systemServiceIdentifier, {
			method: 'setPreferences',
			parameters: {"locale":{"languageCode":languageCode,"countryCode":countryCode}}
		});
	},
	
	setRegion: function(countryCode, countryName) {
		return new Mojo.Service.Request(NovaAccountService.systemServiceIdentifier, {
				method: 'setPreferences',
				parameters: {"region":{"countryCode":countryCode, "countryName": countryName}}
		});		
	},
		
	getCurrentRegion: function (sceneController, successCallback, errorCallback) {
		return sceneController.serviceRequest(NovaAccountService.systemServiceIdentifier, {
			method: 'getPreferences',
			parameters: {"keys":["region"]},
			onSuccess: successCallback,
			onFailure: errorCallback,
		});
	},		
		
	postLoginSettings: function(successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'postLoginSettings',
			parameters: {"firstTime": true},
			onSuccess: successCallback,
			onFailure: errorCallback,
		}, true);
	},
	
	
	notifyRestoreComplete: function() {
		new Mojo.Service.Request('palm://com.palm.messaging', {
			method: 'restoreCompleted',
			parameters: {},
			onSuccess: function(response){
				Mojo.Log.info("sent restore complete message to messaging service %o", $H(response));
			},
			onFailure: function(response){
				Mojo.Log.error("error in sending restore complete message to messaging service %o", $H(response));
			}
		});
		
		new Mojo.Service.Request('palm://com.palm.mail', {
			method: 'restoreCompleted',
			parameters: {},
			onSuccess: function(response){
				Mojo.Log.info("sent restore complete message  to mail service %o", $H(response));
			},
			onFailure: function(response){
				Mojo.Log.error("error in sending restore complete message to mail service %o", $H(response));
			}
		});
			
		new Mojo.Service.Request('palm://com.palm.pimsync', {
			method: 'restoreCompleted',
			parameters: {},
			onSuccess: function(response){
				Mojo.Log.info("sent restore complete message to pimsync service %o", $H(response));
			},
			onFailure: function(response){
				Mojo.Log.error("error in sending restore complete message to pimsync service %o", $H(response));
			}
		});
		
		new Mojo.Service.Request('palm://com.palm.calendar', {
			method: 'restoreCompleted',
			parameters: {},
			onSuccess: function(response){
				Mojo.Log.info("sent restore complete message to calendar service %o", $H(response));
			},
			onFailure: function(response){
				Mojo.Log.error("error in sending restore complete message to calendar service %o", $H(response));
			}
		});
	},
	
	getLanguages: function(successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.systemServiceIdentifier, {
			method: 'getPreferenceValues',
			parameters: {"key":"locale", "subscribe": true},
			onSuccess: successCallback,
			onFailure: errorCallback
		}, true);
	},
	
	getLocale: function(successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.systemServiceIdentifier, {
			method: 'getPreferences',
			parameters: {"keys":["locale"]},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
		
	getEmergencyNumbers: function (successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.telephonyServiceIdentifier, {
			method: 'emergencycalllistQuery',
			parameters: {},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	getCustomerCareNumber: function (successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.systemServiceIdentifier, {
			method: 'getPreferences',
			parameters: {"keys":["customerCareNumber"]},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	isDevice: function(successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.deviceProfileServiceIdentifier, {
			method: 'isDevice',
			parameters: {},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	makeEmergencyCall: function (emergencyNumber) {
		Mojo.Log.info("Emegerncy call to phone app -------------------------------------------------: ",emergencyNumber);
		return new Mojo.Service.Request("palm://com.palm.applicationManager/open", {
            parameters: {
                id: 'com.palm.app.phone',
                params: {
                    "prefill": emergencyNumber,
					"firstuse":true,
					"locale": Account.language + '_' + Account.country 
                }
            }
        });
		
	},
	
	callCustomerCare: function(customerCareNumber, carrierName) {
		return new Mojo.Service.Request("palm://com.palm.applicationManager/open", {
			parameters: {
				id: 'com.palm.app.phone',
				params: {
					"number": customerCareNumber,
					"firstuse":true,
					"name": carrierName,
					"locale": Account.language + '_' + Account.country
				}
			}
		});
	},
	
	launchTutorial : function(languageCode, countryCode) {        
        return new Mojo.Service.Request("palm://com.palm.applicationManager/open", {
            parameters: {
                id: 'com.palm.app.tutorial',
                params: {
                    "languageCode": languageCode,
                    "countryCode": countryCode
                }
            }
        });
    },
	
	// The same as getAccountToken - called from AppAssistant. Didn't want to mess
	// with other scene assistants. 
	checkTokenState: function(successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.identifier, {
			method: 'getAccountToken',
			parameters: {"subscribe": true},
			onSuccess: successCallback,
			onFailure: errorCallback
		}, true);
	},
	
	getAccountToken: function(successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.identifier, {
			method: 'getAccountToken',
			parameters: {},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	isTelephonyReady: function(successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.telephonyServiceIdentifier, {
			method: 'isTelephonyReady',
			parameters: {"subscribe": true},
			onSuccess: successCallback,
			onFailure: errorCallback
		}, true);
	},
	
	setPower: function(successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.telephonyServiceIdentifier, {
			method: 'powerSet',
			parameters: {"state":"on"},
			onSuccess: successCallback,
			onFailure: errorCallback
		}, true);
	},
	
	isSystemServiceUp: function(callback){
		// Subscribe to Battery Power Notifications
		var request = new Mojo.Service.Request('palm://com.palm.lunabus/signal/', {
			method: 'registerServerStatus',
			parameters: {"serviceName":"com.palm.systemservice", "subscribe":true},
			onSuccess: callback
		});
		
		return request;
	},
	
	eraseAll: function(sceneController){
        Mojo.Log.info('Erase All');
        return sceneController.serviceRequest(NovaAccountService.eraseServiceIdentifier, {
            method: 'EraseAll',
            parameters: {},
            onSuccess: function(result){
                Mojo.Log.info('erase all on success', Object.toJSON(result));
            },
            onFailure: function(result){
                Mojo.Log.error('erase all on failure', Object.toJSON(result));
            }
        });
    },
	
	reboot: function(sceneController){
       Mojo.Log.info('Erase Var');
        return sceneController.serviceRequest(NovaAccountService.eraseServiceIdentifier, {
            method: 'EraseVar',
            parameters: {},
            onSuccess: function(result){
                Mojo.Log.info('erase var on success' + Object.toJSON(result));
            },
            onFailure: function(result){
                Mojo.Log.error('erase var on failure' + Object.toJSON(result));
            }
        });
    },
	
	setDefaultTimeFormat: function(format, successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.systemServiceIdentifier, {
			method: 'setPreferences',
			parameters: {"timeFormat":format, "subscribe": true}, 
			onSuccess: successCallback,
			onFailure: errorCallback
		}, true);
	},
	
	isConManUp: function(successCallback, errorCallback) {
		// Subscribe to Connection status
		var request = new Mojo.Service.Request('palm://com.palm.lunabus/signal/', {
			method: 'registerServerStatus',
			parameters: {"serviceName":"com.palm.connectionmanager", "subscribe": true},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
		
		return request;
	},
	
	getConnectionStatus: function(successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.conManIdentifier, {
			method: 'getstatus',
			parameters: {"subscribe": true},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	getConnectionStatusForScene: function(successCallback, errorCallback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.conManIdentifier, {
			method: 'getstatus',
			parameters: {"subscribe": true},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	getCarrierName: function(successCallback) {
		return new Mojo.Service.Request('palm://com.palm.data.carriernetworksettings', {
					method: 'getCarrierName',
					parameters: {"subscribe": true},
					onSuccess: successCallback,	
					onFailure: function(result){
                		Mojo.Log.error('Error in getting carrier name', Object.toJSON(result));
            		}			
    	}, true);	
	},
	
	blockPowerKey: function() {
		return new Mojo.Service.Request(NovaAccountService.displayServiceIdentifier, {
			method: 'setProperty',
			parameters: {
				'powerKeyBlock': true,
				'client': Mojo.appName,
				'subscribe': true
			},
			onSuccess: function(result){
        		Mojo.Log.info('Success in blocking power key', Object.toJSON(result));
    		},	
			onFailure: function(result){
        		Mojo.Log.error('Error in blocking power key', Object.toJSON(result));
    		}
		}, true);
	},
	
	populateDefaults: function (language, country, successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.custIdentifier, {
			method: 'populateDefaults',
			parameters: {"subscribe": true, "language": language, "country": country, "delay": 4000},
			onSuccess: successCallback,
			onFailure: errorCallback
		}, true);
	},
	
	notifyProvisioning: function (callback, sceneController) {
		return sceneController.serviceRequest(NovaAccountService.provisionIdentifier, {
			method: 'FirstUseCompleted',
			parameters: {},
			onSuccess: callback,
			onFailure: callback
		});
	},
	
	resetDefaultVolume: function (sceneController) {
		return sceneController.serviceRequest(NovaAccountService.audioIdentifier, {
			method: 'setVolume',
			parameters: {"scenario":"media_back_speaker","volume":50}
		});
	},
	
	setWanConfig: function (roamguardParam, successCallback, errorCallback) {
		return new Mojo.Service.Request(NovaAccountService.wanIdentifier, {
			method: 'set',
			parameters: {"roamguard":roamguardParam},
			onSuccess: successCallback,
			onFailure: errorCallback
		});
	},
	
	notifyPowerKeyPress: function(successCallback, errorCallback){
		return new Mojo.Service.Request('palm://com.palm.bus/signal/', {
                method: 'addmatch',
                parameters: {
                        "category": "/com/palm/display",
                        "method": "powerKeyPressed",
						"subscribe": true
                },
				onSuccess: successCallback,
				onFailure: errorCallback
		});
	},
	
	powerDown: function (successCallback, errorCallback, reason) {
		return new Mojo.Service.Request('palm://com.palm.power/shutdown', {
        	method: 'machineOff',
        	parameters: {'reason': reason}
        });
	},
	
	runProgressAnimation: function () {
		return new Mojo.Service.Request('palm://com.palm.systemmanager/', {
       		 method: 'runProgressAnimation',
        	 parameters: {'state': 'start'}
        });
	},
	
	getDeviceLockStatus: function () {
		return new Mojo.Service.Request(NovaAccountService.telephonyServiceIdentifier, {
			method: 'deviceLockQuery',
			parameters: {},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getCarrierDetails: function (onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.systemServiceIdentifier, {
			method: 'getPreferences',
			parameters: {"keys":["carrierString", "carrierUrl", "customerCareNumber", "activationCareNumber"]},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getCDMAType: function (onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.systemServiceIdentifier, {
			method: 'getPreferences',
			parameters: {"keys":["omadmActivation", "otaspActivation", "otaspPRLUpdate"]},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},
	
	getMachineName: function (onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.propsServiceIdentifier, {
			method: 'Get',
			parameters: {"key":"com.palm.properties.machineName"},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	},

	getTutorialOverride: function (onSuccessCallback, onErrorCallback) {
		return new Mojo.Service.Request(NovaAccountService.systemServiceIdentifier, {
			method: 'getPreferences',
			parameters: {"keys":["tutorialOverride"]},
			onSuccess: onSuccessCallback,
			onFailure: onErrorCallback
		});
	}
	
 });

NovaAccountService.identifier = 'palm://com.palm.accountservices'; 
NovaAccountService.telephonyServiceIdentifier = 'palm://com.palm.telephony';
NovaAccountService.provisionIdentifier = 'palm://com.palm.provisioning';
NovaAccountService.backupServiceIdentifier = 'palm://com.palm.backup';
NovaAccountService.deviceProfileServiceIdentifier = 'palm://com.palm.deviceprofile';
NovaAccountService.systemServiceIdentifier = 'palm://com.palm.systemservice';
NovaAccountService.displayServiceIdentifier = 'palm://com.palm.display/control/';
NovaAccountService.eraseServiceIdentifier = 'palm://com.palm.storage/erase/';
NovaAccountService.propsServiceIdentifier = 'palm://com.palm.preferences/systemProperties';
NovaAccountService.conManIdentifier = 'palm://com.palm.connectionmanager';
NovaAccountService.custIdentifier = 'palm://com.palm.customization';
NovaAccountService.audioIdentifier = 'palm://com.palm.audio/media/';
NovaAccountService.wanIdentifier = 'palm://com.palm.wan';
