/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2026 Hurry Curry! Contributors

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/

import { tr } from "./locale.ts";
import { ws } from "./main.ts";
import { PacketC } from "./protocol.ts";
import { System } from "./system.ts";

export class SConnectScreen extends System {
    disconnect_reason?: string

    override packet(p: PacketC): void {
        if (p.type == "disconnect" && "translation" in p.reason)
            this.disconnect_reason = tr(p.reason.translation.id)
    }

    override draw(ctx: CanvasRenderingContext2D): void {
        if (ws.readyState == ws.CONNECTING) this.draw_wait(ctx, "Connecting...")
        else if (ws.readyState == ws.CLOSING) this.draw_wait(ctx, "Closing...")
        else if (ws.readyState == ws.CLOSED) this.draw_wait(ctx, this.disconnect_reason ?? "Disconnected")
    }

    draw_wait(ctx: CanvasRenderingContext2D, text: string) {
        ctx.fillStyle = "#444"
        ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)
        ctx.fillStyle = "#555"
        ctx.font = "50px sans-serif"
        ctx.strokeStyle = "black"
        ctx.fillStyle = "white"
        ctx.lineWidth = 10
        ctx.textAlign = "center"
        ctx.textBaseline = "middle"
        ctx.lineJoin = "round"
        ctx.lineCap = "round"
        ctx.strokeText(text, ctx.canvas.width / 2, ctx.canvas.height / 2)
        ctx.fillText(text, ctx.canvas.width / 2, ctx.canvas.height / 2)
    }
}
