var isPlaying = false;

registeredName = localStorage.getItem('name');
didAcceptInvitation = localStorage.getItem('registered');

var currentLang = 'de';
console.log(window.navigator.language);
if (!window.navigator.language.includes('de')) {
    currentLang = 'en';
}
function localize() {
    let lang = ':lang(' + currentLang + ')';
    let show = '[lang]' + lang;
    document.querySelectorAll(show).forEach(function (node) {
        node.style.display = 'unset';
    });
    let hide = '[lang]:not(' + lang + ')';
    document.querySelectorAll(hide).forEach(function (node) {
        node.style.display = 'none';
    });
}

function showRegisterForm() {
    document.getElementById("text-container").style.top = '2%'
    document.getElementById("register-button").style.opacity = 0
    document.getElementById("participation-buttons").style.display = 'inline-block'
    document.getElementById("name-form").style.display = 'inherit'
    document.getElementById("add-calender-event").style.display = 'none'
    document.getElementById("guest-name").focus()
    document.getElementById("info-button").style.display = 'none';
    if (!isPlaying) {
        playPause();
    }
}

function playPause() {
    var audio = document.getElementById("audio");
    var playButton = document.getElementById("play-button");
    var pauseButton = document.getElementById("pause-button");
    var playText = document.getElementById("playing-text");
    var pauseText = document.getElementById("paused-text");
    if (isPlaying) {
        audio.pause();
        playButton.style.display = "inherit";
        pauseButton.style.display = "none";
        playText.style.display = "none";
        pauseText.style.display = "table-cell";
    } else {
        audio.play();
        playButton.style.display = "none";
        pauseButton.style.display = "inherit";
        playText.style.display = "table-cell";
        pauseText.style.display = "none";
    }
    isPlaying = !isPlaying;
}

async function participate(isParticipating) {
    let value = document.getElementById("guest-name").value
    if (!value) {
        return
    }
    let name = value.trim()
    document.getElementById("participation-buttons").style.display = 'none'
    document.getElementById("name-form").style.display = 'none'
    document.getElementById("info-button").style.display = 'inherit'
    console.log("Registering: " + name);
    localStorage.removeItem('name');
    localStorage.removeItem('registered');
    if (isParticipating) {
        let registered = await performRequest(name, "register")
        if (registered) {
            document.getElementById("registered-text").style.display = 'inherit';
            document.getElementById("add-calender-event").style.display = 'inherit';
            localStorage.setItem('name', name);
            localStorage.setItem('registered', 'true');
        } else {
            document.getElementById("error-text").style.display = 'inherit'
        }
    } else {
        let declined = await performRequest(name, "decline")
        if (declined) {
            document.getElementById("declined-text").style.display = 'inherit';
            localStorage.setItem('name', name);
            localStorage.setItem('registered', 'false');
        } else {
            document.getElementById("error-text").style.display = 'inherit'
        }
    }
}

function handleErrors(response) {
    if (!response.ok) throw new Error(response.status);
    return response.text();
}

function catchErrors(error) {
    console.log(error);
    return "Failed";
}

async function performRequest(name, path) {
    let text = await fetch("/festival/api/" + path, {
        method: 'POST',
        body: name
    }).then(handleErrors).catch(catchErrors);
    if (text == "Success") {
        console.log("Registered: " + name);
        return true;
    }
    console.log("Failed to register: " + text);
    return false;
}

function downloadCalender() {
    if (currentLang == 'de') {
        download('/festival/event.ics')
    } else {
        download('/festival/event-en.ics')
    }
}

function download(file) {
    window.location = file
}
