<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Link your account</title>
</head>
<body>
<p id="status">Linking your account...</p>
<script>
    let status = document.getElementById("status");
    let ws = new WebSocket("ws://" + window.location.host + "/api/link");
    ws.onmessage = (event) => {
        let data = JSON.parse(event.data);
        switch (data["type"]) {
            case "ping":
                ws.send(JSON.stringify({"type": "pong"}));
                break;
            case "nonce":
                status.innerText = "To link your account, type \"!link " + data["nonce"] + "\" in the chat of the stream you are watching";
                ws.send(JSON.stringify({"type": "success"}));
                break;
            case "success":
                status.innerText = "Account linked successfully!";
                setTimeout(() => {
                    window.location.href = "/";
                }, 1000);
                break;
            case "error":
                status.innerText = "Error linking account: " + data["error"];
                break;
        }
    };
</script>
</body>