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

export class SScoreDisplay extends System {
    score: Score = { active_recipes: 0, demands_completed: 0, demands_failed: 0, instant_recipes: 0, passive_recipes: 0, players: 0, points: 0, stars: 0, time_remaining: 0 }
    override packet(p: PacketC): void {
        if (p.type != "score") return
        this.score.demands_completed = p.demands_completed
        this.score.demands_failed = p.demands_failed
        this.score.points = p.points
        this.score.time_remaining = p.time_remaining ?? null
    }
    override tick(dt: number): void {
        this.score.time_remaining -= dt
        this.score.time_remaining = Math.max(this.score.time_remaining, 0)
    }
    override draw(ctx: CanvasRenderingContext2D): void {
        ctx.fillStyle = "white"
        ctx.textAlign = "left"
        ctx.textBaseline = "bottom"
        ctx.font = "20px sans-serif"
        ctx.fillText(`${tr("c.score.time_remaining")}: ${this.score.time_remaining?.toFixed(2)}`, 10, ctx.canvas.height - 90)
        ctx.font = "30px sans-serif"
        ctx.fillText(`${tr("c.score.points")}: ${this.score.points}`, 10, ctx.canvas.height - 60)
        ctx.font = "20px sans-serif"
        ctx.fillText(`${tr("c.score.demands_completed")}: ${this.score.demands_completed}`, 10, ctx.canvas.height - 30)
        ctx.fillText(`${tr("c.score.demands_failed")}: ${this.score.demands_failed}`, 10, ctx.canvas.height - 10)
    }
}
