$(document).ready(function () {
    let currentRowCount = 3;

    if (window.innerWidth < 992) {
        currentRowCount = 2;
    }

    function cardsContent(itemsInRow) {
        let card = $('.content .product-card, .content .card-product-banner, .content .card-sponsor-banner, .content .card-sponsor-product').filter(function() {
            return $(this).parents('.product-card, .card-product-banner, .card-sponsor-banner, .card-sponsor-product').length === 0;
        });
        let priceSelectors = [
            '.including-vat .product-card__price__old-price',
            '.excluding-vat .product-card__price__old-price',
            '.including-vat .product-card__price-sqm',
            '.excluding-vat .product-card__price-sqm'
        ];

        revealAll(card, ['.product-card__price__old-price',
                                    '.product-card__price-sqm',
                                    '.product-card__price-value',
                                    '.product-card__sponsored',
                                    '.energy-efficiency',
                                    '.energy-efficiency__link']);

        let rows = Math.ceil(card.length / itemsInRow);

        for ( let i = 0 ; i < rows ; i++ ) {
            let currentRow = card.slice(i * itemsInRow, i * itemsInRow + itemsInRow);
            let priceElements = $(currentRow[0]).find('.product-card__price');

            if (priceElements.length === 2) {
                priceSelectors.forEach(selector => hideEmptiness(currentRow, selector));
            } else {
                hideEmptiness(currentRow, '.product-card__price__old-price');
                hideEmptiness(currentRow, '.product-card__price-sqm');
            }
            hideEmptiness(currentRow, '.product-card__price-value');
            hideEmptiness(currentRow, '.product-card__sponsored');
            hideEmptiness(currentRow, '.product-card__price__old-price');
            hideEmptiness(currentRow, '.energy-efficiency');
            hideEmptiness(currentRow, '.energy-efficiency__link');

            fixHeaders(currentRow, '.product-card__title-v2');
        }
    }

    function hideEmptiness(query, className) {
        let currentQuery = query.find(className);
        let rowContent = currentQuery.text();
        const classEmptyCheckMap = {
            '.energy-efficiency': 'energy-efficiency--empty',
            '.product-card__sponsored': 'product-card__sponsored--empty',
            '.product-card__price__old-price': 'product-card__price_empty'
        };

        if (classEmptyCheckMap[className]) {
            let isClassEmpty = isClassPresentInQuery(currentQuery, classEmptyCheckMap[className]);
            if (isClassEmpty) {
                currentQuery.hide();
            }
            return;
        }

        if (rowContent.length === query.length) {
            currentQuery.hide();
        }
    }

    function isClassPresentInQuery(currentQuery, className) {
        return currentQuery.toArray().every((element) => $(element).hasClass(className));
    }

    function revealAll(query, classNames) {
        for ( let i = 0 ; i < classNames.length ; i++) {
            query.find(classNames[i]).show();
        }
    }

    function fixHeaders(query, className) {
        let currentQuery = query.find(className);
        currentQuery.parent().removeClass('product-card__header-r1 product-card__header-r2 product-card__header-r3');
        let rowHeight = [];
        for (let i = 0 ; i < currentQuery.length; i++ ) {
            rowHeight[i] = currentQuery.eq(i).height() / parseInt(currentQuery.eq(i).css('line-height'));
        }
        let rows = Math.max.apply(null, rowHeight);
        currentQuery.parent().addClass('product-card__header-r' + Math.round(rows));
    }

    cardsContent(currentRowCount);

    $(window).on('resize', _.debounce(function () {
        //  "render" again if our screen resolution changed
        if (window.innerWidth < 992) {
            currentRowCount = 2;
        } else {
            currentRowCount = 3;
        }
        cardsContent(currentRowCount);
    }, 200));

    $(window).on('productBannerPositionChanged', function () {
        cardsContent(currentRowCount);
    });
});
