import {
    highlightCharacter,
    createImageElements,
    countLetters,
    scrollToTop,
    isHasSymbol
} from './utils/helpers';

var Wick = window.Wick || {};
Wick.EnergyEfficiency = {
    el: {
        content: 'body',
        btnEnergyLabels: '.btn-energy-label',
        energyLabel: '.energy-label',
        energyLabelText: '.energy-label-text',
        dataRating: 'data-rating',
        dataType: 'data-type',
        modal: '#modal-energy-efficiency',
        dataModalImages: 'data-images',
        modalImagesWrap: '.modal-images',
    },
    type: {
        Old: {
            'A+++': 'bg-green-haze',
            'A++': 'bg-green-apple',
            'A+': 'bg-rio-grande',
            'A': 'bg-yellow-turbo',
            'B': 'bg-yellow-selective',
            'C': 'bg-christine',
            'D': 'bg-monza',
        },
        New: {
            'A': 'bg-green-haze',
            'B': 'bg-green-apple',
            'C': 'bg-rio-grande',
            'D': 'bg-yellow-turbo',
            'E': 'bg-yellow-selective',
            'F': 'bg-christine',
            'G': 'bg-monza'
        }
    },
    drawEnergyLabels: function () {
        const { energyLabel, energyLabelText, dataRating, dataType, btnEnergyLabels } = Wick.EnergyEfficiency.el;

        $(btnEnergyLabels).each(function (_, label) {
            const iconEnergyLabel = $(label).children(energyLabel).first();
            const energyLabelTextNode = $(iconEnergyLabel).find(energyLabelText);
            const rating = $(iconEnergyLabel).attr(dataRating);
            const type = $(label).attr(dataType);
            const isTypeMultiple = isHasSymbol(rating, '/');
            const arrayImages = Wick.EnergyEfficiency.parsedAttrImageLinks(label);

            if (!arrayImages.length) {
                $(label).addClass('btn-energy-label--c-auto');
            }

            if (isTypeMultiple) {
                Wick.EnergyEfficiency.applyStylesForMultipleLabel(label, iconEnergyLabel, energyLabelTextNode, rating);
            } else {
                switch (type) {
                    case 'New':
                        $(label).addClass('btn-energy-label__new');
                        $(iconEnergyLabel).addClass('energy-label__new');
                        break;
                    case 'Old':
                        $(iconEnergyLabel).addClass('energy-label__old');
                        break;
                }
            }

            energyLabelTextNode.html(highlightCharacter(rating, '+', 'plus'));
            Wick.EnergyEfficiency.addClassByVersion(iconEnergyLabel, rating, type, isTypeMultiple);
        });
    },
    applyStylesForMultipleLabel: function (label, iconEnergyLabel, energyLabelTextNode, rating) {
        $(label).addClass('btn-energy-label__multiple');
        $(iconEnergyLabel).addClass('energy-label__multiple');
        Wick.EnergyEfficiency.reduceSizeMultiEnergyTitle(energyLabelTextNode, rating);
    },
    addClassByVersion: function (iconEnergyLabels, ratingEnergy, typeEnergy, isTypeMultiple) {
        if (isTypeMultiple) {
            ratingEnergy = Wick.EnergyEfficiency.getSingleRating(ratingEnergy);
        }

        const type = Wick.EnergyEfficiency.type[typeEnergy] || {};
        const ratingClass = type[ratingEnergy];

        if (ratingClass) {
            return $(iconEnergyLabels).addClass(ratingClass);
        }

        console.error(`No class exists for the rating ${ratingEnergy} in type ${type}`);

    },
    getSingleRating: function (rating) {
        return rating.split('/')[0];
    },
    reduceSizeMultiEnergyTitle: function (element, str) {
        const letterCount = countLetters(str);
        const deltaCount = Wick.EnergyEfficiency.calculateDeltaCount(str);
        const scaleValue = Wick.EnergyEfficiency.calculationScaleValue(letterCount, deltaCount);

        return $(element).css('transform', `scale(${scaleValue})`);
    },
    calculateDeltaCount: function (str) {
        return str.length - countLetters(str);
    },
    calculationScaleValue: function (letterCount, deltaCount) {
        return letterCount >= 3 ? 1 - (letterCount * 0.1) : 1 - (deltaCount * 0.05)
    },
    parsedAttrImageLinks: function (element) {
        let images = $(element).children(Wick.EnergyEfficiency.el.energyLabel).attr(Wick.EnergyEfficiency.el.dataModalImages);

        if (!images) {
            return [];
        }

        return images.split(',');
    },
    showModal: function (wrapper, modal, imageElements) {
        wrapper.append(imageElements);
        modal.modal('show');
        scrollToTop(modal.find('.modal-body'));
    },
    clearChildElements: function (modal, modalImages) {
        let wrapper = $(modal).find(modalImages);
        wrapper.empty();
    },
    triggerModal: function () {
        const arrayImages = Wick.EnergyEfficiency.parsedAttrImageLinks(this);

        if(!arrayImages.length) {
            return;
        }

        const $modal = $(Wick.EnergyEfficiency.el.modal);
        const imageElements = createImageElements(arrayImages, 'Label Energy Efficiency');
        const $wrapper = $modal.find(Wick.EnergyEfficiency.el.modalImagesWrap);

        Wick.EnergyEfficiency.showModal($wrapper, $modal, imageElements);
    },
    init: function () {
        Wick.EnergyEfficiency.drawEnergyLabels();
        $(Wick.EnergyEfficiency.el.content).on('click', Wick.EnergyEfficiency.el.btnEnergyLabels, Wick.EnergyEfficiency.triggerModal);

        $(Wick.EnergyEfficiency.el.modal).on('hidden.bs.modal', function() {
            Wick.EnergyEfficiency.clearChildElements(Wick.EnergyEfficiency.el.modal, Wick.EnergyEfficiency.el.modalImagesWrap);
        });
    }
}
$(document).ready(function () {
    Wick.EnergyEfficiency.init();
});
