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

var UpdateIntroAssistant = Class.create({
	initialize: function(network){
		FirstUseUtil.addDashboardSmall($('target'));
		RestoreUtil.initUpdateService();
		Mojo.Log.error("---------- Account.action -------------", Account.action);
		//If device reboots during update
		if(Account.action === "update") {
			this.launchUpdateApp();
			return;
		}		
		this.network = network;
		// This could happen when user reboots on this screen
		if(!this.network) {
			this.conRequest = AppAssistant.accountService.getConnectionStatus(this.readConStatus.bind(this), this.readConStatus.bind(this));
		} 
		saveCurrentState("update-intro");
	},
	
	setup: function(){
		
		this.buttonAttr = {
			disabledProperty: 'disabled',
			type: Mojo.Widget.activityButton
		};
		
		this.buttonModel = {
			disabled: false,
			buttonLabel : $L('Download Now'),
			buttonClass: 'palm-button'
		};
		this.downloadNowBtn = this.controller.get("downloadNow");
		
		this.controller.setupWidget('downloadNow', this.buttonAttr, this.buttonModel);
		Mojo.listen(this.controller.get('downloadNow'), Mojo.Event.tap, this.downloadNow.bindAsEventListener(this));
		
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
				
		this.keyPressEventHandler =  FirstUseUtil.handleKeyPressEvent.bind(this);
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, this.keyPressEventHandler);
		
		// Allow screen dimming
		if(displayRequest) {
			displayRequest.cancel();
		}
		
		this.showMessage();
			
	},
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.back) {
			event.preventDefault();
			event.stopPropagation();
		}	
	},
	
	showSpinner: function() {
		// Disable the Sign In button
		this.buttonModel.disabled = true;
		this.buttonModel.buttonLabel = $L("Downloading...");
		this.controller.modelChanged(this.buttonModel);
		this.downloadNowBtn.mojo.activate();
	},
	
	hideSpinner: function() {
		this.buttonModel.disabled = false;
		this.buttonModel.buttonLabel = $L("Download Now");
		this.controller.modelChanged(this.buttonModel);
		this.downloadNowBtn.mojo.deactivate();
	},
	
	deactivateButton: function () {
		this.buttonModel.disabled = true;
		this.buttonModel.buttonLabel = $L("Download Now");
		this.controller.modelChanged(this.buttonModel);
	},
	
	networkError: function () {
		// Show network error message only if updates app is not launched yet
		if (Account.action !== "update") {
			this.controller.get('version-error-2g').style.display = 'none';
			this.controller.get('version-error-3g').style.display = 'none';
			this.controller.get('network-error').style.display = 'block';
			this.deactivateButton();
			if (!this.conRequest) {
				this.conRequest = AppAssistant.accountService.getConnectionStatus(this.readConStatus.bind(this), this.readConStatus.bind(this));
			}	
		}	
	},
	
	showMessage: function () {
		if(this.network === "error") {
			//Show error
			this.networkError();
			return;
		} else if(this.network === '2G') {
			this.controller.get('version-error-2g').style.display = 'block';
			this.controller.get('network-error').style.display = 'none';
			this.controller.get('version-error-3g').style.display = 'none';
		} else {
			this.controller.get('version-error-3g').style.display = 'block';
			this.controller.get('version-error-2g').style.display = 'none';
			this.controller.get('network-error').style.display = 'none';
		}
		this.hideSpinner();
	},
	
	readConStatus: function (response) {
		Mojo.Log.info("------- Con status Response ---------%j", response);
		if(response.errorCode) {
			//Show error
			this.networkError();
			return;
		}
		if(this.conRequest) {
			this.conRequest.cancel();
			delete this.conRequest;
		}
		this.network = RestoreUtil.getNetworkStatus(response);
		
		this.showMessage();
	},
						
	downloadNow: function(event){
		this.showSpinner();
		Mojo.Log.error("------------------- I am called ------------------");
		AppAssistant.updateService.checkForUpdate(this.checkForUpdate.bind(this));
	},
	
	checkForUpdate: function (response) {
		if(!response.returnValue || response.returnValue !== true) {
			// Show error	
		}
	},
	
	startDownload: function () {
		Mojo.Log.error("-------------- startDownload of scene assistant is called --------------");
		Account.action = "update";
		AppAssistant.updateService.downloadUpdate(this.downloadCallback.bind(this));
	},
	
	launchUpdateApp: function () {
		Mojo.Log.error("----------- launchUpdateApp ------------ ");
		AppAssistant.updateService.launchUpdates();	
	},
	
	downloadCallback: function (response) {
		Mojo.Log.info("----------- downloadCallback ------------%j " , response);
		if(response.returnValue === true) {
			saveCurrentState("update-intro");
			this.launchUpdateApp();	
		} else {
			//Show error
			Mojo.Log.error("----------- downloadCallback error------------%j " , response);
		}
	}
});	

