// SETUP:
//
// npm i --g web-ext
// git clone git@gitlab.constantvzw.org:declarations/extension.orchestra.git
// cd extension.orchestra
// web-ext run
//
// and then make edits below:
    
console.log("Super audio enabled!");

let sup = {};

sup.settings = {};
sup.settings.url = "https://tell.declarations.style/static/sound/"
sup.settings.afkTime = 10000;
sup.settings.loopSpeed = 500;

function newAudio( filename ) {
    const audio = new Audio( sup.settings.url + filename + '.mp3' )
    audio.crossOrigin = true
    return audio
}

sup.pageLoadTime = Date.now();
sup.lastMouseMove = Date.now();
sup.loop = undefined;

sup.audio = {};

    sup.audio.click = newAudio('weather-music');
    sup.audio.interact = newAudio('three-alerts');
    sup.audio.interact.volume = 0.3;

    sup.audio.context = new AudioContext();
    sup.audio.track = {};
    sup.audio.track.click = sup.audio.context.createMediaElementSource(sup.audio.click);
    sup.audio.track.click.connect(sup.audio.context.destination);
    sup.audio.track.interact = sup.audio.context.createMediaElementSource(sup.audio.interact);
    sup.audio.track.interact.connect(sup.audio.context.destination);

    sup.loop = setTimeout(audioUpdateLoop, sup.settings.loopSpeed);

    // Hover events
    document.addEventListener("mouseover", (e) => {
        if (e.target.tagName == "A") {
            // Link
            console.log(sup.audio.interact)
            sup.audio.interact.play();
        } else if (e.target.tagName == "IMG") {
            // Image
            sup.audio.interact.play();
        } else if (e.target.tagName == "P") {
            // Image
            sup.audio.interact.play();
        }
    });

    document.addEventListener("click", (e) => {
        if (e.target.tagName == "A") {
            sup.audio.click.play();
        }
    });

    // Mouse Movement General
    document.addEventListener("mousemove", (e) => {
        sup.lastMouseMove = Date.now();
    });

    // Mouse Click Up Events
    document.addEventListener("mouseup", (e) => {
        // let selectedText = document.selection.createRange().text;

        if (selectedText != undefined) {
            // Play selected text sound
        }

        if (e.target.tagName == "A") {
            sup.audio.click.play();
        }
    });

function audioUpdateLoop() {
    let now = Date.now();
    let timeSinceLLoad = now - sup.pageLoadTime;
}