/*
    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 { MessageRenderer } from "./map/message.ts";
import { PacketC } from "./protocol.ts";
import { System } from "./system.ts";

export class SServerMessage extends System {
    inner?: MessageRenderer
    override packet(p: PacketC): void {
        if (p.type == "server_message") {
            this.inner = new MessageRenderer(
                p.error ? "error" : "normal",
                p.message,
                { x: 0, y: 0 },
                0.,
                { initial: 5, remaining: 5, pinned: false }
            )
        } else if (p.type == "menu" && p.menu == "score") {
            this.inner = new MessageRenderer(
                "normal",
                { text: `Score: ${JSON.stringify(p.data, null, 4)}` },
                { x: 0, y: 0 },
                0,
                { initial: 5, remaining: 5, pinned: false },
            )
        }
    }

    override tick(dt: number): void {
        if (this.inner && this.inner.tick(dt)) delete this.inner
    }

    override draw(ctx: CanvasRenderingContext2D): void {
        if (!this.inner) return
        ctx.save()
        ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 6)
        ctx.scale(2, 2)
        this.inner.draw(ctx, undefined, false)
        ctx.restore()
    }
}
