var Wick = window.Wick || {};

Wick.VatToggle = {
    VAT_COOKIE: 'isVatChecked',
    TOGGLE_VAT_EVENT_NAME: 'toggleVat',
    HIDDEN_CLASS_NAME: 'd-none',
    el: {
        switchVatLabel: '.switch-vat-label',
        includingVat: '.including-vat',
        excludingVat: '.excluding-vat',
        includingVatGlobalClass: 'including-vat-global',
        excludingVatGlobalClass: 'excluding-vat-global',
        switchVatInput: '.switch-vat input',
        isHidden: 'd-none',
        switchLabelCheckedClass: 'switch-label__checked',
    },

    toggle: function () {
        const newSwitcherValue = !Wick.VatToggle.isOn();

        Wick.VatToggle.setLabelState();
        Wick.VatToggle.setSwitcherState(newSwitcherValue);

        Wick.VatToggle.setCookie(Wick.VatToggle.VAT_COOKIE, newSwitcherValue);

        const event = Wick.VatToggle.createToggleEvent(newSwitcherValue);
        window.dispatchEvent(event);
    },

    setLabelState: function () {
        Wick.VatToggle.toggleLabelClass();
    },

    setSwitcherState: function (isToggled) {
        $(Wick.VatToggle.el.switchVatInput).attr('checked', isToggled);
    },

    getSwitcherState: function () {
        if ($(Wick.VatToggle.el.switchVatInput).length) {
            return !!$(Wick.VatToggle.el.switchVatInput).attr('checked');
        } else {
            return null;
        }
    },

    createToggleEvent: function (isToggled) {
        return new CustomEvent(Wick.VatToggle.TOGGLE_VAT_EVENT_NAME, {
            detail: { toggled: isToggled },
        });
    },

    toggleLabelClass: function () {
        $(Wick.VatToggle.el.switchVatLabel).toggleClass(Wick.VatToggle.el.switchLabelCheckedClass);
    },

    isToggleVatAvailable: function () {
        return window.ACC.app.isVatToggleAvailable;
    },

    isOn: function () {
        return !!Wick.VatToggle.getCookie(Wick.VatToggle.VAT_COOKIE);
    },

    toggleDisplayClass: function () {
        const includingVatEl = Wick.VatToggle.el.includingVat;
        const excludingVat = Wick.VatToggle.el.excludingVat;
        const hiddenClassName = Wick.VatToggle.HIDDEN_CLASS_NAME;

        $(includingVatEl).toggleClass(hiddenClassName);
        $(excludingVat).toggleClass(hiddenClassName);
    },
    reapplyDisplayClass: function () {
        const includingVatEl = Wick.VatToggle.el.includingVat;
        const excludingVatEl = Wick.VatToggle.el.excludingVat;
        const hiddenClassName = Wick.VatToggle.HIDDEN_CLASS_NAME;

        let vatState = null;

        // check switcher state for web or cookie for app
        if (Wick.VatToggle.getSwitcherState() !== null) {
            vatState = Wick.VatToggle.getSwitcherState();
        } else {
            vatState = Wick.VatToggle.isOn();
        }

        if (vatState) {
            $(includingVatEl).addClass(hiddenClassName);
            $(excludingVatEl).removeClass(hiddenClassName);
        } else {
            $(includingVatEl).removeClass(hiddenClassName);
            $(excludingVatEl).addClass(hiddenClassName);
        }
    },

    updateGlobalClass: function () {
        const includingVatGlobal = Wick.VatToggle.el.includingVatGlobalClass;
        const excludingVatGlobal = Wick.VatToggle.el.excludingVatGlobalClass;

        if (Wick.VatToggle.isOn()) {
            $('body').removeClass(includingVatGlobal).addClass(excludingVatGlobal);
        } else {
            $('body').removeClass(excludingVatGlobal).addClass(includingVatGlobal);
        }
    },

    getCookie: function (cookieName) {
        const cookie = decodeURIComponent(document.cookie)
            .split(';')
            .map((cookie = '') => cookie.trim())
            .find((cookie = '') => cookie.startsWith(`${cookieName}=`));

        if (cookie) {
            const cookieValue = cookie.replace(`${cookieName}=`, '');
            return covertStringBooleanToBoolean(cookieValue);
        }

        return '';
    },

    setCookie: function (cookie, value) {
        const cookiesString = `${cookie}=${encodeURIComponent(value)};path=/`;
        document.cookie = cookiesString;
    },

    init: function () {
        const isVatShouldBeToggled = Wick.VatToggle.isOn();
        const isVatToggled = !!Wick.VatToggle.getSwitcherState();

        if (isVatShouldBeToggled) {
            Wick.VatToggle.setLabelState();
        }
        Wick.VatToggle.setSwitcherState(isVatShouldBeToggled);
        Wick.VatToggle.updateGlobalClass();

        $(Wick.VatToggle.el.switchVatInput).on('change', Wick.VatToggle.toggle);

        $(window).on(Wick.VatToggle.TOGGLE_VAT_EVENT_NAME, function () {
            Wick.VatToggle.toggleDisplayClass();
            Wick.VatToggle.updateGlobalClass();
        });

        if (isVatShouldBeToggled !== isVatToggled) {
            const event = Wick.VatToggle.createToggleEvent(isVatShouldBeToggled);
            window.dispatchEvent(event);
        }
    },
};

function covertStringBooleanToBoolean(stringBoolean) {
    if (stringBoolean === 'true') {
        return true;
    }
    if (stringBoolean === 'false') {
        return false;
    }
    return stringBoolean;
}

$(document).ready(function () {
    Wick.VatToggle.init();
});
