'use strict';
var i18n = require('br/I18n');
var Utility = require('caplin/dom/Utility');
var WebcentricAPI = require('caplin/webcentric/core/WebcentricAPI');
var ServiceRegistry = require('caplin/core/ServiceRegistry');

var AppUtils = {};

/**
 * Gets a map of all the HTML query parameters
 * @returns {Object} a map of parameter names to values
 */
AppUtils.getQueryParams = function() {
	var mParams = {};
	if (window.location.search) {
		var sSearch = window.location.search.replace(/\?/, '');
		var pParams = sSearch.split(/\&/);
		for (var i = 0; i < pParams.length; i++) {
			var pParam = pParams[i].split(/=/);
			mParams[pParam[0]] = pParam[1];
		}
	}
	return mParams;
};

/**
 * Displays the given message in a status bar
 * @param {String} message the message to display in the status bar.
 */
AppUtils.statusMessage = function(message) {
	var eProgressMessage = document.getElementById('progress-message');
	var eProgressBar = document.getElementById('progressImage');

	if (eProgressMessage) {
		eProgressMessage.innerHTML = message;
	}

	if (eProgressBar) {
		var nProgressIncrement = 100 / 5;
		var sCurrentWidth = parseInt(eProgressBar.style.width, 10);

		eProgressBar.style.width = sCurrentWidth + nProgressIncrement + '%';
	}
};

/**
 * Shows an about window with the application version.
 */
AppUtils.showAboutWindow = function() {
	var sVersion = ServiceRegistry.getService('caplin.services.AppService').getVersion();
	var oPrompter = ServiceRegistry.getService('caplin.services.UserPromptService');
	oPrompter.alert(sVersion);
};

/**
 * Shows the help window with the given help page.
 * @param {String} helpPageToShow the page to show
 * @param {boolean} isBuiltEvent - Is build event.
 */
AppUtils.showHelpWindow = function(helpPageToShow, isBuiltEvent) {
	var activeLayout = WebcentricAPI.getActiveLayout();
	var properties = activeLayout.properties;

	if (isBuiltEvent && WebcentricAPI.getUserProperty('show_welcome_hints') === 'false') {
		return;
	}

	if (!helpPageToShow) {
		if (properties.style === 'new_layout') {
			window.showHelpPage = 'NEW_SHEET';
		} else {
			window.showHelpPage = 'DEFAULT';
		}
	} else {
		window.showHelpPage = helpPageToShow;
	}

	var options = 'width=600,height=600,status=yes,resizable=yes';
	var helpPageUrl = '/fxtrader/default-aspect/unbundled-resources/help/indexHelp.html';
	var helper = window.open(helpPageUrl + '?domain=' + document.domain, 'HelpWindow', options);

	if (helper) {
		this._focusHelpWindow(helper);
	} else {
		var oPrompter = ServiceRegistry.getService('caplin.services.UserPromptService');
		oPrompter.alert(i18n('ct.framework.disable.popup.blocker'));
	}
};

AppUtils._focusHelpWindow = function(oHelper, nAttemptNumber) {
	nAttemptNumber = nAttemptNumber || 1;
	try {
		oHelper.focus();
	} catch (e) {
		var oPrompter = ServiceRegistry.getService('caplin.services.UserPromptService');
		oPrompter.alert(i18n('ct.framework.help.focus.fail'));
	}
};

/**
 * Suppress HTML selects within the current page.
 */
AppUtils.suppressUnwantedSelects = function() {
	// prevent browser selections
	Utility.addEventListener(document.body, 'selectstart', function(evt) {
		var oSrc = evt.srcElement || evt.target;
		if (oSrc.tagName && oSrc.tagName.match(/^INPUT$/)) {
			return true;
		}
		return false;

	}, true);

};

module.exports = AppUtils;
