(function () {
    const root = document.documentElement;
    const toggleButton = document.getElementById("theme-toggle");
    const toggleIcon = toggleButton ? toggleButton.querySelector("i") : null;

    const storage = {
        get(key) {
            try {
                return window.localStorage.getItem(key);
            } catch (error) {
                return null;
            }
        },
        set(key, value) {
            try {
                window.localStorage.setItem(key, value);
            } catch (error) {
                return false;
            }

            return true;
        }
    };

    const setTheme = (theme) => {
        const isDark = theme === "dark";

        if (isDark) {
            root.setAttribute("data-theme", "dark");
        } else {
            root.removeAttribute("data-theme");
        }

        if (toggleIcon) {
            toggleIcon.classList.toggle("fa-sun", isDark);
            toggleIcon.classList.toggle("fa-moon", !isDark);
        }

        if (toggleButton) {
            toggleButton.setAttribute("aria-pressed", String(isDark));
            toggleButton.title = isDark ? "Switch to light theme" : "Switch to dark theme";
        }
    };

    const savedTheme = storage.get("theme") || "light";
    setTheme(savedTheme);

    if (toggleButton) {
        toggleButton.addEventListener("click", () => {
            const nextTheme = root.hasAttribute("data-theme") ? "light" : "dark";
            setTheme(nextTheme);
            storage.set("theme", nextTheme);
        });
    }

    const printButton = document.getElementById("print-resume");
    if (printButton) {
        printButton.addEventListener("click", () => window.print());
    }

    const contactKey = 43;
    const contactValues = {
        email: [65, 74, 88, 68, 69, 78, 94, 76, 78, 69, 78, 76, 74, 89, 89, 66, 88, 68, 69, 107, 66, 72, 71, 68, 94, 79, 5, 72, 68, 70],
        ky: [3, 25, 28, 27, 2, 11, 19, 19, 18, 6, 19, 27, 28, 25],
        tn: [3, 19, 29, 30, 2, 11, 18, 19, 30, 6, 26, 29, 31, 26]
    };

    const revealContact = (contactType) => {
        const encoded = contactValues[contactType];

        if (!encoded) {
            return "";
        }

        return encoded.map((code) => String.fromCharCode(code ^ contactKey)).join("");
    };

    const revealButtonContact = (button) => {
        const contactType = button.dataset.contact;
        const value = revealContact(contactType);

        if (!value) {
            return;
        }

        const href = contactType === "email"
            ? `mailto:${value}`
            : `tel:${value.replace(/\D/g, "")}`;

        const link = document.createElement("a");
        link.href = href;
        link.rel = "nofollow";
        link.textContent = value;
        button.replaceWith(link);
    };

    document.querySelectorAll("[data-contact]").forEach((button) => {
        button.addEventListener("click", () => revealButtonContact(button));
    });

    window.addEventListener("beforeprint", () => {
        document.querySelectorAll("[data-contact]").forEach(revealButtonContact);
    });
})();
