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

	$scope.model = null;
	$scope.certificates = null;
	$scope.authMethods = ['None', 'MutualSSL', 'Basic', 'OAuthBearerToken'];

	var init = function () {
		blockUI.start();
		$http.get('/Api/TimestampRequesters/' + modelId).then(function (response) {
			$scope.model = response.data;
			blockUI.stop();
		}, lib.handleRestError);
	};

	$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/TimestampRequesters/' + modelId, data).then(function (response) {
			$scope.model = response.data;
			blockUI.stop();
		}, lib.handleRestError);
	};

	$scope.editCredentials = function () {
		return $q.when({ userOk: false, username: '', password: '' });
	};

	$scope.saveCredentials = function (data) {
		return $q(function (resolve, reject) {
			if (!data.userOk) {
				data.userOk = true;
				reject();
			} else {
				blockUI.start();
				$http.put('/Api/TimestampRequesters/' + modelId + '/BasicCredentials', {
					username: data.username,
					password: data.password
				}).then(function (response) {
					$scope.model.basicUsername = data.username;
					blockUI.stop();
				}, lib.handleRestError).finally(resolve);
			}
		});
	};

	$scope.editCertificate = function () {
		var promise = $q.when({ value: $scope.model.sslCertificate });
		if ($scope.certificates == null) {
			blockUI.start();
			return $http.get('/Api/Admin/SystemCertificates').then(function (response) {
				$scope.certificates = response.data;
				blockUI.stop();
				return promise;
			}, lib.handleRestError);
		}
		return promise;
	};

	$scope.saveCertificate = function (value) {
		blockUI.start();
		return $http.put('/Api/TimestampRequesters/' + modelId + '/SslCertificate',
			value
		).then(function (response) {
			$scope.model.sslCertificate = value;
			blockUI.stop();
		}, lib.handleRestError);
	};

	$scope.displaySslCertificate = function (cert) {
		if (cert) {
			return cert.subjectDisplayName + ' (' + displayThumbprint(cert.thumbprint) + ')';
		}
		return null;
	};

	var displayThumbprint = function (tp) {
		return tp.slice(0, 2) + ' ' + tp.slice(2, 4) + '...' + tp.slice(-4, -2) + ' ' + tp.slice(-2);
	};

	$scope.testRequest = function () {
		blockUI.start();
		$http.post('/Api/TimestampRequesters/' + modelId + '/Test').then(function (response) {
			$translate('tsrdTestSuccessful').then(function (msg) {
				blockUI.stop();
				notifyService.success(msg);
			});
		}, lib.handleRestError);
	};

	$scope.remove = function () {
		$translate('tsrdConfirmRemove', { 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/TimestampRequesters/' + modelId;
		$http.delete(url).then(function (response) {
			blockUI.stop();
			$modalInstance.close();
		}, lib.handleRestError).finally(function () {
			confirmModal.close();
		});
	};

	$scope.showTsaCertificate = function () {
		blockUI.start();
		$http.get('/Api/TimestampRequesters/' + modelId + '/TsaCertificate').then(function (response) {
			if (!response.data) {
				$translate('tsrdNoTsaCertificate').then(function (msg) {
					blockUI.stop();
					notifyService.info(msg);
				});
			} else {
				lib.showCertificate(response.data);
			}
			blockUI.stop();
		}, lib.handleRestError);
	};

	$scope.saveOAuth = function (value) {
		blockUI.start();
		return $http.put('/Api/TimestampRequesters/' + modelId + '/OAuthBearerToken', {
			token: value
		}).then(function (response) {
			blockUI.stop();
			notifyService.success($translate.instant('savedSuccessfully'));
		}, lib.handleRestError);
	};

	$scope.editOAuth = function () {
		return $q.when({ value: '' });
	};

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

	init();
}]);
