/*
    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 { DebugEvent, PacketC } from "../protocol.ts";
import { System } from "../system.ts";
import { SMapItems } from "./item.ts";
import { MapLayer } from "./mod.ts";

export class SDebugEvents extends System implements MapLayer {
    events: Map<string, DebugEvent & { timeout: number }> = new Map()

    constructor(public is: SMapItems) { super() }

    override tick(dt: number): void {
        const remove_events: string[] = []
        for (const [key, ev] of this.events.entries()) {
            ev.timeout -= dt
            if (ev.timeout < 0) remove_events.push(key)
        }
        remove_events.forEach(i => this.events.delete(i))
    }

    override packet(p: PacketC): void {
        if (p.type == "debug")
            this.events.set(p.key, p)
    }

    draw_map_layer(ctx: CanvasRenderingContext2D): void {
        for (const ev of this.events.values()) {
            const color = `oklch(${ev.color.join(" ")})`
            if (ev.display.ty == "path") {
                ctx.lineWidth = 0.1
                ctx.lineCap = "round"
                ctx.strokeStyle = color
                ctx.beginPath()
                let first = true
                if (ev.display.player) {
                    const p = this.is.ps.players.get(ev.display.player)
                    if (p) ctx.moveTo(p.position.x, p.position.y)
                    first = false
                }
                for (let i = ev.display.points.length - 1; i >= 0; i--)
                    if (first) ctx.moveTo(...ev.display.points[i]), first = false
                    else ctx.lineTo(...ev.display.points[i])
                ctx.stroke()
            }
        }
        for (const ev of this.events.values()) {
            const color = `oklch(${ev.color.join(" ")})`
            if (ev.display.ty == "label") {
                ctx.font = "0.15px sans-serif"
                ctx.strokeStyle = "black"
                ctx.fillStyle = color
                ctx.lineWidth = 0.05
                ctx.textAlign = "center"
                ctx.textBaseline = "middle"
                ctx.lineJoin = "round"
                ctx.lineCap = "round"

                const pos = this.is.get_item_location_tracking(ev.display.pos)
                if (!pos) continue
                const lines = ev.display.text.split("\n")
                for (let i = 0; i < lines.length; i++)
                    ctx.strokeText(lines[i], pos.x, pos.y + i * 0.18)
                for (let i = 0; i < lines.length; i++)
                    ctx.fillText(lines[i], pos.x, pos.y + i * 0.18)

            }
        }
    }
}
