/*
    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 { player_controller } from "../main.ts";
import { PacketC } from "../protocol.ts";
import { System } from "../system.ts";
import { MessageRenderer } from "./message.ts";
import { MapLayer } from "./mod.ts";
import { SPlayers } from "./players.ts";
import { SSprites } from "./sprites.ts";

export class SServerHints extends System implements MapLayer {
    hints: Map<string, MessageRenderer> = new Map()

    constructor(
        public ss: SSprites,
        public ps: SPlayers,
    ) { super() }

    override packet(p: PacketC): void {
        if (p.type == "server_hint") {
            if (p.player != player_controller.my_id) return;
            if (p.message) this.hints.set(p.position + "", new MessageRenderer(
                "hint",
                p.message,
                p.position ?
                    { x: p.position[0] + 0.5, y: p.position[1] + 0.5 } :
                    this.ps.players.get(player_controller.my_id)?.anim_position ?? { x: 0, y: 0 },
                0.,
            ))
            else this.hints.delete(p.position + "")
        }
    }

    override tick(dt: number): void {
        for (const [_, h] of this.hints)
            h.tick(dt)
    }

    draw_map_layer(ctx: CanvasRenderingContext2D): void {
        for (const [_, message] of this.hints)
            message.draw(ctx, this.ss, true)
    }
}
