(function() {
	'use strict';

	angular.module('uniflash').service('ufDialog', dialog);

	dialog.$inject = ['$uibModal'];

	class DSDialog {
		close():void{
			// default close does nothing
		}
	}

	function dialog($uibModal) {


		function createDialog(event, buttonClicked, closeClicked) {

			var createdDialog = new DSDialog();

			ModalController.$inject = ['$scope'];

			function ModalController($scope) {

				var vm = this;
				vm.event = event;

				vm.buttonsEnabled = true;
				vm.buttonClicked = function(button) {
					vm.buttonsEnabled = false;
					return buttonClicked(button)
						.then(function() {
							vm.buttonsEnabled = false;
						});
				};

				vm.getIconType = function(){
					// map icon to a boostrap icon type
					// the exclamation mark is used for 'STOP' and warning is used for 'EXCLAMATION'
					// based on the boostrap icon set; this mapping more sense
					if( vm.event.icon === 'STOP')
						return 'exclamation-sign';
					if ( vm.event.icon === 'EXCLAMATION ')
						return 'warning-sign';
					if ( vm.event.icon === 'INFORMATION')
						return 'info-sign';
	
					return 'question-sign';					
				};

				// when the user dismisses the dialog by clicking the close button
				// we want to pass it on to the creator, we don't explicitly close the dialog overselves
				// that would be done by the creator after they have closed everything successfully and call 'close'
				vm.closeClicked = closeClicked;

				// Expose the dialog $dismiss, so when the onDismiss coming up from the dubugger could be handled
				createdDialog.close = $scope.$dismiss;
			}

			return $uibModal.open({
				templateUrl: 'ds/dialog.html',
				controllerAs: 'vm',
				controller: ModalController,
				backdrop: 'static'
			}).opened.then(function() {
				return createdDialog;
			});

		}

		return {
			createDialog: createDialog
		};
	}

})();