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

var RestorecompleteAssistant = Class.create({
	spinner: 'activity-spinner',
	spinning: false,
	spinnerAttrs: {
		spinnerSize: Mojo.Widget.spinnerLarge
	},
				
	initialize: function(){
		FirstUseUtil.addDashboardSmall($('target'));
		saveCurrentState("restorecomplete");
	},
	
	setup: function() {	
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
		// Listen to ## key presses
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));	
		if(displayRequest) {
			displayRequest.cancel();
		}
		
		this.buttonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.buttonModel = {
			disabled: false,
			buttonLabel : $L('Done'),
			buttonClass: 'palm-button'
		};
		
		this.controller.setupWidget('restoreDone', this.buttonAttr, this.buttonModel);
		Mojo.listen(this.controller.get('restoreDone'), Mojo.Event.tap, this.restoreDone.bindAsEventListener(this));
		this.controller.setupWidget(this.spinner, this.spinnerAttrs, {});		
	},
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.back) {
			event.preventDefault();
			event.stopPropagation();
		}	
	},
	
	activate: function() {
		
	},
	
	restoreDone: function() {
		this.showScrim();
		this.buttonModel.disabled = true;
		this.controller.modelChanged(this.buttonModel);
		this.audioRequest = AppAssistant.accountService.resetDefaultVolume(this.controller);
		this.postRequest = AppAssistant.accountService.postLoginSettings(
								this.readPostLoginSettings.bind(this), 
								this.errorPostLoginSettings.bind(this), 
								this.controller);
	},
	
	readPostLoginSettings: function (response) {
		if(response.returnValue && response.returnValue == true) {
			if(this.postRequest) {
				this.postRequest.cancel();
			}
		}
		
		this.notifyProvisioning();
	},
	
	errorPostLoginSettings: function (response) {
		Mojo.Log.error("----------------- Could not do post login settings ------------------%o", $H(response));
		this.notifyProvisioning();
	},
	
	notifyProvisioning: function() {
		if ("cdma" == Account.platformType.toLowerCase()) {
			this.notifyRequest = AppAssistant.accountService.notifyProvisioning(this.readProvisioningNotificationStatus.bind(this), this.controller);
		} else {
			this.notifyServices();
		}									
	},										
	
	readProvisioningNotificationStatus: function (response) {
		if(response.errorCode) {
			Mojo.Log.error("--------- Could not notify provisioning to process NIDC ---------", Object.toJSON(response));
		} else {
			Mojo.Log.info("--------- Notified provisioning to process NIDC ---------", Object.toJSON(response));
		}
		this.notifyServices();
	},
	
	notifyServices: function () {
		this.currentRequest = "messaging";
		this.notifyRequest = AppAssistant.notifyService.notify(
								'com.palm.messaging', 
								this.readNotificationStatus.bind(this), 
								this.errorNotificationStatus.bind(this),
								this.controller);	
	},
	
	sendNextRequest: function () {
		Mojo.Log.info("------------------- this.currentRequest ----------------",this.currentRequest);
		switch (this.currentRequest) {
				case 'messaging': 
					this.currentRequest = "mail";
					this.notifyRequest = AppAssistant.notifyService.notify(
								'com.palm.mail', 
								this.readNotificationStatus.bind(this), 
								this.errorNotificationStatus.bind(this),
								this.controller);
					break;
					
				case 'mail': 
					this.currentRequest = "pimsync";
					this.notifyRequest = AppAssistant.notifyService.notify(
								'com.palm.pimsync', 
								this.readNotificationStatus.bind(this), 
								this.errorNotificationStatus.bind(this),
								this.controller);
					break;
					
				case 'pimsync': 
					this.currentRequest = "calendar";
					this.notifyRequest = AppAssistant.notifyService.notify(
								'com.palm.calendar', 
								this.readNotificationStatus.bind(this), 
								this.errorNotificationStatus.bind(this),
								this.controller);
					break;
					
				case 'calendar': 
					this.currentRequest = "facebook";
					this.notifyRequest = AppAssistant.notifyService.notify(
								'com.palm.facebook', 
								this.readNotificationStatus.bind(this), 
								this.errorNotificationStatus.bind(this),
								this.controller);
					break;
					
				case 'facebook': 
					this.currentRequest = "location";
					this.notifyRequest = AppAssistant.notifyService.notify(
								'com.palm.location', 
								this.readNotificationStatus.bind(this), 
								this.errorNotificationStatus.bind(this),
								this.controller);
					break;			
				
				case 'location': 
					this.currentRequest = "accounts";
                    this.notifyRequest = AppAssistant.notifyService.notify(
                                            'com.palm.accounts',
                                            this.readNotificationStatus.bind(this),
                                            this.errorNotificationStatus.bind(this),
                                            this.controller);
                    break;
				
				case 'accounts':
					this.currentRequest = "none";
					Mojo.Log.info("--------------Set locale ------------------", Account.language, Account.country);
					FirstUseUtil.saveDoneState();
			}
	},
	
	readNotificationStatus: function(response) {
		Mojo.Log.info ("---------------- success from restoreCompleted %o", $H(response));
		if(response.returnValue && response.returnValue == true) {
			if(this.notifyRequest) {
				this.notifyRequest.cancel();
			}
			this.sendNextRequest();
		}
	},
	
	errorNotificationStatus: function (response) {
		Mojo.Log.error ("---------------- error from restoreCompleted %o", $H(response));
		if (response.returnValue && response.returnValue == false &&
				response.errorCode && response.errorCode == -1) {
			// do nothing as java died, wait till java comes back up
		}	else {
			// error in the service, nothing can be done. Proceed to next method
			if(this.notifyRequest) {
				this.notifyRequest.cancel();
			}
			this.sendNextRequest();
		}	
	},
	
	showScrim: function() {
		this.spinning = true;
    	$(this.spinner).mojo.start();
    	this.controller.get('scrim').show();
	},
	
	deactivate: function() {
		this.hideScrim();
	},

	hideScrim: function() {
		this.spinning = false;
		$(this.spinner).mojo.stop();
		this.controller.get('scrim').hide();
    }
	
});	
