//Inspired by https://www.bennadel.com/blog/2806-creating-a-simple-modal-system-in-angularjs.htm

var app = angular.module('solarguessr');

app.service('modals', ['$rootScope', '$q',
	function ($rootScope, $q) {
		var modal = {
			deferred: null,
			params: null
		};
		
		$rootScope.$on('$locationChangeStart', function () {
			reject();
		});
		
		function open(type, params, pipeResponse) {
			var previousDeferred = modal.deferred;
			
			modal.deferred = $q.defer();
			modal.params = params || {};
			
			if (previousDeferred && pipeResponse) {
				modal.deferred.promise
					.then(previousDeferred.resolve)
					.catch(previousDeferred.reject);
			} else if (previousDeferred) {
				previousDeferred.reject();
			}
			
			$rootScope.$emit('modals.open', type);
			
			return modal.deferred.promise;
		}
		
		function params() {
			return modal.params;
		}
		
		function proceedTo(type, params) {
			return open(type, params, true);
		}
		
		function reject(reason) {
			if (!modal.deferred) {
				return;
			}
			
			modal.deferred.reject(reason);
			modal.deferred = modal.params = null;
			
			$rootScope.$emit('modals.close');
		}
		
		function resolve(response) {
			if (!modal.deferred) {
				return;
			}
			
			modal.deferred.resolve(response);
			modal.deferred = modal.params = null;
			
			$rootScope.$emit('modals.close');
		}
		
		return {
			open: open,
			params: params,
			proceedTo: proceedTo,
			reject: reject,
			resolve: resolve
		};
	}
]);
