import { BC_CAMERA, BC_VIEWPORT } from "../GlobalVariables/GlobalVariables";
import { UICameraInfo } from "../UIPipes/UIPipes";
import { interpolate, interpolateCos } from "../Utils/Math.utils";

const cameraSpeed = 500;

export function moveVertically(tick, keyCode) {
    BC_CAMERA.position.y += (tick.deltaMS / 1000) * cameraSpeed * (keyCode === "KeyS" ? 1 : -1);
    UICameraInfo.update((s) => {
        s.position.y = BC_CAMERA.position.y;
    });
}

export function moveHorizontally(tick, keyCode) {
    BC_CAMERA.position.x += (tick.deltaMS / 1000) * cameraSpeed * (keyCode === "KeyD" ? 1 : -1);
    UICameraInfo.update((s) => {
        s.position.x = BC_CAMERA.position.x;
    });
}

export function calculateViewportFromCamera(tick) {
    // BC_VIEWPORT.pivot.set(0.5, 0.5);
    BC_CAMERA.zoom = interpolate(BC_CAMERA.zoom, BC_CAMERA.targetZoom, (tick.deltaMS / 1000) * 45);
    BC_VIEWPORT.position.set(
        Math.floor(((window.innerWidth / 2.0 / BC_CAMERA.zoom) - BC_CAMERA.position.x) * BC_CAMERA.zoom),
        Math.floor(((window.innerHeight / 2.0 / BC_CAMERA.zoom) - BC_CAMERA.position.y) * BC_CAMERA.zoom)
    );
    BC_VIEWPORT.scale.set(BC_CAMERA.zoom, BC_CAMERA.zoom);
}

/**
 * 
 * @param {Number} x 
 * @param {Number} y 
 */
export function screenToWorldCoordinates(x, y) {
    return {
        x: (((x - window.innerWidth / 2.0) / BC_CAMERA.zoom) + BC_CAMERA.position.x),
        y: (((y - window.innerHeight / 2.0) / BC_CAMERA.zoom) + BC_CAMERA.position.y)
    }
}