
//----------------------------------------------------------------------------------------------------------------------

function MessageScreen() {
	var self = this;

	var backButton = document.getElementById( "back_message_button" );
	backButton.onclick = function() {
		 self.onBackClicked();
	};

	var deleteButton = document.getElementById( "delete_message_button" );
	deleteButton.onclick = function() {
		 self.onDeleteClicked();
	};

	var newButton = document.getElementById( "new_message_button" );
	newButton.onclick = function() {
		 self.onNewClicked();
	};

	var unreadButton = document.getElementById( "unread_message_button" );
	unreadButton.onclick = function() {
		 self.onUnreadClicked();
	};

	var flagButton = document.getElementById( "flag_message_button" );
	flagButton.onclick = function() {
		 self.onFlagClicked();
	};

	var unflagButton = document.getElementById( "unflag_message_button" );
	unflagButton.onclick = function() {
		 self.onUnflagClicked();
	};
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.msg_id;
MessageScreen.prototype.show;

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onActivated = function() {
	// Add event listeners.
	var self = this;
	fbService.onSuccess = function( response ) { self.onFatBookieViewMessageResponse( response ) }
	fbService.onError = function( status ) { self.onFBServiceError( status ) }

	// Start the request.
	fbService.getMessage(self.show, self.msg_id);
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onBackClicked = function() {
	var self = this;

	if(self.show == "outbox")
		widgetMenu.activate( Menu.OUTBOX_SCREEN );
	else
		widgetMenu.activate( Menu.INBOX_SCREEN );

}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onDeleteClicked = function() {
	
	if(confirm( StringTable.Code.deleteMessageWarning ))
	{
		// Add event listeners.
		var self = this;
		fbService.onSuccess = function( response ) { self.onFatBookieDeleteMessageResponse( response ) }
		fbService.onError = function( status ) { self.onFBServiceError( status ) }

		// Start the request.
		fbService.messageDelete(self.show, self.msg_id);
	}
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onNewClicked = function() {

	var self = this;

	if(self.show == "inbox") //this action works only on inbox messages
	{
		//activate the message screen
		widgetMenu.activate( Menu.REPLY_SCREEN );
	}
	else
		alert(StringTable.Code.replyMessageWarning);
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onUnreadClicked = function() {
	
	var self = this;

	if(self.show == "inbox") //this action works only on inbox messages
	{
		// Add event listeners.
		fbService.onSuccess = function( response ) { self.onFatBookieMarkUnreadMessageResponse( response ) }
		fbService.onError = function( status ) { self.onFBServiceError( status ) }

		// Start the request.
		fbService.messageMarkUnread(self.msg_id);
	}
	else
		alert(StringTable.Code.markUnreadMessageWarning);

}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onFlagClicked = function() {
	
	// Add event listeners.
	var self = this;
	fbService.onSuccess = function( response ) { self.onFatBookieFlagMessageResponse( response ) }
	fbService.onError = function( status ) { self.onFBServiceError( status ) }

	// Start the request.
	fbService.messageFlag(self.show, self.msg_id);
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onUnflagClicked = function() {
	
	// Add event listeners.
	var self = this;
	fbService.onSuccess = function( response ) { self.onFatBookieUnflagMessageResponse( response ) }
	fbService.onError = function( status ) { self.onFBServiceError( status ) }

	// Start the request.
	fbService.messageUnflag(self.show, self.msg_id);
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onFatBookieViewMessageResponse = function( response ) {

	var self = this;

	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			this.onFBServiceError(response.body);
		else
		{
			if(response.date != undefined) {
				var messageTable = document.getElementById( "message_view_table" );
				var html = "";

				// Fill in the table.
				var v_date = new Date( (response.date).replace(/\-/g,"/") );

				html += Helper.createViewMessage(	response.party, 
													response.party_name, 
													response.subject, 
													response.body, 
													v_date.toLocaleDateString(), 
													response.flag,
													this.show
												);
				
				messageTable.innerHTML = html;

				//prepare data needed in case user decides to reply
				replyScreen.msg_id = self.msg_id;
				replyScreen.msg_reply_to = (response.party_name == "") ? response.party : response.party_name;
				replyScreen.msg_subject = response.subject;
			}
			else
			{
				var messageTable = document.getElementById( "message_view_table" );
				var html = "<tr><th>&nbsp;</th></tr>";

				messageTable.innerHTML = html;
			}
		}
	}
	else
		alert( StringTable.Code.fatalAPIError );
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onFatBookieDeleteMessageResponse = function( response ) {
	
	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			this.onFBServiceError(response.body);
		else
		{
			alert(response.body);

			this.onBackClicked(); //return to messages list
		}
	}
	else
		alert( StringTable.Code.fatalAPIError );
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onFatBookieMarkUnreadMessageResponse = function( response ) {

	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			this.onFBServiceError(response.body);
		else
			this.onBackClicked(); //return to messages list. A new getMessage call would mark this message as read again.
	}
	else
		alert( StringTable.Code.fatalAPIError );
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onFatBookieFlagMessageResponse = function( response ) {

	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			this.onFBServiceError(response.body);
		else
			this.onActivated(); //retrieve the message from server again to confirm flag
	}
	else
		alert( StringTable.Code.fatalAPIError );
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onFatBookieUnflagMessageResponse = function( response ) {
	
	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			this.onFBServiceError(response.body);
		else
			this.onActivated(); //retrieve the message from server again to confirm flag
	}
	else
		alert( StringTable.Code.fatalAPIError );
}

//----------------------------------------------------------------------------------------------------------------------

MessageScreen.prototype.onFBServiceError = function( status ) {
	// Alert the user that something has gone wrong.
	if(status == "Invalid session")
	{
		alert( StringTable.Code.messageScreenInvalidSessionError );

		// Load the login screen to allow user to get a new session
		widgetMenu.activate( Menu.LOGIN_SCREEN );
	}
	else
		alert( StringTable.Code.messageScreenDataError + " (" + status + ")" );
}

//----------------------------------------------------------------------------------------------------------------------
