const version = document.querySelector("#version")
const information = document.querySelector(".information")

document.addEventListener("DOMContentLoaded", async () => {
    version.innerText = `Launcher ${await system.call("launcher::version")}`
})

function showInformation() {
    return information.removeAttribute("hidden")
}

function hideInformation() {
    return information.setAttribute("hidden", "")
}

async function login() {
    const username = document.querySelector("#username").value
    const password = document.querySelector("#password").value
    const result = await system.call("auth::lentia", { username, password })
    await processLoginResult(result)
}

function clearPassword() {
    password.value = ""
}

async function requestLoginWithOAuth2() {
    showFrame("oauth2")
    hideInformation()
    showLoadingBar()
    const result = await system.call("oauth2::discord")
    await processLoginResult(result, true)
}

async function processLoginResult(payload, showDefaultPage = false) {
    if (payload.success == false) {
        await system.call("dialog::error", { 
            title: payload.error.error,
            message: payload.error.errorMessage 
        })
        if (showDefaultPage) {
            hideLoadingBar()
            showFrame("provider")
        }
    }
}

hideLoadingBar()
showFrame("provider")