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

	$scope.customContexts = null;
	$scope.builtinContexts = null;
	$scope.intermediateCAs = null;
	$scope.newName = '';

	var init = function () {
		if (!lib.checkIsAuthenticated()) {
			return;
		}
		$http.get('/Api/SecurityContexts').then(function (response) {
			$scope.customContexts = [];
			$scope.builtinContexts = [];
			for (var i = 0; i < response.data.length; i++) {
				var c = response.data[i];

				if (c.type === 'Custom') {
					$scope.customContexts.push(c);
				} else {
					$scope.builtinContexts.push(c);
				}
			}
		}, lib.handleRestError);
		$http.get('/Api/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/SecurityContexts', {
			name: $scope.newName
		}).then(function (response) {
			$scope.customContexts.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: null
			}
		});
		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/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.upload = function () {
		$timeout(uploadCA, 200);
	};

	var clear = function () {
		angular.element("input[type='file']").val(null);
	};

	var filesToUpload = 0;
	var handleUploadError = function (arg) {
		handleFileUploadComplete();
		lib.handleRestError(arg);
	};

	var handleFileUploadComplete = function () {
		filesToUpload--;
		if (filesToUpload == 0) {
			clear();
			blockUI.stop();
		}
	}

	var uploadCA = function () {
		if ($scope.caFiles != null) {
			blockUI.start();
			var url = '/Api/IntermediateCAs';

			filesToUpload = $scope.caFiles.length;
			angular.forEach($scope.caFiles, function (file) {
				fileUpload.uploadFileToUrl(file, url).then(function (response) {
					$scope.intermediateCAs.push(response.data);
					handleFileUploadComplete();
				}, handleUploadError);
			});
		}
	};

	init();
}]);
