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

	var transactionTypes = {
		pades: 'PADES_S',
		cades: 'CADES_S',
		xades: 'XML_S',
		auth: 'AUTH',
		timestamp: 'TST',
		openSig: 'OPEN_S',
		pdfMark: 'PDF_MARK'
	};

	$scope.isOnPremises = lib.isOnPremises();
	$scope.username = null;
	$scope.padesSignatures = '';
	$scope.cadesSignatures = '';
	$scope.authentications = '';
	$scope.transactions = '';
	$scope.errors = '';

	var init = function () {
		if (!lib.checkIsAuthenticated()) {
			return;
		}

		$scope.username = authService.getUsername();

		getTransactionCount();

		getLastEvents();
	};

	// requests

	var getLastEvents = function () {
		$http.get('/Api/Events?count=10&sortField=DateUtc&sortDirection=0').then(function (response) {
			$scope.events = response.data.entities;
		}, lib.handleRestError);
	};

	var getTransactionCount = function () {
		$http.get('/Api/Transactions/Synopsis?success=true').then(function (response) {
			var signatures = 0;
			var authentications = 0
			var others = 0;
			var transactions = 0;

			for (var i = 0, len = response.data.length; i < len; i++) {
				var transaction = response.data[i];
				if (transaction.transactionType == transactionTypes.pades
					|| transaction.transactionType == transactionTypes.cades
					|| transaction.transactionType == transactionTypes.xades) {
					signatures += transaction.count;
				} else if (transaction.transactionType == transactionTypes.auth) {
					authentications += transaction.count;
				} else {
					others += transaction.count;
				}

				transactions += transaction.count;
			}

			$scope.signatures = signatures;
			$scope.authentications = authentications;
			$scope.others = others;
			$scope.transactions = transactions;
		}, lib.handleRestError);
	};

	// click

	$scope.generateToken = function () {
		$http.get('/Api/Account/LongTermToken').then(function (response) {
			$modal.open({
				templateUrl: views.tokenDialog,
				controller: 'tokenDialogController',
				resolve: {
					token: function () { return response.data.accessToken; }
				}
			});
		}, lib.handleIdentityError);
	};

	$scope.revokeTokens = function () {
		$translate(['dashboardRevokeTokensConfirm', 'dashboardRevokeTokensButtonText', 'cancel', 'areYouSure']).then(function (msgs) {
			var buttons = {};
			buttons[msgs.dashboardRevokeTokensButtonText] = {
				css: 'btn-danger',
				click: function (modalInstance) {
					blockUI.start();
					$http.delete('/Api/Account/Tokens').then(function (response) {
						blockUI.stop();
						modalInstance.close();
						authService.logout();
					}, lib.handleIdentityError);
				}
			};
			buttons[msgs.cancel] = {
				css: 'btn-default',
				click: function (modalInstance) {
					modalInstance.close();
				}
			};
			lib.showMessageBox(msgs.dashboardRevokeTokensConfirm, msgs.areYouSure, buttons);
		});
	};

	$scope.showEventDetails = function (e) {
		blockUI.start();
		$http.get('/Api/Events/' + e.id).then(function (response) {
			blockUI.stop();
			lib.showAlert($translate.instant('evDetails'), response.data.details.replace(/\n/g, '<br/>'));
		}, function (response) {
			lib.showRestError(response, function () {
				$modalInstance.close();
			});
		});
	};

	init();

}]);
