(function () {
    global.dashModule
        .controller('packagesDetailsCtrl', ['$scope', '$sce', packagesDetailsCtrl])
        .directive('packagesDetails', [packagesDetailsDirective]);

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

    function packagesDetailsCtrl($scope, $sce) {
        $scope.renderHtml = renderHtml;
        $scope.hasExtraPackages = hasExtraPackages;
        $scope.areAllPayTypesAvailable = areAllPayTypesAvailable;
        $scope.showHideInfoBox = showHideInfoBox;
        $scope.getProductsArray = getProductsArray;
        $scope.productsArray = [];

        function renderHtml(htmlCode) {
            return $sce.trustAsHtml(htmlCode);
        }

        function hasExtraPackages(packageObject) {
            return packageObject.packageType === 'standard';
        }

        function areAllPayTypesAvailable(packagePrices, optionPrices){
            return packagePrices.length === optionPrices.length && packagePrices.every((v,i)=> v.idPaymentType === optionPrices[i]);
        }

        function showHideInfoBox(optionPackage) {
            optionPackage.isInfoBoxVisible = !optionPackage.isInfoBoxVisible;
        }

        function getProductsArray(packageInfo) {
            if('products' in packageInfo && packageInfo.products) {
                $scope.productsArray = packageInfo.products.split(',');
            }
        }
    }
})();
