//
// Created by slinky on 5/11/26.
//

#include "FreeCameraController.h"
#include "InputManager.h"

void FreeCameraController::update(EditorContext &ctx) {
    checkInput = ctx.sceneViewerFocused;

    if (!checkInput) return;

    if (InputManager::check_action_performed("move_forward")) {
        const glm::vec3 translation = camera->front() * movement_speed;
        camera->translate(translation);
    }

    if (InputManager::check_action_performed("move_backward")) {
        const glm::vec3 translation = camera->front() * -movement_speed;
        camera->translate(translation);
    }

    if (InputManager::check_action_performed("move_left")) {
        const glm::vec3 translation = camera->right() * -movement_speed;
        camera->translate(translation);
    }

    if (InputManager::check_action_performed("move_right")) {
        const glm::vec3 translation = camera->right() * movement_speed;
        camera->translate(translation);
    }

    if (InputManager::check_action_performed("move_up")) {
        const glm::vec3 translation = camera->up() * movement_speed;
        camera->translate(translation);
    }

    if (InputManager::check_action_performed("move_down")) {
        const glm::vec3 translation = camera->up() * -movement_speed;
        camera->translate(translation);
    }
}

void FreeCameraController::on_mouse_delta(float xoff, float yoff) const {
    if (!checkInput) return;

    camera->rotate_pitch(yoff * look_sensitivity);
    camera->rotate_yaw(xoff * look_sensitivity);
}

void FreeCameraController::set_camera(Camera& c) {
    camera = &c;
}

void FreeCameraController::release_camera() {
    camera = nullptr;
}
