
var controller = (function($) {
	
	var attributes;

	var connectorClient = qmatic.connector.client;
	
	var wwClient = qmatic.webwidget.client,
	attributeKeys = {
		WORKSTATION_TEXT                 	: 'workstation.text',
		WORKSTATION_NUMBER_BELOW_TEXT      	: 'number.below.text',
		WORKSTATION_SHOW_SHADOW				: 'show.shadow',
		WORKSTATION_BACKGROUND_COLOR		: 'workstation.background.color',
		WORKSTATION_NUMBER_COLOR			: 'workstation.number.color',
		WORKSTATION_NUMBER_FONT				: 'workstation.number.font',
		WORKSTATION_STATION_COLOR			: 'workstation.station.color',
		WORKSTATION_STATION_FONT			: 'workstation.station.font'
	},
	attributeDefaults = {};	
	
	
	attributeDefaults[attributeKeys.WORKSTATION_TEXT] = 'Workstation';
	attributeDefaults[attributeKeys.WORKSTATION_NUMBER_BELOW_TEXT] = 'false';
	attributeDefaults[attributeKeys.WORKSTATION_SHOW_SHADOW] = 'false';
	attributeDefaults[attributeKeys.WORKSTATION_BACKGROUND_COLOR] = '#E5EDFF';
	attributeDefaults[attributeKeys.WORKSTATION_NUMBER_COLOR] = '#000000';
	attributeDefaults[attributeKeys.WORKSTATION_NUMBER_FONT] = 'Helvetica;46px;normal;normal';
	attributeDefaults[attributeKeys.WORKSTATION_STATION_COLOR] = '#000000';
	attributeDefaults[attributeKeys.WORKSTATION_STATION_FONT] = 'Helvetica;28px;normal;normal';
	/**
	 * If starting part of unit id from request matches the unit id from getServicePointsByDeviceType
	 *
	 * baseUnitId - expected format : BRA:ServicePoint_PositionalDisplay3
	 * dynamicBaseUnit - expected formats : BRA:ServicePoint_PositionalDisplay3 
			BRA:ServicePoint_PositionalDisplay3:SW_POSITIONAL_DISPLAY, 
			BRA:ServicePoint_PositionalDisplay3:SW_POSITIONAL_DISPLAY:1
	 */	
	var isUnitIdMatching = function(baseUnitId, dynamicBaseUnit) {
		var baseUnitIdLength = baseUnitId.length;
		var result = (baseUnitId == dynamicBaseUnit.substring(0, baseUnitIdLength));
		return result;	
	};

	// Public contents of controller
	return {
		/**
		 * Called when widget client has finished loading. This is where you initialise your widget
		 */
		onLoaded : function(configuration) {
			var attr = configuration.attributes;
			
			var attribParser = new qmatic.webwidget.AttributeParser(attr || {}),
				defaultsParser = new qmatic.webwidget.AttributeParser(attributeDefaults),
				ak = attributeKeys,				
				
				workstationBackgroundColor = attribParser.getString(ak.WORKSTATION_BACKGROUND_COLOR, attributeDefaults[ak.WORKSTATION_BACKGROUND_COLOR]),
				workstationNumberColor = attribParser.getString(ak.WORKSTATION_NUMBER_COLOR, attributeDefaults[ak.WORKSTATION_NUMBER_COLOR]),
				workstationNumberFontSize = attribParser.getFontSize(ak.WORKSTATION_NUMBER_FONT, defaultsParser.getFontSize(ak.WORKSTATION_NUMBER_FONT)),
				workstationNumberFontFamily = attribParser.getFontFamily(ak.WORKSTATION_NUMBER_FONT, defaultsParser.getFontFamily(ak.WORKSTATION_NUMBER_FONT)),
				workstationNumberFontStyle = attribParser.getFontStyle(ak.WORKSTATION_NUMBER_FONT, defaultsParser.getFontStyle(ak.WORKSTATION_NUMBER_FONT)),
				workstationNumberFontWeight = attribParser.getFontWeight(ak.WORKSTATION_NUMBER_FONT, defaultsParser.getFontWeight(ak.WORKSTATION_NUMBER_FONT)),
				workstationStationColor = attribParser.getString(ak.WORKSTATION_STATION_COLOR, attributeDefaults[ak.WORKSTATION_STATION_COLOR]),
				workstationStationFontSize = attribParser.getFontSize(ak.WORKSTATION_STATION_FONT, defaultsParser.getFontSize(ak.WORKSTATION_STATION_FONT)),
				workstationStationFontFamily = attribParser.getFontFamily(ak.WORKSTATION_STATION_FONT, defaultsParser.getFontFamily(ak.WORKSTATION_STATION_FONT)),
				workstationStationFontStyle = attribParser.getFontStyle(ak.WORKSTATION_STATION_FONT, defaultsParser.getFontStyle(ak.WORKSTATION_STATION_FONT)),
				workstationStationFontWeight = attribParser.getFontWeight(ak.WORKSTATION_STATION_FONT, defaultsParser.getFontWeight(ak.WORKSTATION_STATION_FONT));
			
			var workstationText = attribParser.getString(ak.WORKSTATION_TEXT, attributeDefaults[ak.WORKSTATION_TEXT]);
			var numberBelowText = attribParser.getBoolean(ak.WORKSTATION_NUMBER_BELOW_TEXT, attributeDefaults[ak.WORKSTATION_NUMBER_BELOW_TEXT]);
			var showShadow = attribParser.getBoolean(ak.WORKSTATION_SHOW_SHADOW, attributeDefaults[ak.WORKSTATION_SHOW_SHADOW]);
			
			attributes = attr;
			$('#content').css('text-align','center');
			$('#content').css('background-color', workstationBackgroundColor);
			if(showShadow) {
				$('#content').css('border-radius','15px 15px 15px 15px'); 
				$('#content').css('box-shadow','3px 2px 2px #C3C3C3');
			} else {
				$('#content').css('border-radius',''); 
				$('#content').css('box-shadow','');
			}
			// For simplicity, two different codepaths...
			if(!numberBelowText) {				
				$('#content').html('<span id="workstationName">' + workstationText + '</span>&nbsp;' +
						'<span id="workstationNumber"></span>');
				
			} else {				
				$('#content').html('<div style="text-align:center;">' +
						'<div id="workstationName">' + workstationText + '</div>' +
						'<div id="workstationNumber"></div>');				
			}
			$('#workstationName').css('font-family', workstationStationFontFamily);
			$('#workstationName').css('font-size', workstationStationFontSize);
			$('#workstationName').css('font-style', workstationStationFontStyle);
			$('#workstationName').css('font-weight', workstationStationFontWeight);
			$('#workstationName').css('color', workstationStationColor);
			
			$('#workstationNumber').css('font-family', workstationNumberFontFamily);
			$('#workstationNumber').css('font-size', workstationNumberFontSize);
			$('#workstationNumber').css('font-style', workstationNumberFontStyle);
			$('#workstationNumber').css('font-weight', workstationNumberFontWeight);
			$('#workstationNumber').css('color', workstationNumberColor);
			

			
			var servicePoints = connectorClient.getServicePointsByDeviceType(wwClient.getBranchId(), 'SW_SERVICE_POINT');
			
			if(servicePoints.length == 0) {
				$('#workstationNumber').text('ERROR: No Service Point assigned to this display!');
			} else if(servicePoints.length == 1) {
				if(isUnitIdMatching(servicePoints[0].unitId, wwClient.getUnitId())) {
					$('#workstationNumber').text(servicePoints[0].id);
				} else {
					$('#workstationNumber').text('ERROR: No Service Point assigned to this display! (1 other service point exists)');
				}
				
			} else {
				$('#workstationNumber').empty();
				for(var i = 0; i < servicePoints.length;i++) {
					if(isUnitIdMatching(servicePoints[i].unitId, wwClient.getUnitId())) {
						$('#workstationNumber').append(servicePoints[i].id + ' ');
					}					
				}	
			}
		},
		
		onLoadError : function(message) {
			$('body').append('<p>Widget load error: ' + message + '</p>');
		}
	};
	
	
})(jQuery);