// Site-specific elements
const elementIdUserName = "user-name";
const elementIdUserPassword = "user-pwd";
const elementIdUserEmail = "user-email";
const elementIdUserHint = "user-hint";

function getLoginName() {
    return document.getElementById(elementIdUserName).value;
}

function clearLoginName() {
    document.getElementById(elementIdUserName).value = "";
}

function setLoginName(name) {
    document.getElementById(elementIdUserName).value = name;
}

function getLoginPassword() {
    return document.getElementById(elementIdUserPassword).value;
}

function clearLoginPassword() {
    document.getElementById(elementIdUserPassword).value = "";
}

function setLoginPassword(password) {
    document.getElementById(elementIdUserPassword).value = password;
}

function getLoginEmail() {
    return document.getElementById(elementIdUserEmail).value;
}

/*
 * Set a text to indicate an action, warning or other message below the registration field;
 */
function setTextHint(text) {
    document.getElementById(elementIdUserHint).innerHTML = text;
}

/*
 * Attempt to reopen an existing session, either from the login or register page.
 *
 * It could be argued that this shouldn't be attempted for the registration page,
 * but, a user may accidentally go to the registration page through the browser history,
 * and then it will be convenient to be redirected to the game.
 */
function loadExistingSession() {
    const token = loadSessionToken();
    if (token == null) {
        console.log("No session token to load")
        return;
    }
    resumeSessionRequest(token)
    .then(function(name) {
        storePlayerNameAndToken(name, token);
        showTableList();
    }).catch(function(error) {
        // We don't expect a session to resume if the registration page is shown,
        // and if the token is expired on the login page we just fail quietly.
        console.log(error);
        deleteSessionToken();
    })
}

function showTableList() {
    window.location.href = "list.html";
}
