(function () {
    global.dashModule
        .controller('customerAcceptanceCtrl', ['$scope', '$', '$http', '$translate', 'Upload', 'utilsService', customerAcceptanceCtrl])
        .directive('customerAcceptance', [customerAcceptanceDirective]);

    function customerAcceptanceDirective() {
        return {
            restrict: 'E',
            templateUrl: 'orders/html/customerAcceptanceTemplate'
        };
    }

    function customerAcceptanceCtrl($scope, $, $http, $translate, Upload, utilsService) {
        const step = $scope.$parent.step;
        const idOrder = step.idOrder;
        const idProcess = step.idProcess;
        const stepInfo = {
            idOrder,
            idProcess,
            isNextButtonDisabled: true
        };
        $scope.getCustmerAcceptance = getCustmerAcceptance;
        $scope.getDueDateClass = getDueDateClass;
        $scope.uploadFile = uploadFile;
        $scope.acceptDeclineInstallation = acceptDeclineInstallation;
        $scope.getStatusIcon = getStatusIcon;
        $scope.acceptance = {};
        $scope.isInstallationNotAccepted = {};
        $scope.showHideDialog = showHideDialog;
        $scope.showDeclineInstallation = showDeclineInstallation;
        $scope.isAcceptInstallationDisabled = false;
        $scope.isDeclineInstallationDisabled = false;
        $scope.isInstallationDeclined = {};
        $scope.isDialogVisible = {};
        $scope.getAcceptanceClass = getAcceptanceClass;
        $scope.showCustomerAcceptance = showCustomerAcceptance;
        $scope.getCustomerAcceptanceDescription = getCustomerAcceptanceDescription;
        $scope.removeAcceptanceDocument = removeAcceptanceDocument;

        function getCustmerAcceptance() {
            const params = $.param({
                idOrder
            });

            $http({
                method: 'POST',
                data: params,
                url: 'orders/api/getCustomerAcceptance'
            }).then(setCustomerAcceptance, utilsService.onHttpError);
        }

        function setCustomerAcceptance(response) {
            if (response.data && Object.keys(response.data).length) {
                $scope.acceptance = response.data[idOrder];
                stepInfo.isNextButtonDisabled = parseInt($scope.acceptance.customerAccepted) === 0;
                $scope.isAcceptInstallationDisabled = parseInt($scope.acceptance.customerAccepted) === 1;
                $scope.isDeclineInstallationDisabled = parseInt($scope.acceptance.customerAccepted) === -1;
            }
            utilsService.executeRegisteredFunction('isNextBtnDisabled', stepInfo);
        }

        function getDueDateClass() {
            if ($scope.acceptance.daysDiff <= 0) {
                return 'alert-danger';
            }

            if ($scope.acceptance.daysDiff <= 3) {
                return 'alert-warning';
            }

            return 'alert-info';
        }

        function displayMessage(response) {
            if (typeof response.data.messages !== 'undefined') {
                response.data.messages.forEach((messageObj) => {
                    const translatedMessage = $translate.instant('orders.messages.' + messageObj.message);
                    utilsService.displayMessage(messageObj.code, translatedMessage);

                    if (messageObj.code === 'success') {
                        getCustmerAcceptance();
                    }

                    if (messageObj.message === 'INSTALLATION_DECLINED') {
                        showDeclineInstallation();
                        $scope.installationDeclinedReason = '';
                        $scope.isDeclineInstallationDisabled = true;
                    }
                });
            }
        }

        function uploadFile(file) {
            Upload.upload({
                url: 'orders/api/uploadAcceptanceDocument',
                method: 'POST',
                file: file,
                data: {
                    idPackage,
                    idOrder
                }
            }).then(displayMessage, utilsService.onHttpError);
        }

        function acceptDeclineInstallation(actionType) {
            const params = $.param({
                idOrder,
                idPackage,
                actionType,
                declineReason: $scope.installationDeclinedReason
            });

            if ((actionType === 'accept' && !$scope.isAcceptInstallationDisabled) ||
                (actionType === 'decline' && !$scope.isDeclineInstallationDisabled)) {
                $http({
                    method: 'POST',
                    data: params,
                    url: 'orders/api/acceptDeclineInstallation'
                }).then(displayMessage, utilsService.onHttpError);
            }
        }

        function getStatusIcon(status) {
            let icon = 'time';
            if (parseInt(status) === -1) {
                icon = 'remove';
            } else if (parseInt(status) === 1) {
                icon = 'ok';
            }

            return icon;
        }

        function showHideDialog(actionType) {
            $scope.$evalAsync(() => {
                $scope.isDialogVisible[actionType] = !$scope.isDialogVisible[actionType];
            });
        }

        function showDeclineInstallation() {
            if (!$scope.isDeclineInstallationDisabled) {
                $scope.$evalAsync(() => {
                    $scope.isInstallationDeclined[idPackage] = !$scope.isInstallationDeclined[idPackage];
                });
            }
        }

        function getAcceptanceClass(acceptanceStatus) {
            let cssClass = 'warning';
            if (parseInt(acceptanceStatus) === -1) {
                cssClass = 'danger';
            } else if (parseInt(acceptanceStatus) === 1) {
                cssClass = 'success';
            }

            return cssClass;
        }

        function showCustomerAcceptance(acceptance) {
            return acceptance !== 0;
        }

        function getCustomerAcceptanceDescription(customerAcceptance) {
            let acceptanceStatus = 'WAITING';
            if (parseInt(customerAcceptance) === -1) {
                acceptanceStatus = 'DECLINED';
            } else if (parseInt(customerAcceptance) === 1) {
                acceptanceStatus = 'ACCEPTED';
            }

            return $translate.instant('orders.messages.CUSTOMER_INSTALLATION_' + acceptanceStatus);
        }

        function removeAcceptanceDocument(idDocument) {
            const params = $.param({
                idOrder,
                idPackage,
                idDocument
            });

            $http({
                method: 'POST',
                data: params,
                url: 'orders/api/removeOrderDocument'
            }).then(displayMessage, utilsService.onHttpError);
        }
    }
})();
