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

var AppInfoAssistant = Class.create({
	initialize: function(sceneAssistant, app) {
		this.app = app;
		this.sceneAssistant = sceneAssistant;
		this.controller = this.sceneAssistant.controller;
	},

	setup : function(widget) {
		this.widget = widget;
		
		this.sceneAssistant.appInfoDialog = this.widget;
		
		this.insertVersion();
		this.setupButtons();
		
		if(!this.isDynamicLaunchPoint(this.app.launchPointId)) {
			this.controller.serviceRequest('palm://com.palm.applicationManager', {
				method: 'getSizeOfApps',
				parameters: {'appId':this.app.appId},
				onSuccess: this.onAppSize.bind(this)
			});
		}
		
//		Mojo.listen($('app-icon'), Mojo.Event.tap, this.launchApp.bindAsEventListener(this));
		
//		this.sceneAssistant.showQuickLaunch(false);
//		this.restoreQuickLaunch = true;
	},
	
	insertVersion: function() {
		
		// construct the version portion and insert it 
		var obj = {
			version: "",
			vendor: $L('Unknown'),
			vendorUrl: undefined
		};
		
		if (this.app.version) {
			obj.version = $L("v")+this.app.version;
		}
		
		if (this.app.vendor) {
			obj.vendor = this.app.vendor;
			if (this.app.vendorUrl && this.app.vendorUrl.length > 0) {
				obj.vendorUrl = this.app.vendorUrl;
			}
		}
		
		var versionTemplate;
		if (obj.vendorUrl) {
			versionTemplate = Mojo.View.render({template: "more/dialogs/app-version-hyperlink", object: obj});
		}
		else {
			versionTemplate = Mojo.View.render({template: "more/dialogs/app-version", object: obj});
		}
		$('app-version').update(versionTemplate);
	},
	
	setupButtons: function() {
		// delete button
		var deleteModel = {buttonLabel: $L('Delete'), buttonClass: 'negative'};
		var deleteAttrs = {};

		if (this.app.removable) {		
			// show the delete button	
			this.controller.setupWidget('delete-btn', deleteAttrs, deleteModel);
			this.controller.listen('delete-btn', Mojo.Event.tap, this.onDelete.bindAsEventListener(this));	
			this.controller.get('delete-btn').show();
			// done button is light
			var doneModel = {
				buttonLabel: $L('Done'),
				buttonClass: 'dismiss'
			};
		} else {
			// hide 
			this.controller.get('delete-btn').hide();
			// done button is dark by default
			var doneModel = {
				buttonLabel: $L('Done')
			};
		}	

		this.controller.setupWidget('done-btn', undefined, doneModel);
		this.controller.listen('done-btn', Mojo.Event.tap, this.close.bindAsEventListener(this));
	},
	
	onDelete: function() {
//		this.restoreQuickLaunch = false;
		if (this.deleteRequest) {
			return;
		}
		
		if (this.isDynamicLaunchPoint(this.app.launchPointId)) {
			
			this.deleteRequest = new Mojo.Service.Request('palm://com.palm.applicationManager', {
				method: 'removeLaunchPoint',
				parameters: { launchPointId: this.app.launchPointId },
				onSuccess: this.deleteSuccessCallback.bind(this),
				onFailure: this.deleteFailureCallback.bind(this)
			});
		} else {

			this.deleteRequest = new Mojo.Service.Request('palm://com.palm.appInstallService', {
				method: 'remove',
				parameters: {id: this.app.id },
				onSuccess: this.deleteSuccessCallback.bind(this),
				onFailure: this.deleteFailureCallback.bind(this)
			});
		}
	},
	
	isDynamicLaunchPoint: function(str) {				
		var num = new Number(str);
		if (isNaN(num)) {
			return false;
		}
		if (num.toPaddedString(8, 10) !== str) {
			return false;
		}
		return true;	
	},
	
	deleteSuccessCallback: function(response) {
		this.widget.mojo.close();
	},
	
	deleteFailureCallback: function(response) {
		this.widget.mojo.close();
	},
	
	onAppSize: function(response) {
		if (response.returnValue) {
			var sizeInKBytes = response[this.app.appId] / 1024;
			
			this.sceneAssistant.controller.get('app-size').update(sizeInKBytes + $L('K'));
		}
	},
	
	close: function() {
		this.widget.mojo.close();
	},
	
	launchApp: function(event) {
//		this.sceneAssistant.launchApp(this.app);
		this.widget.mojo.close();
	},
	
	cleanup: function() {
//		this.sceneAssistant.showQuickLaunch(this.restoreQuickLaunch);
		this.sceneAssistant.appInfoDialog = null;
	}
});
