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

var StatusAssistant = Class.create({
	
	initialize: function(response) {
		Mojo.Log.info("------------- response -------------%j", response);
		this.reportData = response;
	},
	
	setup: function() {
		Mojo.listen(this.controller.get('doneButton'), Mojo.Event.tap, this.closeApp.bindAsEventListener(this));
		if(this.reportData) {
			if(this.reportData.isSuccessful == false || this.reportData.errorCode) {
				if(this.reportData.errorCode) {
					this.controller.get('errorText').show();
				} else {
					this.controller.get('partialTransferText').show();
				}
				this.controller.get('helpButton').show();
				this.controller.get('errorTitle').show();
				this.controller.get('error').show();
				Mojo.listen(this.controller.get('helpButton'), Mojo.Event.tap, this.launchHelp.bindAsEventListener(this));
			} else {
				this.controller.get('success').show();
				this.controller.get('successTitle').show();
			}
		
			if(this.reportData.totalContacts > 0) {
				var message = "";
				if ((this.reportData.savedContacts < this.reportData.savedContacts) || (this.reportData.isSuccessful == false)) {
					message = $L("#{savedContacts} of #{totalContacts} Contacts Transferred").interpolate({
						savedContacts: this.reportData.savedContacts,
						totalContacts: this.reportData.totalContacts
					});
				} else {
					message = $L("#{savedContacts} Contacts Transferred").interpolate({
						savedContacts: this.reportData.savedContacts
					});
				}
				this.controller.get('contactsText').update(message);
			}
			if(this.reportData.totalEvents > 0) {
				var message = "";
				if ((this.reportData.totalEvents < this.reportData.savedEvents) || (this.reportData.isSuccessful == false)) {
					message = $L("#{savedEvents} of #{totalEvents} Events Transferred").interpolate({
						savedEvents: this.reportData.savedEvents,
						totalEvents: this.reportData.totalEvents
					});
				} else {
					message = $L("#{savedEvents} Events Transferred").interpolate({
						savedEvents: this.reportData.savedEvents
					});
				}
				this.controller.get('eventsText').update(message);
			}
			if(this.reportData.totalMemos > 0) {
				var message = "";
				if ((this.reportData.savedMemos < this.reportData.totalMemos) || (this.reportData.isSuccessful == false)) {
					message = $L("#{savedMemos} of #{totalMemos} Memos Transferred").interpolate({
						savedMemos: this.reportData.savedMemos,
						totalMemos: this.reportData.totalMemos
					});
				} else {
					message = $L("#{savedMemos} Memos Transferred").interpolate({
						savedMemos: this.reportData.savedMemos
					});
				}
				this.controller.get('memosText').update(message);
			}
			if(this.reportData.totalTasks > 0) {
				var message = "";
				if ((this.reportData.totalTasks < this.reportData.totalTasks) || (this.reportData.isSuccessful == false)) {
					message = $L("#{savedTasks} of #{totalTasks} Tasks Transferred").interpolate({
						savedTasks: this.reportData.savedTasks,
						totalTasks: this.reportData.totalTasks
					});
				} else {
					message = $L("#{savedTasks} Tasks Transferred").interpolate({
						savedTasks: this.reportData.savedTasks
					});
				}
				this.controller.get('tasksText').update(message);
			}	
		}
	},
	
	launchHelp: function () {
		AppAssistant.dataImportService.launchHelp();
	},
	
	closeApp: function() {
		this.controller.window.close();
	}
	
});	