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

function bloonsa_level_completed(level_id) {
    if (document.getElementById("login_box") != null) {
        return
    }
    console.log("call to bloonsa_level_completed");
    bloonsa_update_data(level_id)
}

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

// TODO only update neccessary parts
// eg: new_level_started -> level info box
//     level_completed -> all
//     level_rated -> level info box


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.timeout = 5000;
    xhr.send(JSON.stringify({
        level_id: level_id
    }));
}

function set_stars(rating) {
    for (let i = 0; i < 10; i++) {
        let star = document.getElementById(`level-star${i}`);
        if (i < rating) {
            star.classList.add("filter-orange")
        } else {
            star.classList.remove("filter-orange")
        }
    };
}

function bloonsa_update_html(r) {

    var url_path = `/a/game/${r.level_id}`
    if (window.location.pathname != url_path) {
        window.history.pushState({}, r.level_title, url_path);
    }

    document.getElementById("level-info-box").classList.remove("invisible")

    // Level Infobox
    document.getElementById("level-name").textContent = r.level_title;
    document.getElementById("level-id").textContent = `${r.level_id}`;
    document.getElementById("level-author-name").textContent = r.level_author_name;
    document.getElementById("level-author-id").textContent = `${r.level_author_id}`;

    set_stars(r.level_rating);

    document.getElementById("level-wins-number").textContent = r.level_wins;
    document.getElementById("level-wins-percentage").textContent = (r.level_win_percentage * 100).toFixed(2) + "%";
    document.getElementById("level-plays-number").textContent = r.level_plays;
    document.getElementById("level-glitch-number").textContent = r.dart_glitch_count;

    // Personal best
    if (document.getElementById("login_box") != null) {
        document.getElementById("level-pb").remove();
        return
    }
    var darts_img = document.getElementById("level-pb-darts-img");
    if ("score_pops" in r) {
        document.getElementById("level-pb-pops-number").textContent = r.score_pops;
        document.getElementById("level-target").textContent = r.level_target;
        document.getElementById("level-pb-darts-number").textContent = r.score_darts_left;
        document.getElementById("level-pb-darts-glitch").textContent = r.score_dart_glitch ? "+1": "";

        if (r.score_dart_glitch | r.score_dart_glitch_ever) {
            darts_img.classList.add("filter-orange");
        } else {
            darts_img.classList.remove("filter-orange");
        }

    } else {
        document.getElementById("level-pb-pops-number").textContent = "0";
        document.getElementById("level-target").textContent = r.level_target;
        document.getElementById("level-pb-darts-number").textContent = "-";
        document.getElementById("level-pb-darts-glitch").textContent = "";
        darts_img.classList.remove("filter-orange");
    }

    if (r.level_cleared) {
        document.getElementById("level-pb-pops-number").classList.add("clear-color");
        document.getElementById("level-pb-pops-separator").classList.add("clear-color");
        document.getElementById("level-target").classList.add("clear-color");
    } else {
        document.getElementById("level-pb-pops-number").classList.remove("clear-color");
        document.getElementById("level-pb-pops-separator").classList.remove("clear-color");
        document.getElementById("level-target").classList.remove("clear-color");
    }

    // Profile
    document.getElementById("profile-plays-number").textContent = r.bloonsa_levels_played;
    document.getElementById("profile-clears-number").textContent = r.bloonsa_levels_beaten;
    document.getElementById("profile-plays-max").textContent = r.bloonsa_total_levels;
    document.getElementById("profile-clears-max").textContent = r.bloonsa_total_levels;
}