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

	$scope.systemContexts = null;
	$scope.intermediateCAs = null;
	$scope.newName = '';
	$scope.hasLacunaSecurityContext = false;
	$scope.lacunaSecurityContextCreated = false;

	var lacunaSecurityContextId = "803517ad-3bbc-4169-b085-60053a8f6dbf";

	var init = function () {
		if (!lib.checkIsAdmin()) {
			return;
		}
		$http.get('/Api/SecurityContexts/' + lacunaSecurityContextId).success(function (response) {
			$scope.hasLacunaSecurityContext = true;
		});

		$http.get('/Api/Admin/SecurityContexts').then(function (response) {
			$scope.systemContexts = [];
			for (var i = 0; i < response.data.length; i++) {
				var c = response.data[i];
				$scope.systemContexts.push(c);
			}
		}, lib.handleRestError);
		$http.get('/Api/Admin/IntermediateCAs').then(function (response) {
			$scope.intermediateCAs = response.data;
		}, lib.handleRestError);
	};

	$scope.createSecurityContext = function () {
		if ($scope.newName === '') {
			$translate(['message', 'scFillContextName']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.scFillContextName);
			});
			return;
		}
		blockUI.start();
		$http.post('/Api/Admin/SecurityContexts', {
			name: $scope.newName
		}).then(function (response) {
			$scope.systemContexts.push(response.data);
			$scope.newName = '';
			blockUI.stop();
		}, lib.handleRestError);
	};

	$scope.showContext = function (context) {
		var modalInstance = $modal.open({
			templateUrl: views.securityContextDetails,
			controller: 'securityContextDetailsController',
			size: 'lg',
			resolve: {
				securityContextId: function () { return context.id; },
				canEditSystem: function () { return true; }
			}
		});
		modalInstance.result.then(init, init);
	};

	$scope.showCA = function (ca) {
		$translate(['remove']).then(function (msgs) {
			var url = '/Api/IntermediateCAs/' + ca.thumbprint;
			var buttons = {};
			buttons[msgs.remove] = {
				css: 'btn-danger',
				click: removeCA
			};
			lib.showCertificate(url, url + '/DownloadLink', buttons);
		});
	};

	var removeCA = function (certDetailsModalInstance, certModel) {
		$translate('scConfirmCARemoval', { name: certModel.subjectDisplayName }).then(function (confirmMsg) {
			$translate(['areYouSure', 'yesRemove', 'cancel']).then(function (msgs) {
				var buttons = {};
				buttons[msgs.yesRemove] = {
					css: 'btn-danger',
					click: function (confirmModalInstance) {
						blockUI.start();
						$http.delete('/Api/Admin/IntermediateCAs/' + certModel.thumbprint).then(function (response) {
							blockUI.stop();
							confirmModalInstance.close();
							certDetailsModalInstance.close();
							init();
						}, lib.handleRestError);
					}
				};
				buttons[msgs.cancel] = {
					css: 'btn-default',
					click: function (confirmModalInstance) {
						confirmModalInstance.close();
					}
				};
				lib.showMessageBox(confirmMsg, msgs.areYouSure, buttons);
			});
		});
	};

	$scope.caFileChanged = function () {
		$timeout(uploadCA, 200);
	};

	var uploadCA = function () {
		var file = $scope.caFile;
		if (file != null) {
			var url = '/Api/Admin/IntermediateCAs';
			fileUpload.uploadFileToUrl(file, url).then(function (response) {
				$scope.intermediateCAs.push(response.data);
			}, lib.handleRestError);
		}
	};

	$scope.addLacunaSecurityContext = function () {
		$translate('scConfirmLacunaSecurityContext').then(function (confirmMsg) {
			$translate(['areYouSure', 'true', 'cancel']).then(function (msgs) {
				var buttons = {};
				buttons[msgs.true] = {
					css: 'btn-primary',
					click: function (confirmModalInstance) {
						blockUI.start();
						$http.post('/Api/Admin/SecurityContexts/Lacuna').then(function (response) {
							blockUI.stop();
							confirmModalInstance.close();
							$scope.lacunaSecurityContextCreated = true;
						}, lib.handleRestError);
					}
				};
				buttons[msgs.cancel] = {
					css: 'btn-default',
					click: function (confirmModalInstance) {
						confirmModalInstance.close();
					}
				};
				lib.showMessageBox(confirmMsg, msgs.areYouSure, buttons);
			});
		});
	}

	init();
}]);
