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

var UpdateModel = Class.create({
	initialize: function() {
		Mojo.Log.info("UpdateModel initialize");
		this.callback = null;
		this.status = "Checking";
		this.response = {};
		this.serviceRequest = AppAssistant.updatedaemonService.getSubscribe(this.updateCallback.bind(this));
	},
	
	setCallback: function(callback, blockCallback) {
		Mojo.Log.info("UpdateModel setCallBack");
		this.callback = callback;
		if((this.response && this.response.status) && !blockCallback){
			callback(this.response);
		}
	},	
	
	updateCallback: function(response){
		Mojo.Log.info("UpdateModel updateCallBack", Object.toJSON(response));
		
		if (this.pauseCallback == true) {
			Mojo.Log.info("UpdateModel updateCallBack return pause");
			this.pauseResponse = response;
			return;
		} else {
			this.pauseResponse = undefined;
		}
			
		this.response = response;
		
		if ("status" in response) {
			this.status = response.status;
		}
		
		if(this.callback){
			this.callback(response);
		}		
	},
	
	getStatus: function(){
		return this.status;
	},
	
	dummy: function(response){
		
	},
	
	checkSuccess: function(response){
		this.pauseCallback = false;
		if(this.checkSuccessCallback && this.checkSuccessCallback != null)
			this.checkSuccessCallback(response);
		
		if(this.pauseResponse != undefined && this.pauseResponse.status != "UpToDate"){
			this.updateCallback(this.pauseResponse);
		}
			
		this.checkRequest.cancel();
	},
	
	checkForUpdates: function(callback){
		this.pauseCallback = true;
		this.checkSuccessCallback = callback;
		this.checkRequest = AppAssistant.updatedaemonService.checkForUpdate(this.checkSuccess.bind(this));
//		this.response.status = "Checking"; 	
//		this.callback(this.response);
	},
	
	downloadSuccess: function(response){
		this.pauseCallback = false;
		if(this.downloadSuccessCallback && this.downloadSuccessCallback != null)
			this.downloadSuccessCallback(response);
			
		this.downloadRequest.cancel();			
	},
	
	downloadNow: function(callback){
		this.pauseCallback = true;
		this.downloadSuccessCallback = callback;
		this.downloadRequest = AppAssistant.updatedaemonService.downloadNow(this.downloadSuccess.bind(this), this.dummy.bind(this));
		this.response.status = "Downloading"; 	
		this.callback(this.response);
	},
	
	cancelSuccess: function(response){
		this.pauseCallback = false;
		if(this.cancelSuccessCallback && this.cancelSuccessCallback != null)
			this.cancelSuccessCallback(response);
		
		this.cancelRequest.cancel();	
	},
	
	cancelDownload: function(callback){
		this.pauseCallback = true;
		this.cancelSuccessCallback = callback;
		this.cancelRequest = AppAssistant.updatedaemonService.cancelDownload(this.cancelSuccess.bind(this), this.dummy.bind(this));
		this.response.status = "DownloadRetry"; 	
		this.callback(this.response);
	},
	
	installSuccess: function(response){
		this.pauseCallback = false;
//		AppAssistant.updatedaemonService.get(this.updateCallback.bind(this));
		if(this.installSuccessCallback && this.installSuccessCallback != null)
			this.installSuccessCallback(response);
			
		this.installRequest.cancel();
	},
	
	installNow: function(callback){
		this.pauseCallback = true;
		this.installSuccessCallback = callback;
		this.installRequest = AppAssistant.updatedaemonService.installNow(this.installSuccess.bind(this), this.dummy.bind(this));
		this.response.status = "InstallBegun";
		this.callback(this.response);
	}
	

});

UpdateModel.model = null;
UpdateModel.getModel = function() {
	if (!UpdateModel.model) {
		UpdateModel.model = new UpdateModel();
	}
	return UpdateModel.model;
};