(function () {
    global.dashModule
        .controller('myPackagesDetailsCtrl', ['$scope', '$sce', '$translate', myPackagesDetailsCtrl])
        .directive('myPackagesDetails', [myPackagesDetails]);

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

    function myPackagesDetailsCtrl($scope, $sce, $translate) {
        $scope.isVisibleToCustomer = isVisibleToCustomer;
        $scope.sumPrices = sumPrices;
        $scope.renderHtml = renderHtml;
        $scope.hasExtraPackages = hasExtraPackages;
        $scope.areAllPayTypesAvailable = areAllPayTypesAvailable;
        $scope.showHideInfoBox = showHideInfoBox;
        $scope.getAlertClass = getAlertClass;
        $scope.getAlertText = getAlertText;
        $scope.getAlertIcon = getAlertIcon;
        $scope.getProductsArray = getProductsArray;
        $scope.productsArray = [];

        function getAlertClass(option) {
            return !option.isAvailable ? 'alert-danger' : 'alert-warning';
        }

        function getAlertText(option) {
            return !option.isAvailable ? $translate.instant('packages.messages.NOT_ALL_PAY_AVAILABLE') : $translate.instant('packages.messages.NOT_SET_PAYMENT');
        }

        function getAlertIcon(option) {
            return !option.isAvailable ? 'glyphicon-ban-circle' : 'glyphicon-warning-sign';
        }

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

        function areAllPayTypesAvailable(packagePrices, optionPackage) {
            optionPackage.isAvailable = true;
            optionPackage.isSetByMe = true;
            packagePrices.forEach((value, key) => {
                optionPackage.isAvailable = optionPackage.isAvailable &&
                    typeof optionPackage.prices[key] !== 'undefined' &&
                    value.idPaymentType === optionPackage.prices[key];

                optionPackage.isSetByMe = optionPackage.isSetByMe &&
                    typeof optionPackage.commercialLeadPrices[key] !== 'undefined' &&
                    value.idPaymentType === optionPackage.commercialLeadPrices[key];
            });
            return packagePrices.length === optionPackage.prices.length && optionPackage.isAvailable && optionPackage.isSetByMe;
        }

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

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

        function isVisibleToCustomer(visibleToCustomer) {
            const visibleClasses = {
                0: 'glyphicon-eye-close',
                1: 'glyphicon-eye-open',
                2: 'no-icon'
            };

            return visibleClasses[visibleToCustomer];
        }

        function sumPrices(values) {
            let total = 0;
            values.forEach((val) => {
                total += parseFloat(val);
            });

            return total;
        }

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