(function () {
    global.dashModule
        .controller('chooseInstallationCtrl', ['$scope', '$', '$http', '$translate', 'utilsService', chooseInstallationCtrl])
        .directive('chooseInstallation', [chooseInstallationDirective]);

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

    function chooseInstallationCtrl($scope, $, $http, $translate, utilsService) {
        const step = $scope.$parent.step;
        const idOrder = step.idOrder || 0;
        const idPackage = step.idPackage || 0;
        $scope.saveInstallationForPackage = saveInstallationForPackage;
        $scope.getInstallCompaniesForPackage = getInstallCompaniesForPackage;
        $scope.installCompany = {};
        $scope.multipleInstallCompanies = false;

        function getInstallCompaniesForPackage(step) {
            const params = $.param({
                idOrder,
                idPackage
            });

            $http({
                method: 'POST',
                data: params,
                url: 'orders/api/getInstallCompaniesForPackage'
            }).then((response) => {
                setInstallationCompanies(response, step);
            }, utilsService.onHttpError);
        }

        function setInstallationCompanies(response, step) {
            const availableInstallationCompanies = response.data.available;
            const selectedInstallationCompany = response.data.selected;
            if (response.data && availableInstallationCompanies.length) {
                if (availableInstallationCompanies.length === 1) {
                    $scope.multipleInstallCompanies = false;
                    step.installationCompany = availableInstallationCompanies[0];
                } else {
                    $scope.multipleInstallCompanies = true;
                    if (selectedInstallationCompany.length > 0) {
                        step.installationCompany = selectedInstallationCompany[0];
                    }
                    step.installCompanies = availableInstallationCompanies;
                }
            }
        }

        function saveInstallationForPackage(step) {
            const params = $.param({
                idOrder,
                idPackage,
                idInstallation: step.installationCompany.id
            });

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

        function showConfirmationMessage(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);
                });
            }
        }

    }
})();
