function bloonsa_new_level_started(level_id) {
    console.log("call to new_level_started");
    bloonsa_update_data(level_id);
}

function bloonsa_level_completed(level_id) {
    bloonsa_update_data(level_id)
}

function bloonsa_update_data(level_id) {
    const xhr = new XMLHttpRequest();
    console.log("call to update_data");
    xhr.open("POST", "/bloonsa_api/get_status_data", true);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            try {
                var r = JSON.parse(xhr.response);

                bloonsa_update_html(r);

            } catch (error) {
                console.error("Error:", error);
            }
        }
    };
    xhr.send(JSON.stringify({
        level_id: level_id
    }));
}

function bloonsa_update_html(r) {
    document.getElementById("level-title").textContent = r.level_title;
    document.getElementById("level-id").textContent = `(${r.level_id})`;
    document.getElementById("level-author").textContent = `by ${r.level_author_name}`;
    document.getElementById("level-author-id").textContent = `(${r.level_author_id})`;
    if (r.level_cleared) {
        document.getElementById("level-clear-state").textContent = `✅`;
    }
    document.getElementById("levels_played").textContent = `${r.bloonsa_levels_played}`;
    document.getElementById("levels_beaten").textContent = `${r.bloonsa_levels_beaten}`;
    document.getElementById("total_levels").textContent = `${r.bloonsa_total_levels}`;



}