(function () {
    global.dashModule
        .controller('setCustomersDiscountCtrl', ['$scope', '$http', '$', '$translate', 'utilsService', setCustomersDiscountController])
        .directive('setCustomersDiscount', [setCustomersDiscountDirective]);

    function setCustomersDiscountDirective() {
        return {
            restrict: 'E',
            templateUrl: 'financing/html/setCustomersDiscountTemplate'
        };
    }

    function setCustomersDiscountController($scope, $http, $, $translate, utilsService) {
        $scope.getCustomersAndDiscount = getCustomersAndDiscount;
        $scope.saveCustomersDiscount = saveCustomersDiscount;
        $scope.getNewInterestRate = getNewInterestRate;
        $scope.customers = {};
        $scope.discounts = {};
        $scope.interestRate = 0;
        $scope.showMessage = false;
        $scope.getMaxInterestRateValue = getMaxInterestRateValue;
        $scope.searchText = '';

        function getCustomersAndDiscount() {
            $http({
                method: 'POST',
                url: 'financing/api/getCustomersAndDiscount'
            }).then(setCustomersAndDiscount, utilsService.onHttpError);
        }

        function setCustomersAndDiscount(response) {
            $scope.customers = response.data.customers ? response.data.customers : {};
            $scope.interestRate = response.data.interestRate ? response.data.interestRate : 0;

            $scope.customers.forEach(detail => {
                $scope.discounts[detail.idCustomer] = parseFloat(detail.discount);
            });
        }

        function getNewInterestRate(idCustomer) {
            const newDiscount = $scope.discounts[idCustomer] || 0;
            const newInterestRate = $scope.interestRate - newDiscount;
            $scope.showMessage = newInterestRate <= 0 ||
                newInterestRate > $scope.interestRate ||
                $scope.discounts[idCustomer] === undefined ?
                true : false;

            return newInterestRate.toFixed(2);
        }

        function saveCustomersDiscount() {
            const modifiedDiscounts = getOnlyModifiedDiscounts();
            const params = $.param({
                discounts: modifiedDiscounts
            });

            $http({
                method: 'POST',
                url: 'financing/api/saveCustomersDiscount',
                data: params
            }).then(showUpdateDefaultMessage, utilsService.onHttpError);
        }

        function getOnlyModifiedDiscounts() {
            const filteredCustomers = $scope.customers.filter(info => {
                $scope.discounts[info.idCustomer] = $scope.discounts[info.idCustomer] || 0;

                return parseFloat(info.discount) !== $scope.discounts[info.idCustomer];
            });

            filteredCustomers.map(info => {
                info.discount = $scope.discounts[info.idCustomer].toFixed(2);
            });

            return filteredCustomers;
        }

        function showUpdateDefaultMessage(response) {
            if (typeof response.data.messages !== 'undefined') {
                response.data.messages.forEach((messageObj) => {
                    const key = messageObj.key || '';
                    let translatedMessage = $translate.instant('financing.messages.' + messageObj.message);
                    translatedMessage = key !== '' ? translatedMessage + ': ' + key : translatedMessage;
                    utilsService.displayMessage(messageObj.code, translatedMessage);
                });
            }
        }

        function getMaxInterestRateValue() {
            return ($scope.interestRate - 0.01).toFixed(2);
        }
    }
})();
