(function () {
    global.dashModule
        .directive('editPackageTemplates', editPackageTemplatesDirective)
        .controller('editPackageTemplatesCtrl', ['$scope', 'utilsService', 'packagesUtilsService', editPackageTemplatesCtrl]);

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

    function editPackageTemplatesCtrl($scope, utilsService, packagesUtilsService) {
        $scope.isPackageSelected = isPackageSelected;
        $scope.showSelectPackages = getPackageTemplates;
        $scope.packageList = [];
        $scope.checkQuantityAmount = utilsService.verifyAmountAdded;
        $scope.setPackageTemplateSelected = setPackageTemplateSelected;

        function getPackageTemplates() {
            utilsService.registerFunction('setPackagesTemplates', setPackagesTemplates);
            packagesUtilsService.getPackageTemplates().then(setPackagesTemplates, global.onHttpError);
        }

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

        function setPackagesTemplates(response) {
            $scope.packageList = response.data || [];
        }

        function setPackageTemplateSelected() {
            packagesUtilsService.setPackageTemplateSelected($scope.packageSelected);
        }
    }
})();
