var mfFlashMessage = (function ($) {

	var _messages = {},
		_lastCreated,
		_activeFlash = false,
		bodyEl = 'body',
		containerEl = '#flashMessageContainer',
		messageEl = '.flashMessage';


	/**
	 * default options
	 * @type {{}}
	 */
	var _defaults = {
		template: 'fade', // fade, confirm
		delay: 5000, // (template: fade)
		status: 'info', // info, warn, error
		closeButton: 'Dismiss', // Default close text (template: fixed)
		additionalButton: false,
		message: ""
	};


	/**
	 * Create a FlashMessage and return uuid
	 *
	 * @param message FlashMessage as String
	 * @param options Obj {} will be merged with defaults (optional arg)
	 */
	function create(message, options) {
		var tmp = {};
		$.each(_defaults, function (key, value) {
			tmp[key] = value;
		});

		if (options) {
			options = jQuery.extend(tmp, options);
		} else {
			options = Object.assign({}, tmp);
		}

		if (message) {
			options.message = message;
		}

		var uuid = mfUtility.guid();
		_messages[uuid] = options;
		_lastCreated = uuid;
		// console.log('message created', uuid);
	}


	/**
	 * This function will render all flashmessages.
	 */
	function render() {
		var flashContainer;

		// Initialize the flashMessage Container
		if (!$(bodyEl + " " + containerEl).length) {
			flashContainer = $('<div id="' + containerEl.replace('#', '') + '"></div>');
			$(bodyEl).prepend(flashContainer);
		} else {
			flashContainer = $(bodyEl + ' ' + containerEl);
		}

		var timeToWait = 0;

		// Add messages to it
		$.each(_messages, function (key, value) {
			var msg = $('<div class="' + messageEl.replace('.', '') + ' card-3 card ' + value.status + '" data-uuid="' + key + '"><div class="content">' + value.message + '</div></div>');

			// console.log('status:', value.status);
			if (!$(bodyEl + ' ' + containerEl + ' [data-uuid="' + key + '"]').length) {
				if (value.template === 'fade') {
					$(flashContainer).append(msg);
					// console.log('append', key);

					setTimeout(function () {
						$(msg).show();
						// console.log('fade in', key);

						setTimeout(function () {
							clear(key);
						}, value.delay);

					}, timeToWait);

					timeToWait += value.delay + 500;

				} else if (value.template === 'confirm') {
					$(msg).addClass('confirm');
					$(msg).append('<div class="action-bar text-right"><span class="btn primary flat dismiss">' + value.closeButton + '</span></div>');
					$(flashContainer).append(msg);

					$(msg).on('click', '.dismiss', function () {
						clear(key);
					});

					// if( _activeFlash === false ) {
					//     _activeFlash = true;
					$(msg).show();
					// }
				}
			}
		});
	}


	/**
	 * Function to show the next confirm flashmessage
	 */
	function showNextConfirmFlash() {
		$(containerEl).children(messageEl).each(function () {
			if (_activeFlash === false && $(this).hasClass('confirm')) {
				$(this).show();
			}
		});
	}


	/**
	 * Method to remove a flashMessage
	 * @param uuid
	 * @param callback
	 */
	function clear(uuid, callback) {
		var message = _messages[uuid];
		// console.log('clear', uuid);

		if (message.template === 'fade') {
			$('[data-uuid="' + uuid + '"]').fadeOut(function () {
				$(this).remove();
				// console.log('element removed');
				delete _messages[uuid];
				// console.log('message deleted', _messages);
				_activeFlash = false;
				if (callback) callback();
			});
		} else {
			$('[data-uuid="' + uuid + '"]').remove();
			delete _messages[uuid];
			_activeFlash = false;
			if (callback) callback();
		}
	}


	return {
		create: function (message, options) {
			create(message, options);
			return this;
		},
		render: function (uuid) {
			render(uuid);
			return this;
		}
	};

})(jQuery);