/**
 * CAM ADA Frontend
 * - Handles font resizing and high-contrast mode (toolbar)
 * - Applies saved custom attributes from admin
 */
(function () {
    document.addEventListener("DOMContentLoaded", function () {
        const toolbar = document.getElementById("camanager-toolbar");
        const html = document.documentElement;
        const body = document.body;

        /** ========================
         * Apply Saved Attributes
         * ======================== */
        if (typeof camFrontend !== "undefined" && camFrontend.attributes) {
            Object.values(camFrontend.attributes).forEach(attr => {
                try {
                    const nodes = document.querySelectorAll(attr.selector);
                    if (nodes.length) {
                        nodes.forEach(node => node.setAttribute(attr.attr, attr.value));
                    } else {
                        console.warn(
                            "CAM: Selector did not match any elements:",
                            attr.selector
                        );
                    }
                } catch (e) {
                    console.error("CAM: Error applying attribute", attr, e);
                }
            });
        }

        /** ========================
         * Toolbar Logic
         * ======================== */
        if (!toolbar) return;

        // Default state
        let fontScale = parseFloat(localStorage.getItem("camanager-font-scale")) || 1.0;
        let contrastEnabled = localStorage.getItem("camanager-contrast") === "true";

        // Apply initial states
        applyFontScale(fontScale);
        if (contrastEnabled) {
            body.classList.add("camanager-high-contrast");
        }

        toolbar.addEventListener("click", function (e) {
            if (!e.target.dataset.action) return;

            switch (e.target.dataset.action) {
                case "increase-font":
                    fontScale = Math.min(fontScale + 0.1, 2.0); // cap at 2x
                    localStorage.setItem("camanager-font-scale", fontScale);
                    applyFontScale(fontScale);
                    break;

                case "decrease-font":
                    fontScale = Math.max(fontScale - 0.1, 0.6); // min 0.6x
                    localStorage.setItem("camanager-font-scale", fontScale);
                    applyFontScale(fontScale);
                    break;

                case "high-contrast":
                    contrastEnabled = !contrastEnabled;
                    localStorage.setItem("camanager-contrast", contrastEnabled);
                    body.classList.toggle("camanager-high-contrast", contrastEnabled);
                    break;
            }
        });

        function applyFontScale(scale) {
            html.style.fontSize = (100 * scale).toFixed(0) + "%";
        }
    });

    if (!window.camFrontend || !camFrontend.attributes) return;

    try {
        Object.values(camFrontend.attributes).forEach(attr => {
            const elements = document.querySelectorAll(attr.selector);
            elements.forEach(el => {
                el.setAttribute(attr.attr, attr.value);
            });
        });
    } catch (e) {
        console.warn("CAM attribute error:", e);
    }
})();
