(function () {

	'use strict';

	angular.module('ti_common').service('ticAlerts', alerts);

	alerts.$inject = ['$uibModal', '$log', '$sce'];

	function alerts($uibModal, $log, $sce) {

		function showAlert(type, displayType, message) {

			function AlertControler() {
				var vm = this;
				vm.type = type;
				vm.message = message;
				vm.displayType = displayType;

				vm.toTrustedHTML = function(html) {
					return $sce.trustAsHtml( html );
				}
			}

			var modalInstance = $uibModal.open({
				templateUrl: 'ti_common/alerts/alert.html',
				controllerAs: 'vm',
				controller: AlertControler,
				size: 'lg'
			});

			return modalInstance.result;
		}

		function showConfirm(title, message) {

			function ConfirmControler() {
				var vm = this;
				vm.title = title;
				vm.message = message;
				vm.showNo = true;
				vm.showYes = true;

				vm.toTrustedHTML = function(html) {
					return $sce.trustAsHtml( html );
				}
			}

			var modalInstance = $uibModal.open({
				templateUrl: 'ti_common/alerts/confirm.html',
				controllerAs: 'vm',
				controller: ConfirmControler
			});

			return modalInstance.result;
		}

		function showMessage(title, message, loading) {
			
			function MessageControler() {
				var vm = this;
				vm.title = title;
				vm.message = message;
				vm.showDismiss = true;
				vm.showLoading = loading;

				vm.toTrustedHTML = function(html) {
					return $sce.trustAsHtml( html );
				}
			}

			var modalInstance = $uibModal.open({
				templateUrl: 'ti_common/alerts/confirm.html',
				controllerAs: 'vm',
				controller: MessageControler
			});

			return modalInstance;
		}

		function showError(message) {
			$log.error(message);
			return showAlert('danger', 'Error', message);
		}

		function showInfo(message) {
			$log.info(message);
			return showAlert('info', 'Info', message);
		}

		function showWarning(message) {
			$log.warn(message);
			return showAlert('warning', 'Warning', message);
		}

		function showSuccess(message) {
			$log.debug(message);
			return showAlert('success', 'Success', message);
		}

		return {
			showError: showError,
			showInfo: showInfo,
			showWarning: showWarning,
			showSuccess: showSuccess,
			showConfirm: showConfirm,
			showMessage: showMessage
		};
	}

})();