/*
    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 { data } from "./main.ts";
import { SMapTiles } from "./map/tiles.ts";
import { V2, normalize, length, sub_v2, lerp_exp_v2_mut } from "./util.ts";

export const PLAYER_SIZE = 0.4
export const PLAYER_ACCELERATION = 10
export const PLAYER_SPEED = 3.8
export const BOOST_FACTOR = 2.5
export const BOOST_DURATION = 0.3
export const BOOST_RESTORE = 0.5

export interface MovementBase {
    input_direction: V2,
    input_boost: boolean,
    position: V2,
    facing: V2,
    rotation: number,
    velocity: V2,
    boosting: boolean,
    stamina: number
}

export function update_movement(ts: SMapTiles, p: MovementBase, dt: number) {
    const direction = p.input_direction;
    let boost = p.input_boost;
    if (length(direction) > 0.05) lerp_exp_v2_mut(p.facing, direction, dt * 10.)
    if (length(direction) < 0.5) direction.x = direction.y = 0
    p.rotation = Math.atan2(p.facing.x, p.facing.y)
    boost &&= length(direction) > 0.1
    p.boosting = boost && (p.boosting || p.stamina >= 1) && p.stamina > 0
    if (p.boosting) p.stamina -= dt / BOOST_DURATION
    else p.stamina += dt / BOOST_RESTORE
    p.stamina = Math.max(Math.min(p.stamina, 1), 0)
    const speed = PLAYER_SPEED * (p.boosting ? BOOST_FACTOR : 1)
    lerp_exp_v2_mut(p.velocity, { x: direction.x * speed, y: direction.y * speed }, dt * PLAYER_ACCELERATION)
    p.position.x += p.velocity.x * dt
    p.position.y += p.velocity.y * dt
    collide_player(ts, p, dt)
}
function collide_player(ts: SMapTiles, p: MovementBase, _dt: number) {
    for (let xo = -1; xo <= 1; xo++) {
        for (let yo = -1; yo <= 1; yo++) {
            const x = Math.floor(p.position.x) + xo
            const y = Math.floor(p.position.y) + yo

            const tile = ts.tiles.get([x, y].toString())
            if (!tile) continue
            let has_coll = false
            for (const p of tile.parts) {
                has_coll ||= data.tile_collide.includes(p) //? eff
            }
            if (!has_coll) continue

            const d = aabb_point_distance(x, y, x + 1, y + 1, p.position.x, p.position.y)
            if (d > PLAYER_SIZE) continue

            const h = 0.01
            const d_sample_x = aabb_point_distance(x, y, x + 1, y + 1, p.position.x + h, p.position.y)
            const d_sample_y = aabb_point_distance(x, y, x + 1, y + 1, p.position.x, p.position.y + h)
            const grad_x = (d_sample_x - d) / h
            const grad_y = (d_sample_y - d) / h

            p.position.x += (PLAYER_SIZE - d) * grad_x
            p.position.y += (PLAYER_SIZE - d) * grad_y

            const vdotn = (grad_x * p.velocity.x) + (grad_y * p.velocity.y)
            p.velocity.x -= grad_x * vdotn
            p.velocity.y -= grad_y * vdotn
        }
    }
}
export function collide_player_player(a: MovementBase, b: MovementBase, dt: number) {
    const diff = sub_v2(a.position, b.position)
    const d = length(diff)
    if (d < 0.01) return
    if (d >= PLAYER_SIZE * 2) return
    const norm = normalize(diff);
    const f = 100 / (1 + d)
    a.velocity.x += norm.x * f * dt
    a.velocity.y += norm.y * f * dt
}

export function aabb_point_distance(
    min_x: number,
    min_y: number,
    max_x: number,
    max_y: number,
    px: number,
    py: number
): number {
    const dx = px - Math.max(min_x, Math.min(max_x, px))
    const dy = py - Math.max(min_y, Math.min(max_y, py))
    return Math.sqrt(dx * dx + dy * dy)
}
