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

	$scope.data = null;
	$scope.transactionTypes = {};
	$scope.parameters = { userId: null };

	var init = function () {
		parseTargetUser($location.search());
	};

	var getBaseUrl = function () {
		return '/Api/Admin/TransactionLimits/';
	};

	var getTransactionLimits = function () {
		blockUI.start();
		var url = $scope.targetUser ? '/Api/Admin/Users/' + $scope.targetUser.id + '/TransactionLimits/' : getBaseUrl();
		$http.get(url).then(function (response) {
			$scope.data = response.data;
			$timeout(function () { blockUI.stop(); }, 500);
		}, lib.handleRestError);
	};

	var getTransactionTypes = function () {
		var url = '/Api/Admin/TransactionTypes/';
		blockUI.start();
		$http.get(url).then(function (response) {
			$scope.transactionTypes = response.data;
			getTransactionLimits();
			$timeout(function () { blockUI.stop(); }, 500);
		}, lib.handleRestError);
	};

	var parseTargetUser = function (params) {
		if ($scope.parameters.userId !== params.userId) {
			$scope.targetUser = null;
			$scope.parameters.userId = params.userId;
			if (params.userId) {
				blockUI.start();
				$http.get('/Api/Users/' + params.userId).then(function (response) {
					$scope.targetUser = {
						name: response.data.username,
						id: response.data.id
					};
					$timeout(function () { blockUI.stop(); }, 500);
					getTransactionTypes();
				}, lib.handleRestError);
			} else {
				getTransactionTypes();
			}
		} else {
			getTransactionTypes();
		}
	};

	$scope.showTransactionLimit = function(id, name, code, parameter) {
		var modalInstance = $modal.open({
			templateUrl: views.transactionLimitDetails,
			controller: 'transactionLimitDetailsController',
			size: 'lg',
			resolve: {
				transactionLimitId: function () {
					return id;
				},
				transactionLimitName: function () {
					return name;
				},
				transactionLimitCode: function () {
					return code;
				}
			}
		});
		modalInstance.result.then(init, init);
	};

	$scope.create = function () {
		$modal.open({
			templateUrl: views.transactionLimitCreate,
			controller: 'transactionLimitCreateController',
			size: 'lg',
			resolve: {
				targetUser: function () {
					return $scope.targetUser;
				}
			}
		}).result.then(init, init);
	};

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

	$scope.deleteConfirm = function(id) {
		var url = getBaseUrl() + id;
		$http.delete(url).then(function (response) {
			getTransactionLimits();
		}, lib.handleRestError);
	};

	$scope.getTransactionTypeName = function(code) {
		for (var index in $scope.transactionTypes) {
			if (code != null && $scope.transactionTypes[index].code === code.trim()) {
				return $scope.transactionTypes[index].name;
			}
		}
		return null;
	};

	init();
}]);
