(function () {
    global.dashModule
        .directive('editPackages', editPackagesDirective)
        .controller('editPackagesCtrl', ['$scope', '$http', '$', '$translate', '$timeout', 'utilsService', 'packagesUtilsService', editPackagesCtrl]);

    function editPackagesDirective() {
        return {
            restrict: 'E',
            templateUrl: 'packages/html/editPackagesTemplate'
        };
    }

    function editPackagesCtrl($scope, $http, $, $translate, $timeout, utilsService, packagesUtilsService) {
        $scope.getCountries = getCountries;
        $scope.isCountrySelected = isCountrySelected;
        $scope.isPackageSelected = isPackageSelected;
        $scope.showSelectPackages = getPackages;
        $scope.packageList = [];
        $scope.productUnit = [];
        $scope.getEditPackagesTitle = getEditPackagesTitle;
        $scope.checkQuantityAmount = utilsService.verifyAmountAdded;
        $scope.setCountryAndPackageSelected = setCountryAndPackageSelected;

        function getCountries() {
            const params = $.param({
                getArray: true
            });
            $http({
                method: 'POST',
                data: params,
                url: 'countries/api/getAllCountries'
            }).then(setCountries, utilsService.onHttpError);
        }

        function setCountries(response) {
            $scope.countries = response.data;
        }

        function isCountrySelected(selectedCountryId) {
            return typeof selectedCountryId !== 'undefined' && selectedCountryId !== 0;
        }

        function isPackageSelected() {
            return typeof $scope.packageSelected !== 'undefined' && $scope.packageSelected;
        }

        function getPackages() {
            utilsService.registerFunction('setPackages', setPackages);
            packagesUtilsService.getPackagesPerCountry($scope.selectedCountryId).then(setPackages, global.onHttpError);
        }

        function setPackages(response) {
            $scope.packageList = [];

            if (response.data) {
                $timeout(() => {
                    $scope.packageList = response.data && response.data.data ? response.data.data : response.data;
                }, 0);
            }
        }

        function getEditPackagesTitle() {
            const translationData = {
                packageName: $scope.packageSelected ? $scope.packageSelected.name : '',
                packageReference: $scope.packageSelected ? $scope.packageSelected.reference : '',
                country: $scope.selectedCountryId && $scope.countries ? searchCountryNameSelected() : ''
            };

            const editPackageTitle = getEditPackageTitle();
            const packageName = $scope.packageSelected ? $scope.packageSelected.name : '';
            const translatedMessage = editPackageTitle ? $translate.instant('packages.forms.' + editPackageTitle, translationData) : $scope.packageName;

            return packageName ? packageName + translatedMessage : translatedMessage;
        }

        function getEditPackageTitle() {
            if (typeof $scope.countrySelected === 'undefined' && typeof $scope.packageSelected === 'undefined') {
                return 'SELECT_COUNTRY_PACKAGE';
            }
            return 'SELECTED_COUNTRY';
        }

        function setCountryAndPackageSelected() {
            if($scope.packageSelected && $scope.selectedCountryId){
                packagesUtilsService.setCountryAndPackageSelected(searchCountryNameSelected(), $scope.selectedCountryId, $scope.packageSelected);
            }
        }

        function searchCountryNameSelected() {
            const selectedCountry = $scope.countries.find(countryInfo => {
                return countryInfo.id === $scope.selectedCountryId;
            });

            return selectedCountry ? selectedCountry.name : '';
        }
    }
})();
