/*eslint no-alert:0*/
/*global alert: false*/

'use strict';
var WebcentricManager = require('mycompany/WebcentricManager');
var TradeChannelMapper = require('caplin/trading/trademodel/TradeChannelMapper');
var CaplinConfigService = require('caplin/services/providers/CaplinConfigService');
var ServiceRegistry = require('caplin/core/ServiceRegistry');

function App() {
	this._appProperties = {
		'APP.VERSION': 'Version x',
		'CAPLIN.PERMISSION.CONFIG.URL': 'unbundled-resources/permissionDatasourceDefinitions.xml',
		'CAPLIN.SECURITY.PERMISSIONING.STATUS': 'ENABLED',
		'CAPLIN.WEBCENTRIC.START.PAGE': 'blank_layout'
	};
}

/**
 * The start method is called on page load.
 */
App.prototype.start = function() {
	this._initialiseConfigService();

	this._initialiseStreamLink();
	this._startStreamLinkConnection();
	this._initialiseTradeMessageService();
	this._createTradeFactory();
	this._initialiseDelayedRedinessServices();

	this.createTile();
};

/**
 * The stop method is called on page unload.
 */
App.prototype.stop = function() {
	this._stopStreamLinkConnection();
};

/**
 * Initialise ConfigService with app properties.
 */
App.prototype._initialiseConfigService = function() {
	ServiceRegistry.registerService('caplin.config-service', new CaplinConfigService(this._appProperties));
};

/**
 * Create your new tile Presenter component here. Add the element created to the document body.
 */
App.prototype.createTile = function() {
	//var oPresenterComponent = ...
	//document.getElementById('body').appendChild(oPresenterComponent.getElement());
};

/**
 * Set up a StreamLink object and register a the SteamLinkConnectionServce as the service
 * to be used for connecting to the streaming server.
 */
App.prototype._initialiseStreamLink = function() {};

/**
 * Connect to the server using the connection service.
 */
App.prototype._startStreamLinkConnection = function() {};

/**
 * Disconnect from the server using the connection service.
 */
App.prototype._stopStreamLinkConnection = function() {};

/**
 * Initialise the TradeFactory and register it with the TradeService.
 */
App.prototype._createTradeFactory = function() {};

/**
 * Initialise a StreamlinkTradeMessageService and register it with
 * the ServiceRegistry as the as the TradeMessageService.
 */
App.prototype._initialiseTradeMessageService = function() {};

/**
 * Returns a Mapping from Channel Name: <sChannel>, to Object Name (on the server): <sChannel>.
 *
 * @param {string} sChannel - Trade channel.
 * @returns {TradeChannelMapper} Trade channel mapper.
 */
App.prototype._getTradeChannelMapper = function(sChannel) {
	var oTradeChannelMapper = new TradeChannelMapper();
	oTradeChannelMapper.getChannel = function() {
		return sChannel;
	};
	oTradeChannelMapper.getObjectList = function() {
		return sChannel;
	};

	return oTradeChannelMapper;
};

/**
 * Initialise the User and PermissionService, services implementing the
 * caplin.core.DelayedReadinessService interface.
 */
App.prototype._initialiseDelayedRedinessServices = function() {};

/**
 * Callback for successfully loading user and permission services.
 */
App.prototype._onServicesInitialisedSuccess = function() {};

/**
 * Callback for unsuccessfully loading user and permission services.
 *
 * @param {string} errorMessage - Error message.
 */
App.prototype._onServicesInitialisedError = function(errorMessage) {
	alert('An error occurred while initialising services: ' + errorMessage);
};

App.prototype._startWebcentric = function() {
	var username = ServiceRegistry.getService('caplin.user-service').getLoginName();
	WebcentricManager.start(username, 'unbundled-resources/webcentric/', 'noir');
};

App.prototype._stopWebcentric = function() {
	WebcentricManager.stop();
};

module.exports = App;
