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

export class SAnnouncement extends System {
    timer = 0
    active = false

    override packet(p: PacketC): void {
        if (p.type == "menu" && p.menu == "announce_start") {
            this.active = true
            this.timer = 3.5
        }
    }

    override tick(dt: number): void {
        if (this.active) {
            this.timer -= dt
            if (this.timer < -3)
                this.active = false
        }
    }

    override draw(ctx: CanvasRenderingContext2D): void {
        if (!this.active) return
        ctx.save()

        ctx.fillStyle = "#ffffff"
        ctx.strokeStyle = "#903544"
        ctx.font = "80px sans-serif"
        ctx.fillStyle = "white"
        ctx.lineWidth = 10
        ctx.textAlign = "center"
        ctx.textBaseline = "middle"
        ctx.lineJoin = "round"
        ctx.lineCap = "round"
        ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2)

        for (const [text, t] of [["Ready?", 3.5], ["Go!", 1]] as const) {
            const rt = t - this.timer;
            if (rt < 0 || rt > 2) continue

            const scale = rt < 0.5
                ? 1 - Math.pow(1 - rt / 0.5, 3)
                : 1 + Math.pow((rt - 0.5) / 1.5, 4)
            const alpha = rt < 0.5
                ? 1 - Math.pow(1 - rt / 0.5, 4)
                : 1 - (rt - 0.5) / 1.5

            ctx.save()
            ctx.globalAlpha = alpha
            ctx.scale(scale, scale)
            ctx.strokeText(text, 0, 0)
            ctx.fillText(text, 0, 0)
            ctx.restore()
        }

        ctx.restore()
    }
}
