document.addEventListener('DOMContentLoaded', function () {
    const modalCookieSettingsElement = document.querySelector('#modal-cookie-settings');

    let cookieSettings = document.getElementById("cookie-settings");

    if (document.cookie.indexOf('cookies-consent') === -1) {
        cookieSettings.style.bottom = '0%';
        setTimeout(() => { cookieSettings.style.bottom = '0%'; }, 0);
    }

    document.querySelectorAll('.btn-cookies-consent').forEach(button => {
        button.addEventListener('click', function (event) {
            event.preventDefault();
            let cookiesLevel = this.getAttribute('data-cookies');
            if (cookiesLevel !== null) {
                document.cookie = `cookies-consent=${cookiesLevel};max-age=${365 * 24 * 60 * 60}`;
            } else {
                cookiesLevel = "";

                const checkFunctionalCookies = document.getElementById('functional-cookies');
                const checkAnalyticsCookies = document.getElementById('analytics-cookies');
                const checkProfileCookies = document.getElementById('profile-cookies');

                if (checkFunctionalCookies.checked) cookiesLevel += "1";
                else cookiesLevel += "0";

                if (checkAnalyticsCookies.checked) cookiesLevel += "1";
                else cookiesLevel += "0";

                if (checkProfileCookies.checked) cookiesLevel += "1";
                else cookiesLevel += "0";

                document.cookie = `cookies-consent=${cookiesLevel};max-age=${365 * 24 * 60 * 60}`;
            }

            const modal = bootstrap.Modal.getInstance(modalCookieSettingsElement);
            if (modal) modal.hide();

            if (cookieSettings) setTimeout(() => {
                cookieSettings.style.transition = "bottom 1s";
                cookieSettings.style.bottom = '-100%';
            }, 1000);

            if (cookiesLevel[0] == 1) document.querySelectorAll(".google-map-func").forEach(googleMap => googleMap.innerHTML = `<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2616.891113063286!2d12.10357687683789!3d49.0126564898375!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x479fc10aad3c56e3%3A0x4e1e3fd12a6be9a3!2sImmobilienverwaltung%20V%C3%B6lkl!5e0!3m2!1sde!2sde!4v1692953660472!5m2!1sde!2sde" width="100%" height="600" style="border:0;" allowfullscreen="" loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>`);
        });
    });

    modalCookieSettingsElement.addEventListener('shown.bs.modal', event => {
        const checkFunctionalCookies = document.getElementById('functional-cookies');
        const checkAnalyticsCookies = document.getElementById('analytics-cookies');
        const checkProfileCookies = document.getElementById('profile-cookies');

        let cookiesConsent = getCookie('cookies-consent');

        if (cookiesConsent) {
            if (cookiesConsent[0] == 1) checkFunctionalCookies.checked = true;
            else checkFunctionalCookies.checked = false;

            if (cookiesConsent[1] == 1) checkAnalyticsCookies.checked = true;
            else checkAnalyticsCookies.checked = false;

            if (cookiesConsent[2] == 1) checkProfileCookies.checked = true;
            else checkProfileCookies.checked = false;
        }
    });
});

function getCookie(cookieName) {
    const cookies = document.cookie;

    const cookiesArray = cookies.split('; ');

    for (let i = 0; i < cookiesArray.length; i++) {
        const cookie = cookiesArray[i];
        const [cookieKey, cookieValue] = cookie.split('=');

        if (cookieKey.trim() === cookieName) return decodeURIComponent(cookieValue);
    }

    return null;
}