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

var AppDeleteAssistant = 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.appDeleteDialog = this.widget;
		
		this.setupButtons();
	},
	
	setupButtons: function() {
		// yes button		
		this.controller.setupWidget('yes-btn', null, {buttonLabel: $L('Delete'), buttonClass: 'negative'});
		Mojo.listen($('yes-btn'), Mojo.Event.tap, this.onDelete.bindAsEventListener(this));
		
		// no button
		var noModel = {
			buttonLabel: $L('Cancel'),
			buttonClass: 'dismiss'
		};
		
		this.controller.setupWidget('no-btn', null, noModel);
		Mojo.listen($('no-btn'), Mojo.Event.tap, this.onCancel.bindAsEventListener(this));
	},
	
	onDelete: function() {
		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();
	},
	
	onCancel: function() {
		this.widget.mojo.close();
	},
	
	cleanup: function() {
		delete this.deleteRequest;
		
//		this.sceneAssistant.showQuickLaunch(true);
//		this.sceneAssistant.appDeleteDialog = null;
	}
});
