(function () {

	'use strict';

	angular.module('uniflash').service('ufConsole', consoleSrv);

	var messages = [];
	var newMessageListener;

	var maxBufferSize = 1000;

	// public console service for adding messages
	consoleSrv.$inject = [];

	function consoleSrv() {
		function addMessage(type, text) {
			if ( !type ) {
				type = 'INFO';
			}

			if (messages.length === maxBufferSize) {
				// we reached the max size.. lez pop one off
				messages.shift();
			}

			var d = new Date();
			var formatDate = d.toLocaleString();       // -> "2/1/2013 7:37:08 AM"

			messages.push({
				type: type,
				text: text,
				date: formatDate
			});

			if (newMessageListener)
				newMessageListener();
		}

		var msgTypes = {
			INSTALL: 'Install',
			STATUS: 'Status',
			GEL: 'GEL',
			UPDATE: 'Update'
		};

		function clearMessages() {
			messages = [];
		}

		return {
			addMessage: addMessage,
			clearMessages: clearMessages,
			msgTypes: msgTypes
		};

	}

	angular.module('uniflash').service('ufConsoleInternal', consoleSrvInternal);

	function consoleSrvInternal() {
        let closed = false;

        function close() {
            closed = true;
        }

        function open() {
            closed = false;
        }

        function isClosed() {
            return closed;
        }

		function getMessages() {
			return messages;
		}

		function clearMessages() {
			messages = [];
		}

		return {
            close: close,
            open: open,
            isClosed: isClosed,
			getMessages: getMessages,
			clearMessages: clearMessages,
			addNewMessageListener: function (listener) {
				newMessageListener = listener;
			}
		};
	}

})();