/*
    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 { canvas, keys_down } from "../main.ts";
import { SPlayerController } from "../player_controller.ts";
import { System } from "../system.ts";
import { lerp_exp, lerp_exp_v2_mut, V2 } from "../util.ts";

const KEY_ZOOM = "KeyI"

export class SCamera extends System {
    center: V2 = { x: 0, y: 0 }
    scale = 0.05;
    overlay_vis_anim: number = 0

    constructor(public pcs: SPlayerController) { super() }

    override tick(dt: number): void {
        const p = this.pcs.ps.players.get(this.pcs.my_id!)
        if (!p) return
        lerp_exp_v2_mut(this.center, p.position, dt * 10.)
        const zoom_target = Math.min(canvas.width, canvas.height) * (keys_down.has(KEY_ZOOM) ? 0.05 : 0.1)
        this.scale = lerp_exp(this.scale, zoom_target, dt * 5)
        this.overlay_vis_anim = lerp_exp(this.overlay_vis_anim, +keys_down.has(KEY_ZOOM), dt * 10)
    }

    apply_map_transform(ctx: CanvasRenderingContext2D) {
        ctx.translate(canvas.width / 2, canvas.height / 2)
        ctx.scale(this.scale, this.scale)
        ctx.translate(-this.center.x, -this.center.y)
    }

    screen_to_world(screen: V2): V2 {
        return {
            x: ((screen.x - canvas.width / 2) / this.scale) + this.center.x,
            y: ((screen.y - canvas.height / 2) / this.scale) + this.center.y,
        }
    }
}
