/**
 * @class
 * A class to manage the WebCentric layout manager
 */

'use strict';
var i18n = require('br/I18n');
var WebcentricAPI = require('caplin/webcentric/core/WebcentricAPI');
var ServiceRegistry = require('caplin/core/ServiceRegistry');
var Launcher = require('caplin/webcentric/Launcher');
var Application = require('caplin/webcentric/presentation/Application');

/**
 * @class
 * A class to manage the WebCentric layout manager
 */

var WebcentricManager = {};
WebcentricManager._hasWebcentricStarted = false;

/**
 * Start the WebCentric instance using the given username and theme
 * @param {String} username the user name used to retrieve the WebCentric layout.
 * @param {String} themePath the path to the css theme
 * @param {String} theme the name of the css theme
 */
WebcentricManager.start = function(username, themePath, theme) {
	window.webcentricParams = {
		'theme_path': themePath,
		theme: theme,
		'layout_theme': theme
	};

	if (!WebcentricManager._hasWebcentricStarted) {
		if (username) {
			setTimeout("require('mycompany/AppUtils').statusMessage('" + i18n('ct.framework.message.loading_config') + "');", 10);
			Launcher.start_webcentric(function() {});
			WebcentricManager._hasWebcentricStarted = true;
		} else {
			setTimeout("require('mycompany/AppUtils').statusMessage('" + i18n('ct.framework.auth.ret.user.creds') + "');", 10);
		}
	}
};

/**
 * Asks the user to confirm logout, called when the user invokes the logout action in the application.
 */
WebcentricManager.ConfirmLogout = function() {
	var oSavedSheets = [];

	if (Application.logout === false) {

		Application.logout = true;

		var sMessage = i18n('ct.framework.confirm.logout');
		if (oSavedSheets.returnValue) {
			sMessage = sMessage + oSavedSheets.returnValue;
		} else {
			sMessage = sMessage + i18n('ct.framework.confirm.logout.unsaved.changes');
		}

		var fHandleLogout = WebcentricManager.logout.bind(this);
		var oPrompter = ServiceRegistry.getService('caplin.services.UserPromptService');
		oPrompter.confirm(sMessage, fHandleLogout, i18n('ct.core.command.ok'), i18n('ct.core.command.cancel'));
	} else {
		WebcentricManager.logout(true);
	}
};

/**
 * Check if the supplied xhr is the login page and exit the application if it is.
 *
 * Note : This method is also called from within WebCentric. (request_manager.js)
 *
 * @param {XMLHttpRequest} oXhr - The request to be checked.
 * @return {boolean} True if oXhr is not the login page and processing should continue, otherwise false.
 */
WebcentricManager.sessionTimeoutHandler = function sessionTimeoutHandler(oXhr) {
	var sReloginMarker = 'LOGIN_PAGE_APPLICATION_SERVER_RELOGIN_REQUIRED';
	//JT removed setting of marker from ApplicationProperties:CAPLIN.FRAMEWORK.SESSION.REFRESH.LOGINPAGE.IDENTIFIER

	if (oXhr && oXhr.responseText && oXhr.responseText.indexOf('//' + sReloginMarker) !== -1) {
		WebcentricManager.cleanUpBeforeUnloadAndLogUserOut();
		return false;
	}
	return true;
};

/**
 * Stops WebCentric
 */
WebcentricManager.stop = function() {
	WebcentricAPI.removeBeforeUnload();
	Application.shutDown();
};

module.exports = WebcentricManager;
