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

	$scope.limit = null;
	$scope.warning = null;
	$scope.tolerance = null;
	$scope.type = '';
	$scope.parameter = null;
	$scope.transactionTypes = {};
	$scope.typeCode = '';
	$scope.plans = null;
	$scope.userId = targetUser != null ? targetUser.id : null;
	$scope.userName = targetUser != null ? targetUser.name : null;
	$scope.targetUser = targetUser;

	var getTransactionTypes = function () {
		var url = '/Api/Admin/TransactionTypes/';

		$scope.loading = true;
		$http.get(url).then(function (response) {
			$scope.transactionTypes = response.data;
			$timeout(function () { $scope.loading = false; }, 500);
		}, lib.handleRestError);
	};

	var handleValidationError = function (response) {
		blockUI.stop();

		if (response.status === 422 && response.data.code === "TransactionLimitAlreadyExists") {
			$translate(['tlcConfirmOverwriteTitle', 'tlcConfirmOverwriteMessage', 'false', 'true']).then(function (msgs) {
				var buttons = {};
				buttons[msgs.true] = {
					css: 'btn-success',
					click: function (modalInstance) {
						$scope.submit(true);
						modalInstance.close();
					}
				};
				buttons[msgs.false] = {
					css: 'btn-default',
					click: function (modalInstance) {
						modalInstance.close();
					}
				};
				lib.showMessageBox(msgs.tlcConfirmOverwriteMessage, msgs.tlcConfirmOverwriteTitle, buttons);
			});
		} else {
			lib.handleRestError(response);
		}
	};

	var getTimeStampPlans = function () {
		$http.get('/Api/TimestampPlans').then(function (response) {
			$scope.plans = response.data;
		}, lib.handleRestError);
	};

	var init = function () {
		getTransactionTypes();
		getTimeStampPlans();
	};

	$scope.submit = function (overwrite) {

		if ($scope.tlCreate.limit.$invalid || $scope.tlCreate.tolerance.$invalid || $scope.tlCreate.warning.$invalid) {
			return;
		}

		if (!$scope.typeCode) {
			$translate(['message', 'tlcSelectTransactionType']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.tlcSelectTransactionType);
			});
			return;
		}

		blockUI.start();
		$http.post('/Api/Admin/TransactionLimits/', {
			TransactionTypeCode: $scope.typeCode,
			Parameter: $scope.parameter,
			Limit: $scope.limit,
			TolerancePercentage: $scope.tolerance,
			WarningPercentage: $scope.warning,
			UserId: $scope.userId,
			Overwrite: overwrite
		}).then(function (response) {
			blockUI.stop();
			$translate(['message', 'tlcCreatedSuccess']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.tlcCreatedSuccess, function (alertDialog) {
					alertDialog.close();
					$modalInstance.close();
				});
			});
		}, handleValidationError);

	};

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

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

	init();
}]);
