'use strict';
app.controller('signaturePolicyDetailsController', ['$scope', '$http', '$modalInstance', '$timeout', '$modal', '$translate', '$q', 'blockUI', 'lib', 'modelId', 'isEditorEnabled', 'notifyService', function ($scope, $http, $modalInstance, $timeout, $modal, $translate, $q, blockUI, lib, modelId, isEditorEnabled, notifyService) {

	$scope.model = null;
	$scope.underlyingPolicies = null;
	$scope.securityContexts = null;
	$scope.timestampPlans = null;
	$scope.policyTypes = ['Cades', 'Pades', 'Xml'];

	$scope.editor = { isEnabled: false, isActive: false, key: null, model: null };

	var init = function () {
		blockUI.start();
		$http.get('/Api/SignaturePolicies/' + modelId).then(function (response) {
			$scope.model = response.data;
			$scope.editor.isEnabled = isEditorEnabled && !$scope.model.isReadOnly;
			$scope.isEditable = isEditorEnabled && !$scope.model.isReadOnly;
			blockUI.stop();
		}, function (response) {
			lib.showRestError(response, $scope.close);
		});
	};

	$scope.edit = function (key) {
		return $q.when({ key: key, value: $scope.model[key] });
	};

	$scope.save = function (key, value) {
		var data = {};
		data[key] = value;
		blockUI.start();
		return $http.put('/Api/Admin/SignaturePolicies/' + modelId, data).then(function (response) {
			$scope.model = response.data;
			blockUI.stop();
		}, lib.handleRestError);
	};

	$scope.editor.start = function (key, initialModel) {
		if (!this.isActive) {
			if (typeof initialModel === 'undefined') {
				this.model = angular.copy($scope.model[key]);
			} else {
				this.model = angular.copy(initialModel);
			}
			this.key = key;
			this.isActive = true;
		}
	};

	$scope.editor.save = function () {
		if (this.isActive) {
			blockUI.start();
			var data = {};
			data[this.key] = this.model;
			$http.put('/Api/Admin/SignaturePolicies/' + modelId, data).then(function (response) {
				$scope.model = response.data;
				$scope.editor.cancel();
				blockUI.stop();
			}, lib.handleRestError);
		}
	};

	$scope.editor.cancel = function () {
		this.isActive = false;
	};

	$scope.editUnderlyingPolicy = function () {
		var promise = $q.when({ key: 'underlyingPolicyId', value: $scope.model.underlyingPolicy.id });
		if ($scope.underlyingPolicies == null) {
			blockUI.start();
			return $http.get('/Api/SignaturePolicies/ListByType/' + $scope.model.type).then(function (response) {
				$scope.underlyingPolicies = response.data;
				blockUI.stop();
				return promise;
			}, lib.handleRestError);
		}
		return promise;
	};

	$scope.editSecurityContext = function () {
		var promise = $q.when({ key: 'securityContextId', value: ($scope.model.securityContext || { id: '' }).id });
		if ($scope.securityContexts == null) {
			blockUI.start();
			return $http.get('/Api/SecurityContexts').then(function (response) {
				$scope.securityContexts = [{ id: '', name: $translate.instant('default') }];
				for (var i = 0; i < response.data.length; i++) {
					var c = response.data[i];
					if (c.type !== 'Custom') {
						$scope.securityContexts.push(c);
					}
				}
				blockUI.stop();
				return promise;
			}, lib.handleRestError);
		}
		return promise;
	};

	$scope.editTimestampPlan = function () {
		var promise = $q.when({ key: 'timestampPlanId', value: ($scope.model.timestampPlan || { id: '' }).id });
		if ($scope.timestampPlans == null) {
			blockUI.start();
			return $http.get('/Api/TimestampPlans').then(function (response) {
				$scope.timestampPlans = response.data;
				$scope.timestampPlans.unshift({ id: '', name: $translate.instant('default') });
				blockUI.stop();
				return promise;
			}, lib.handleRestError);
		}
		return promise;
	};

	$scope.testValidation = function () {
		$translate('spdConfirmTest').then(function (confirmMsg) {
			$translate(['areYouSure', 'true', 'cancel']).then(function (msgs) {
				var buttons = {};
				buttons[msgs['true']] = {
					css: 'btn-danger',
					click: testConfirmed
				};
				buttons[msgs.cancel] = {
					css: 'btn-default',
					click: function (modalInstance) {
						modalInstance.dismiss();
					}
				};
				lib.showMessageBox(confirmMsg, msgs.areYouSure, buttons);
			});
		});
	};

	var testConfirmed = function (confirmModal) {
		blockUI.start();
		var url = '/Api/Admin/SignaturePolicies/' + modelId + '/Test';
		$http.post(url).then(function (response) {
			blockUI.stop();
			notifyService.success($translate.instant('spdTestSuccessful'));
		}, lib.handleRestError).finally(confirmModal.dismiss);
	};

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

	var removeConfirmed = function (confirmModal) {
		blockUI.start();
		var url = '/Api/Admin/SignaturePolicies/' + modelId;
		$http.delete(url).then(function (response) {
			blockUI.stop();
			confirmModal.close();
			$modalInstance.close();
		}, lib.handleRestError);
	};

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

	init();
}]);
