'use strict';
app.controller('timestampPlansController', ['$scope', '$http', '$modal', '$timeout', '$translate', 'blockUI', 'lib', 'fileUpload', 'authService', function ($scope, $http, $modal, $timeout, $translate, blockUI, lib, fileUpload, authService) {

	// Initialization

	$scope.newPlan = { name: '' };
	$scope.newRequester = { name: '', url: '' };

	var init = function () {
		if (!lib.checkIsAdmin()) {
			return;
		}
		$scope.plans = null;
		$scope.requesters = null;
		$scope.selectedPlan = null;
		$http.get('/Api/TimestampPlans').then(function (response) {
			$scope.plans = response.data;
		}, lib.handleRestError);
	};

	// Plans

	$scope.createPlan = function () {
		if ($scope.newPlan.name !== '') {
			blockUI.start();
			$http.post('/Api/TimestampPlans', $scope.newPlan).then(function (response) {
				$scope.plans.push(response.data);
				$scope.newPlan.name = '';
				blockUI.stop();
			}, lib.handleRestError);
		}
	};

	$scope.selectPlan = function (plan) {
		if ($scope.selectedPlan !== plan) {
			$scope.selectedPlan = plan;
			fetchRequesters();
		}
	};

	$scope.editPlan = function (plan) {
		var modalInstance = $modal.open({
			templateUrl: views.timestampPlanDetails,
			controller: 'timestampPlanDetailsController',
			size: 'md',
			resolve: {
				modelId: function () {
					return plan.id;
				}
			}
		});
		modalInstance.result.then(init);
	};

	// Requesters

	var resetNewRequester = function () {
		$scope.newRequester.name = '';
		$scope.newRequester.url = '';
	};

	var fetchRequesters = function () {
		if (!$scope.selectedPlan) {
			$scope.requesters = null;
		} else {
			blockUI.start();
			$http.get('/Api/TimestampPlans/' + $scope.selectedPlan.id + '/Requesters').then(function (response) {
				$scope.requesters = response.data;
				calculatePriorities();
				blockUI.stop();
			}, lib.handleRestError);
		}
	};

	var calculatePriorities = function () {
		angular.forEach($scope.requesters, function (requester, index) { requester.priority = index + 1; });
	};

	$scope.createRequester = function () {
		$modal.open({
			templateUrl: views.timestampRequesterCreate,
			controller: 'timestampRequesterCreateController',
			size: 'md',
			resolve: {
				model: function () {
					return { planId: $scope.selectedPlan.id, name: $scope.newRequester.name };
				}
			}
		}).result.then(function () {
			resetNewRequester();
			init();
		}, null);
	};

	$scope.editRequester = function (requester) {
		var modalInstance = $modal.open({
			templateUrl: views.timestampRequesterDetails,
			controller: 'timestampRequesterDetailsController',
			size: 'lg',
			resolve: {
				modelId: function () {
					return requester.id;
				}
			}
		});
		modalInstance.result.then(fetchRequesters);
	};

	$scope.requestHistory = function (requester) {
		var modalInstance = $modal.open({
			templateUrl: views.timestampRequestHistory,
			controller: 'timestampRequestHistoryController',
			size: 'lg',
			resolve: {
				modelId: function () {
					return requester.id;
				}
			}
		});
	};

	$scope.moveRequester = function (requester, down) {
		var index = $scope.requesters.indexOf(requester);
		var other = down ? index + 1 : index - 1;
		if (!$scope.originalRequesters) {
			$scope.originalRequesters = angular.copy($scope.requesters);
		}
		$scope.requesters[index] = $scope.requesters[other];
		$scope.requesters[other] = requester;
	};

	// Priorities

	$scope.savePriorities = function () {
		var list = [];
		angular.forEach($scope.requesters, function (requester) { list.push(requester.id); });
		blockUI.start();
		var planId = $scope.selectedPlan.id;
		$http.put('/Api/TimestampPlans/' + planId + '/Priorities', list).then(function (response) {
			$scope.originalRequesters = null;
			calculatePriorities();
			blockUI.stop();
		}, lib.handleRestError);
	};

	$scope.discardPriorities = function () {
		$scope.requesters = $scope.originalRequesters;
		$scope.originalRequesters = null;
	};

	$scope.hasDirtyPriorities = function () {
		return $scope.originalRequesters != null;
	};

	init();
}]);
