import SexyTooltip from "./SexyTooltip.js";

const tooltipDiv = document.getElementById("tooltipCarrier") as HTMLDivElement;
const tooltip = new SexyTooltip(tooltipDiv);

const dudes = document.querySelectorAll(".dude") as NodeListOf<HTMLImageElement>;
const shakers = document.querySelectorAll(".shakeable") as NodeListOf<HTMLElement>;
const emailLink = document.getElementById("emailLink") as HTMLAnchorElement;
let numDudesDroppingSickBeats: number = 0;

dudes.forEach((dude) => dude.addEventListener("mouseup", () => toggleDude(dude)));

function toggleDude(dude: HTMLImageElement) {
    if (dude.classList.contains("spinMe")) {
        numDudesDroppingSickBeats -= 1;
        dude.addEventListener("animationiteration", function listener() {
            dude.classList.remove("spinMe");
            dude.removeEventListener("animationiteration", listener as EventListenerOrEventListenerObject);
        });
    } else {
        numDudesDroppingSickBeats += 1;
        dude.classList.add("spinMe");
    }
    updateShakers();
}

function updateShakers() {
    shakers.forEach((shaker) => {
        if (numDudesDroppingSickBeats === 0) {
            shaker.classList.remove("shakeMe");
        } else if (!shaker.classList.contains("shakeMe")) {
            shaker.classList.add("shakeMe");
        }
    });
}

emailLink.addEventListener("click", (event) => {
    const myDomain = "gmail";
    const myTld = "com";
    const myName = "danjledda";
    const dot = ".";
    const at = "@";
    let link = "mailto:" + myName + myDomain + myTld;
    link = link.slice(0, 10) + dot + link.slice(10, 11) + dot + link.slice(11, 16) + at + link.slice(16, 21) + dot +
        link.slice(21);
    window.location.href = link;
});
