'use strict';
app.controller('securityContextDetailsController', ['$scope', '$http', '$modalInstance', '$timeout', '$modal', '$translate', 'blockUI', 'lib', 'fileUpload', 'securityContextId', 'canEditSystem', function ($scope, $http, $modalInstance, $timeout, $modal, $translate, blockUI, lib, fileUpload, securityContextId, canEditSystem) {

	$scope.model = null;
	$scope.renaming = false;
	$scope.newName = null;
	$scope.trustedRoots = null;
	$scope.additionalArbitrators = null;
	$scope.editingArbitrators = false;
	$scope.isEditable = false;

	var init = function () {
		blockUI.start();
		$http.get('/Api/SecurityContexts/' + securityContextId).then(function (response) {
			readModel(response.data);
			blockUI.stop();
		}, function (response) {
			lib.showRestError(response, function () {
				$modalInstance.close();
			});
		});
	};

	var readModel = function (data) {
		$scope.model = data;
		$scope.isEditable = data.type === 'Custom' || (canEditSystem && data.type === 'System');
		$scope.additionalArbitrators = [];
		angular.forEach(data.additionalStandardArbitrators, function (value, key) {
			if (value) {
				$scope.additionalArbitrators.push(key);
			}
		});
		if (data.type !== 'Standard') {
			$http.get('/Api/SecurityContexts/' + securityContextId + '/TrustedRoots').then(function (response) {
				$scope.trustedRoots = response.data;
			}, lib.handleRestError);
		}
	};

	$scope.editArbitrators = function () {
		$scope.editingArbitrators = true;
		$scope.newAdditionalArbitrators = angular.copy($scope.model.additionalStandardArbitrators);
	};

	$scope.saveArbitrators = function () {
		$http.put('/Api/SecurityContexts/' + securityContextId, {
			additionalStandardArbitrators: $scope.newAdditionalArbitrators
		}).then(function (response) {
			readModel(response.data);
			$scope.editingArbitrators = false;
		}, lib.handleRestError);
	};

	$scope.discardArbitrators = function () {
		$scope.editingArbitrators = false;
	};

	$scope.rename = function () {
		$scope.newName = $scope.model.name;
		$scope.renaming = true;
	};

	$scope.renameConfirm = function () {
		$http.put('/Api/SecurityContexts/' + securityContextId, {
			name: $scope.newName
		}).then(function (response) {
			readModel(response.data);
			$scope.renaming = false;
		}, lib.handleRestError);
	};

	$scope.renameCancel = function () {
		$scope.renaming = false;
	};

	$scope.trustedRootFileChanged = function () {
		$timeout(uploadTrustedRoot, 200);
	};

	var uploadIntermediateCA = function () {
		var file = $scope.trustedRootFile;
		var url = '/Api/IntermediateCAs';
		if ($scope.model.type === 'System') {
			url = '/Api/Admin/IntermediateCAs';
		}
		blockUI.start();
		fileUpload.uploadFileToUrl(file, url).then(function (response) {
			blockUI.stop();
		}, lib.handleRestError);
	};

	var confirmIntermediateCA = function () {
		$translate(['cscdIntermediateCA', 'warning', 'true', 'cancel']).then(function (msgs) {
			var buttons = {};
			buttons[msgs['true']] = {
				css: 'btn-info',
				click: function (modalInstance) {
					modalInstance.close();
					uploadIntermediateCA();
				}
			};
			buttons[msgs.cancel] = {
				css: 'btn-default',
				click: function (modalInstance) {
					modalInstance.close();
				}
			};
			lib.showMessageBox(msgs.cscdIntermediateCA, msgs.warning, buttons);
		});
	};

	var uploadTrustedRoot = function () {
		var file = $scope.trustedRootFile;
		var url = '/Api/SecurityContexts/' + securityContextId + '/TrustedRoots';
		blockUI.start();
		fileUpload.uploadFileToUrl(file, url).then(function (response) {
			$scope.trustedRoots.push(response.data);
			blockUI.stop();
		}, function (response) {
			blockUI.stop();
			if (response.data.code === 'CACertificate') {
				confirmIntermediateCA();
			} else {
				lib.handleRestError(response);
			}
		});
	};

	$scope.removeTrustedRoot = function (rootCert) {
		var url = '/Api/SecurityContexts/' + securityContextId + '/TrustedRoots/' + rootCert.thumbprint;
		$http.delete(url).then(function (response) {
			var index = $scope.trustedRoots.indexOf(rootCert);
			$scope.trustedRoots.splice(index, 1);
		}, lib.handleRestError);
	};

	$scope.showTrustedRoot = function (rootCert) {
		var url = '/Api/SecurityContexts/' + securityContextId + '/TrustedRoots/' + rootCert.thumbprint;
		lib.showCertificate(url, url + '/DownloadLink');
	};

	$scope.remove = function () {
		$translate('cscdConfirmRemove', { name: $scope.model.name }).then(function (confirmMsg) {
			$translate(['areYouSure', 'yesRemove', 'cancel']).then(function (msgs) {
				var buttons = {};
				buttons[msgs.yesRemove] = {
					css: 'btn-danger',
					click: removeSecurityContext
				};
				buttons[msgs.cancel] = {
					css: 'btn-default',
					click: function (modalInstance) {
						modalInstance.close();
					}
				};
				lib.showMessageBox(confirmMsg, msgs.areYouSure, buttons);
			});
		});
	};

	var removeSecurityContext = function (confirmModal) {
		blockUI.start();
		var url = '/Api/SecurityContexts/' + securityContextId;
		$http.delete(url).then(function (response) {
			blockUI.stop();
			confirmModal.close();
			$scope.close();
		}, lib.handleRestError);
	};

	$scope.close = function () {
		$modalInstance.close();
	};

	init();
}]);
