//let customLocation = document.location;
document.addEventListener('DOMContentLoaded', () => {
    if (customLocation.pathname.slice(0, 3) !== "/en") {
        let words = document.querySelectorAll(".asw-translate")
        let ukrWords = [
            "Меню доступності",
            "Збільшити розмір тексту",
            "Зменшити розмір тексту",
            "Міжрядковий інтервал",
            "Міжлітерний інтервал",
            "Дислексія",
            "Підсвічування заголовків",
            "Підсвічування посилань",
            "Жирний шрифт",
            "Більший курсор",
            "Контрастність"
        ]
        for (let i = 0; i < words.length; i++) {
            words[i].innerHTML = ukrWords[i]
        }
    }
})
function toggleMenu() {
    var menu = document.getElementById("aswMenu");
    if (menu.style.display === "none") {
        menu.style.display = "block";
    } else {
        menu.style.display = "none";
    }
}
function adjustFontSize(multiply = 0) {
    const storedPercentage = parseFloat(localStorage.getItem('fontPercentage'));
    if (multiply) {
        if (storedPercentage) {
            const newPercentage = storedPercentage + multiply;
            localStorage.setItem('fontPercentage', newPercentage);
        } else {
            const newPercentage = 1 + multiply;
            localStorage.setItem('fontPercentage', newPercentage);
        }
    }
    document
        .querySelectorAll("*")
        .forEach((el) => {
            if (!el.classList.contains('material-icons')) {
                let orgFontSize = parseFloat(el.getAttribute('data-asw-orgFontSize'));

                if (!orgFontSize) {
                    orgFontSize = parseFloat(window.getComputedStyle(el).getPropertyValue('font-size'));
                    el.setAttribute('data-asw-orgFontSize', orgFontSize);
                }
                let adjustedFontSize = orgFontSize * (parseFloat(localStorage.getItem('fontPercentage')) || 1);
                el.style['font-size'] = adjustedFontSize + 'px';
            }
        });
}


function adjustLetterSpacing(increment = 0) {
    let isLetterSpacingEnabled = parseInt(localStorage.getItem('isLetterSpacingEnabled'));
    if (!increment) {
        isLetterSpacingEnabled = !isLetterSpacingEnabled;
        increment = 0.1;
    }
    if (!isLetterSpacingEnabled) {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                if (!el.classList.contains('material-icons')) {

                    let orgLetterSpacing = el.getAttribute('data-asw-orgLetterSpacing');

                    if (!orgLetterSpacing) {
                        orgLetterSpacing = el.style['letter-spacing'];
                        el.setAttribute('data-asw-orgLetterSpacing', orgLetterSpacing);
                        if (!(orgLetterSpacing)) {
                            orgLetterSpacing = 0;
                        }
                        orgLetterSpacing = parseFloat(orgLetterSpacing);
                        let newLetterSpacing = orgLetterSpacing + increment;
                        el.style['letter-spacing'] = newLetterSpacing + 'em';
                    }
                }
            });

        localStorage.setItem('isLetterSpacingEnabled', 1);
    } else {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                if (!el.classList.contains('material-icons')) {
                    let orgLetterSpacing = el.getAttribute('data-asw-orgLetterSpacing');
                    if (orgLetterSpacing) {
                        el.style['letter-spacing'] = orgLetterSpacing;
                        el.removeAttribute('data-asw-orgLetterSpacing');
                    }
                    else {
                        el.style.removeProperty('letter-spacing');
                    }
                }
            });

        localStorage.setItem('isLetterSpacingEnabled', 0);
    }
}

function enableDyslexicFont(load = false) {
    let isDyslexicFontEnabled = parseInt(localStorage.getItem('isDyslexicFontEnabled'));
    if (load) {
        isDyslexicFontEnabled = !isDyslexicFontEnabled;
    }
    if (!isDyslexicFontEnabled) {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                if (!el.classList.contains('material-icons')) {
                    orgFontFamily = el.style['font-family'];
                    el.setAttribute('data-asw-orgFontFamily', orgFontFamily);
                    el.style['font-family'] = 'OpenDyslexic3';
                }
            });

        localStorage.setItem('isDyslexicFontEnabled', 1);
    } else {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                if (!el.classList.contains('material-icons')) {
                    orgFontFamily = el.getAttribute('data-asw-orgFontFamily');
                    if (orgFontFamily) {
                        el.style['font-family'] = orgFontFamily;
                        el.removeAttribute('data-asw-orgFontFamily');
                    }
                    else {
                        el.style.removeProperty('font-family');
                    }
                }
            });

        localStorage.setItem('isDyslexicFontEnabled', 0);
    }
}


//second file for embed webflow
function enableBigCursor(load = false) {
    let isBigCursorEnabled = parseInt(localStorage.getItem('isBigCursorEnabled'));
    if (load) {
        isBigCursorEnabled = !isBigCursorEnabled;
    }
    if (!isBigCursorEnabled) {
        document
            .querySelectorAll("*")
            .forEach((el) => {                
                el.classList.add("customCursor")
            });
        localStorage.setItem('isBigCursorEnabled', 1);
    } else {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                el.classList.remove("customCursor")
            });

        localStorage.setItem('isBigCursorEnabled', 0);
    }
}


function enableHighlightLinks(load = false) {
    let isHighlightLinks = parseInt(localStorage.getItem('isHighlightLinks'));
    if (load) {
        isHighlightLinks = !isHighlightLinks;
    }
    if (!isHighlightLinks) {
        document.querySelectorAll('a,button').forEach((anchor) => {
            const orgTextDecoration = window.getComputedStyle(anchor).getPropertyValue('text-decoration');
            const orgFontWeight = window.getComputedStyle(anchor).getPropertyValue('font-weight');
            const orgFontSize = window.getComputedStyle(anchor).getPropertyValue('font-size');
            const orgLinkColor = window.getComputedStyle(anchor).getPropertyValue('color');
            anchor.setAttribute('data-asw-orgLinkTextDecoration', orgTextDecoration);
            anchor.setAttribute('data-asw-orgLinkFontWeight', orgFontWeight);
            anchor.setAttribute('data-asw-orgLinkFontSize', orgFontSize);
            anchor.setAttribute('data-asw-orgLinkColor', orgLinkColor);
            anchor.style.textDecoration = 'underline';
            anchor.style.fontWeight = '800';
            anchor.style['font-size'] = parseInt(orgFontSize) * 1.1 + 'px';
            anchor.style['color'] = '#ff0000';
        });

        localStorage.setItem('isHighlightLinks', 1);
    } else {
        document.querySelectorAll('a,button').forEach((anchor) => {
            const orgTextDecoration = anchor.getAttribute('data-asw-orgLinkTextDecoration');
            const orgFontWeight = anchor.getAttribute('data-asw-orgLinkFontWeight');
            const orgFontSize = anchor.getAttribute('data-asw-orgLinkFontSize');
            const orgLinkColor = anchor.getAttribute('data-asw-orgLinkColor');
            anchor.style.color = orgLinkColor;
            anchor.style.fontSize = orgFontSize;
            anchor.style.textDecoration = orgTextDecoration;
            anchor.style.fontWeight = orgFontWeight;
        });

        localStorage.setItem('isHighlightLinks', 0);
    }
}


function enableHighlightHeadings(load = false) {
    let isHighlightHeadings = parseInt(localStorage.getItem('isHighlightHeadings'));
    if (load) {
        isHighlightHeadings = !isHighlightHeadings;
    }
    if (!isHighlightHeadings) {
        document.querySelectorAll('h1, h2, h3,heading1').forEach((heading) => {
            const orgTextDecoration = window.getComputedStyle(heading).getPropertyValue('text-decoration');
            const orgHighlightColor = window.getComputedStyle(heading).getPropertyValue('color');
            heading.setAttribute('data-asw-orgHighlightTextDecoration', orgTextDecoration)
            heading.setAttribute('data-asw-orgHighlightColor', orgHighlightColor);
            heading.style.color = '#ff0000';
            heading.style.textDecoration = 'underline';
        });

        localStorage.setItem('isHighlightHeadings', 1);
    } else {
        document.querySelectorAll('h1, h2, h3,heading1').forEach((heading) => {
            const orgTextDecoration = heading.getAttribute('data-asw-orgHighlightTextDecoration');
            const orgHighlightColor = heading.getAttribute('data-asw-orgHighlightColor');
            heading.style.textDecoration = orgTextDecoration;
            heading.style.color = orgHighlightColor;
        });

        localStorage.setItem('isHighlightHeadings', 0);
    }
}


function adjustLineHeight(increment = 0) {
    let isLineHeightEnabled = parseInt(localStorage.getItem('isLineHeightEnabled'));
    if (!increment) {
        isLineHeightEnabled = !isLineHeightEnabled;
        increment = 1;
    }
    if (!isLineHeightEnabled) {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                if (!el.classList.contains('material-icons')) {
                    let orgLineHeight = el.getAttribute('data-asw-orgLineHeight');

                    if (!orgLineHeight) {
                        orgLineHeight = el.style['line-height'];
                        el.setAttribute('data-asw-orgLineHeight', orgLineHeight);
                        if (!orgLineHeight) {
                            orgLineHeight = 1.1;
                        }
                        orgLineHeight = parseFloat(orgLineHeight);
                        let newLineHeight = orgLineHeight + increment;
                        el.style['line-height'] = newLineHeight;
                    }
                }
            });

        localStorage.setItem('isLineHeightEnabled', 1);
    } else {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                if (!el.classList.contains('material-icons')) {
                    let orgLineHeight = el.getAttribute('data-asw-orgLineHeight');
                    if (orgLineHeight) {
                        el.style['line-height'] = orgLineHeight;
                        el.removeAttribute('data-asw-orgLineHeight');
                    }
                    else {
                        el.style.removeProperty('line-height');
                    }

                }
            });

        localStorage.setItem('isLineHeightEnabled', 0);
    }
}

//third part embed webflow
function adjustFontWeight(increment = 100) {
    let isFontWeightEnabled = parseInt(localStorage.getItem('isFontWeightEnabled'));
    if (increment === 100) {
        isFontWeightEnabled = !isFontWeightEnabled;
        increment = 400;
    }
    if (!isFontWeightEnabled) {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                if (!el.classList.contains('material-icons')) {
                    let orgBoldFontWeight = window.getComputedStyle(el).getPropertyValue('--org-bold-font-weight');
                    if (!orgBoldFontWeight) {
                        orgBoldFontWeight = window.getComputedStyle(el).getPropertyValue('font-weight');
                        el.style.setProperty('--org-bold-font-weight', orgBoldFontWeight);
                    }
                    let newFontWeight = parseInt(orgBoldFontWeight) + increment;
                    el.style.setProperty('font-weight', newFontWeight);
                }
            });
        localStorage.setItem('isFontWeightEnabled', 1);
    } else {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                if (!el.classList.contains('material-icons')) {
                    let orgBoldFontWeight = window.getComputedStyle(el).getPropertyValue('--org-bold-font-weight');
                    if (orgBoldFontWeight) {
                        el.style['font-weight'] = orgBoldFontWeight;
                    } else {
                        el.style.removeProperty('font-weight');
                    }
                    el.style.removeProperty('--org-bold-font-weight');
                }
            });
        localStorage.setItem('isFontWeightEnabled', 0);
    }
}

function adjustContrast(load = false) {
    let isContrastEnabled = parseInt(localStorage.getItem('isContrastEnabled'));
    if (load) {
        isContrastEnabled = !isContrastEnabled;
    }
    if (!isContrastEnabled) {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                let orgColor = el.getAttribute('data-asw-orgContrastColor');
                let orgBgColor = el.getAttribute('data-asw-orgContrastBgColor');

                if (!orgColor) {
                    orgColor = el.style.color;
                    el.setAttribute('data-asw-orgContrastColor', orgColor);
                }
                if (!orgBgColor) {
                    orgBgColor = window.getComputedStyle(el).getPropertyValue('background-color');
                    el.setAttribute('data-asw-orgContrastBgColor', orgBgColor);
                }

                el.style["color"] = '#ffff00';
                el.style["background-color"] = '#0000ff';
            });

        localStorage.setItem('isContrastEnabled', 1);
    } else {
        document
            .querySelectorAll("*")
            .forEach((el) => {
                let orgContrastColor = el.getAttribute('data-asw-orgContrastColor');
                let orgContrastBgColor = el.getAttribute('data-asw-orgContrastBgColor');
                if (orgContrastColor) {
                    el.style.color = orgContrastColor;
                } else {
                    el.style.removeProperty('color');
                }
                if (orgContrastBgColor) {
                    el.style.backgroundColor = orgContrastBgColor;
                } else {
                    el.style.removeProperty('background-color');
                }
                el.removeAttribute('data-asw-orgContrastColor');
                el.removeAttribute('data-asw-orgContrastBgColor');
            });
        localStorage.setItem('isContrastEnabled', 0);
    }
}
function onPageLoad() {
    adjustFontSize();
    adjustLetterSpacing();
    enableDyslexicFont(true);
    enableBigCursor(true);
    enableHighlightLinks(true);
    enableHighlightHeadings(true);
    adjustLineHeight();
    adjustFontWeight();
    adjustContrast(true);
}
onPageLoad();
function reset() {
    localStorage.clear();
    onPageLoad();
}