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

var UpdateService = Class.create( {
	
	checkForUpdate: function (callback) {
		this.statusRequest = this.getStatus();
		return new Mojo.Service.Request("palm://com.palm.update", {
			method: 'CheckForUpdate',
			parameters: {},
			onSuccess: callback,
			onFailure: callback
		});
	},
	
	downloadUpdate: function (callback) {
		return new Mojo.Service.Request("palm://com.palm.update", {
			method: 'DownloadNow',
			parameters: {},
			onSuccess: callback,
			onFailure: callback
		});
	},
	
	launchUpdates: function () {
		
		return new Mojo.Service.Request("palm://com.palm.applicationManager/open", {
            parameters: {
                'id': "com.palm.app.updates",
                params: {
                    autoUpdate: true,
					locale: Account.language + '_' + Account.country
                } 
            }
        });
	},
	
	getStatus: function () {
		return new Mojo.Service.Request("palm://com.palm.update", {
			method: 'GetStatusApp',
			parameters: {subscribe: true},
			onSuccess: (function(response){
					Mojo.Log.info("----------- Update status ------------%j " , response);
					if (response.status && response.status === "Available") {
						// Update is done, now restore
						Account.action = "update";
						saveCurrentState("wait");
					} else if (response.status && response.status === "Waiting") {
						Mojo.Controller.stageController.delegateToSceneAssistant("startDownload");
					} else if (response.status && response.status === "NetworkFailed") {
						Mojo.Controller.stageController.delegateToSceneAssistant("networkError");
					} else if (response && response.networkAvailable === false) {
						Mojo.Controller.stageController.delegateToSceneAssistant("networkError");
					} 
				}).bind(this),
			onFailure: 	(function(response){
					Mojo.Log.error(" ----------- Error Update status ------------%j" , response);
				}).bind(this)});
	}
	
}); 	