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

function DashboardSendErrorAssistant(dashboardData){
	this.dashboardData = dashboardData; // hold onto a reference to the dashboardData
	this.appController = Mojo.Controller.getAppController();
	this.Messaging = this.appController.Messaging;
	this.tapHandler = this.tapHandler.bindAsEventListener(this);
}

DashboardSendErrorAssistant.prototype.setup = function(){
	this.update(true);
	this.messageInfoElement = this.controller.get('message_info');
	this.messageInfoElement.addEventListener(Mojo.Event.tap, this.tapHandler);	
};

DashboardSendErrorAssistant.prototype.cleanup = function(){
	// If the dashboard is unloaded then wipe out our data
	this.dashboardData.list.length = 0;
	this.messageInfoElement.removeEventListener(Mojo.Event.tap, this.tapHandler);
};

DashboardSendErrorAssistant.prototype.update = function(isNewContent) {
	if (!this.dashboardData || !this.dashboardData.list.length) {
		return;
	}

	var data = this.dashboardData.list;
	
	var dModel = {
		topLine: data[data.length-1].multipleFailures ? this.dashboardData.multipleFailuresError : this.dashboardData.singleMessageError,
		bottomLine: '',
		leftImage: Mojo.appPath + "/images/notification-large.png",
		rightImage: "",
		rightImageClass: "hidden" // setting this to something other than hidden will make it visible
	}	
	
	
	if (data.length > 1) {
		dModel.singleMessageClass = "";
		dModel.errorCount = data.length;
	} else if(data.length == 1) {
		dModel.singleMessageClass = "single";
	}
		
	var renderedInfo = Mojo.View.render({
		object: dModel,
		template: 'dashboardSendError/message-info'
	})
	// Insert the HTML into the DOM, replacing the existing content.
	Element.update(this.controller.get('message_info'), renderedInfo);
	
	if(isNewContent) {
		this.controller.stageController.indicateNewContent(true);
	}
};

DashboardSendErrorAssistant.prototype.tapHandler = function(event){
	var data = this.dashboardData.list;

	var lastMessage = data[data.length - 1];
	if(data.length == 1) {
		if (lastMessage.multipleFailures) {
			this.launchToHistory();
		} else {
			this.launchToChat(lastMessage.chatThreadId);	
		}
	} else {
		var target = event.target;
		// Clicked on the error count -> launch to conversation history view
		MessagingUtils.simpleListClick(target,"palm-dashboard-icon-container",function(newTarget) {
			this.launchToHistory();
		}.bind(this));
	
		// Clicked on the last error -> launch the chatview if there are not multiple failures
		MessagingUtils.simpleListClick(target,"palm-dashboard-text-container",function(newTarget) {
			if (lastMessage.multipleFailures) {
				this.launchToHistory();
			} else {
				this.launchToChat(lastMessage.chatThreadId);			
			}
		}.bind(this));
	}	
};

DashboardSendErrorAssistant.prototype.launchToChat = function(chatThreadId){
	MessagingMojoService.launchMessaging(this.controller, {
		"chatThreadId": chatThreadId
	});	
};

DashboardSendErrorAssistant.prototype.launchToHistory = function(){
	// kill the dashboard because we are launching to the history view
	this.closeDashboard();
	
	MessagingMojoService.launchMessaging(this.controller, {
		launchListView: true, 
		forceListView:this.Messaging.Views.HISTORY
	});
};


DashboardSendErrorAssistant.prototype.closeDashboard = function(){
	this.controller.window.close(); 
};