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

var UpdatedaemonMojoService = Class.create( {
  initialize: function() {
  },
  checkForUpdate: function(callback, sceneController) {
  	console.log("DaemonService: update checkforupdate");
    return new Mojo.Service.Request(UpdatedaemonMojoService.identifier, {
      method: 'CheckForUpdate',
			parameters: { },
			onSuccess: callback,
			onFailure: Mojo.log
    });
  },
  installNow: function(callback, callbackFail, sceneController) {
  	console.log("DaemonService: update installnow");
    return new Mojo.Service.Request(UpdatedaemonMojoService.identifier, {
      method: 'InstallNow',
			parameters: { },
			onSuccess: callback,
			onFailure: callbackFail
    });
  },
  downloadNow: function(callback, callbackFail, sceneController) {
  	console.log("DaemonService: update downloadnow");
    return new Mojo.Service.Request(UpdatedaemonMojoService.identifier, {
      method: 'DownloadNow',
			parameters: { },
			onSuccess: callback,
			onFailure: callbackFail
    });
  },
  cancelDownload: function(callback, callbackFail, sceneController) {
  	console.log("DaemonService: update cancel download");
    return new Mojo.Service.Request(UpdatedaemonMojoService.identifier, {
      method: 'CancelDownload',
			parameters: { },
			onSuccess: callback,
			onFailure: callbackFail
    });
  },
  getSubscribe: function(callback, sceneController) {
  	console.log("DaemonService: update getstatus subscribe");
    return new Mojo.Service.Request(UpdatedaemonMojoService.identifier, {
      method: 'GetStatusApp',
			parameters: { subscribe: true },
			onSuccess: callback
    });
  },
  get: function(callback, sceneController) {
  	console.log("DaemonService: update getstatus");
    return new Mojo.Service.Request(UpdatedaemonMojoService.identifier, {
      method: 'GetStatusApp',
			parameters: { },
			onSuccess: callback
    });
  }
});

var UpdatedaemonMockService = Class.create( {
  initialize: function() {
	this.status = "Checking";
  },
  
  checkForUpdate: function(callback, sceneController) {
  	console.log("Updates Mock App: update checkforupdate");
	
	setTimeout(function(){
//		this.status = "Waiting";
		this.status = "UpToDate";
		
		if (this.callback) {
			this.callback({
				"status": this.status,
				"spaceNeeded": 0,
				"networkAvailable": true,
				"version": 1.1,
				"size": 10,
				"lowSpeed": false,
				"downRev": false,
				"time": 1230691549
			});
		}
	}.bind(this), 1000);
	
  },
  
  installNow: function(callback, callbackFail, sceneController) {
  	console.log("Updates Mock App: update installnow");
//    this.status = "InsufficientCharge";
//	this.status = "InvalidUpdate";
//	callback({"status": this.status, "time": 1230691549});
  },
  
  downloadNow: function(callback, callbackFail, sceneController) {
  	console.log("Updates Mock App: update downloadnow");
	
	setTimeout(function(){
	    this.status = "Downloading";
		this.callback({
			"status": this.status,
			"spaceNeeded": 0,
			"networkAvailable": true,
			"version": 1.1,
//			"percent": 0,
			"lowSpeed": false,
			"time": 1230691549
		})
	}.bind(this), 10);	
	
	setTimeout(function(){
	    this.status = "Downloading";
		this.callback({
			"status": this.status,
			"spaceNeeded": 0,
			"networkAvailable": true,
			"version": 1.1,
			"percent": 20,
			"lowSpeed": false,
			"time": 1230691549
		})
	}.bind(this), 1000);
	
	setTimeout(function(){
	    this.status = "Downloading";
		this.callback({
			"status": this.status,
			"spaceNeeded": 0,
			"networkAvailable": true,
			"version": 1.1,
			"percent": 40,
			"lowSpeed": false,
			"time": 1230691549
		})
	}.bind(this), 1500);
	
	setTimeout(function(){
	    this.status = "Downloading";
		this.callback({
			"status": this.status,
			"spaceNeeded": 0,
			"networkAvailable": true,
			"version": 1.1,
			"percent": 60,
			"lowSpeed": false,
			"time": 1230691549
		})
	}.bind(this), 2000);
	
	setTimeout(function(){
		this.status = "Downloading";
		this.callback({
			"status": this.status,
			"spaceNeeded": 0,
			"networkAvailable": true,
			"version": 1.1,
			"percent": 100,
//			"lowSpeed": true,
			"time": 1230691549
		})
	}.bind(this), 3000);	
    
	setTimeout(function(){
		this.status = "Available";
		this.callback({
			"status": this.status,
			"spaceNeeded": 0,
			"networkAvailable": true,
			"version": 1.1,
			"lowSpeed": false,
			"percent": 100,
			"time": 1230691549
		})
	}.bind(this), 6000);
  },
  
  get: function(callback, sceneController) {
  	console.log("Updates Mock App: update getstatus");
	this.callback = callback;    
	//this.callback({"status": this.status, "time": 1230691549});
	this.callback({
			"status": this.status,
			"spaceNeeded": 0,
			"networkAvailable": true,
			"version": 1.1,
			"size": 10,
			"lowSpeed": false,
			"downRev":false,
			"time": 1230691549
		});
	
  }
});

UpdatedaemonMojoService.identifier = 'palm://com.palm.update';
