use glam::Vec3;

use crate::camera::Camera;
use crate::utility::input::InputState;
use crate::world::World;

pub fn update_noclip_camera(camera: &mut Camera, input_state: &InputState, delta: f32)
{
    camera.update_rotation(input_state.mouse_delta, 0.0008);

    let mut input_vec = Vec3::ZERO;

    if input_state.w
    {
        input_vec.z += 1.0;
    }
    if input_state.s
    {
        input_vec.z -= 1.0;
    }
    if input_state.d
    {
        input_vec.x += 1.0;
    }
    if input_state.a
    {
        input_vec.x -= 1.0;
    }
    if input_state.space
    {
        input_vec.y += 1.0;
    }

    if input_vec.length_squared() > 0.0
    {
        input_vec = input_vec.normalize();
    }

    let mut speed = 10.0 * delta;
    if input_state.shift
    {
        speed *= 2.0;
    }

    camera.update_noclip(input_vec, speed);
}

pub fn update_follow_camera(camera: &mut Camera, world: &World, input_state: &InputState)
{
    let player_entities = world.player_tags.all();

    if let Some(&player_entity) = player_entities.first()
    {
        if let Some(player_transform) = world.transforms.get(player_entity)
        {
            camera.update_follow(player_transform.position, input_state.mouse_delta, 0.0008);
        }
    }
}
