let userAccessLevels = { // Name | Text Colour | Background Colour | Border Colour
    "-3": ["Banned", "#fff", "#2e2e2e", "#727272"],
    "-2": ["Banned", "#fff", "#2e2e2e", "#727272"],
    "-1": ["Banned", "#fff", "#2e2e2e", "#727272"],
    "0": ["Member", "#fff", "#047205", "#00a003"],
    "1": ["Tester", "#fff", "#0c98a2", "#01c9d7"],
    "2": ["Moderator", "#000", "#00dc04", "#067708"],
    "3": ["Admin", "#fff", "#920606", "#c80404"],
}

function updateUserDataDisplay(data) {
    document.getElementById("display-name").textContent = data["mii"]["name"];
    document.getElementById("sfid").textContent = `SFID: ${data["user_id"]}`;

    document.getElementById("email").innerHTML = `<strong>Email: </strong>${data["email"]["address"]}`;
    document.getElementById("dob").innerHTML = `<strong>Date of Birth: </strong>${data["birth_date"]}`;

    let createdAt = new Date(data["create_date"] + "Z");
    document.getElementById("created-at").innerHTML = `<strong>Created: </strong>${createdAt.toLocaleString()}`

    document.getElementById("tz").innerHTML = `<strong>Timezone: </strong>${data["tz_name"]}`;
    document.getElementById("region").innerHTML = `<strong>Country/Region: </strong>${data["country"]}`;

    document.getElementById("user-info").style.display = "flex";

    let miiLink = `https://mii.spfn.net/${data["pid"]}/main.png`
    document.getElementById("mii-img").src = miiLink;

    let level = userAccessLevels[data["account_level"]];
    if (!level) level = ["Unknown", "#fff", "#000000ff", "#ffffffff"]; // Refer to comment on userAccessLevels for a formatting guide

    document.getElementById("access-level").textContent = level[0];
    document.getElementById("access-level").style = `color: ${level[1]}; background-color: ${level[2]}; border-color: ${level[3]}`;
}

function logOut() {
    sessionStorage.clear();
    window.location.href = "/account/login?redirect=/account"
}



// Check if there is an active login
window.onload = async function() {
    let expiryStr = sessionStorage.getItem("authExpires");
    if (expiryStr) {
        let expiry = new Date(parseInt(expiryStr));

        if (expiry > new Date()) { // Hasn't expired - Get user data
            let token = sessionStorage.getItem("authToken");

            const response = await fetch("https://account.spfn.net/api/v2/users/@me/profile", {
                method: "GET",
                headers: {
                    "Authorization": `Bearer ${token}`
                }
            })
            if (!response.ok) throw new Error("Network Response was not okay when requesting Profile")
        
            const data = await response.json();

            updateUserDataDisplay(data);
            document.getElementById("loading-screen").style.display = "none";
        
        } else { // Has expired - Prompt user to log back in
            window.location.href = "/account/login?redirect=/account"
        }
    } else { // User has never logged in for this session
        window.location.href = "/account/login?redirect=/account"
    }
}

function showDeleteWarning() {
    document.getElementById("confirm-delete").style = ""
    document.getElementById("user-info").style = "display: none;"
}

function hideDeleteWarning() {
    document.getElementById("user-info").style = ""
    document.getElementById("confirm-delete").style = "display: none;"
}

async function deleteAccount() {
    let token;

    let expiryStr = sessionStorage.getItem("authExpires");
    if (expiryStr) { // Expiry exists so token should exist
        token = sessionStorage.getItem("authToken");

        let expiry = new Date(expiryStr);
        if (expiry < new Date()) { // Expired token
            window.location.href = "/account/login?redirect=/account"
        } else if (!token) { // Expiry Saved but No Token (shouldn't be possible but it'll be caught if it happens)
            window.location.href = "/account/login?redirect=/account"
        }
    } else { // Token Never Saved in Session
        window.location.href = "/account/login?redirect=/account"
    }

    const response = await fetch("https://account.spfn.net/api/v2/users/@me/delete", {
        method: "GET",
        headers: {
            "Authorization": `Bearer ${token}`
        }
    })

    if (!response.ok) {
        loginError(await response.text(), response.status);
        throw new Error("Network Response was not okay when requesting Account Deletion");
    }

    document.getElementById("confirm-delete").style = "display: none;"
    document.getElementById("delete-success").style = ""
}