/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   key_press.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: bszilas <bszilas@student.42vienna.com>     +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/12/05 16:30:08 by vvobis            #+#    #+#             */
/*   Updated: 2025/05/31 15:08:27 by victor           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../../minirt.h"

uint    key_misc_function(int keycode, t_scene *scene, t_data *data)
{
    if (keycode == KEY_P)
        scene_save(scene);
    else if (keycode == KEY_X)
        return (scene->anti_aliasing = scene->anti_aliasing == 0 ? ANTI_ALIASING_FACTOR : 0, true);
    else if (keycode == KEY_M)
        return (data->current_menu = &data->menu[MENU_MAIN], true);
    else if (keycode == KEY_Y)
        return (data->func_ptr = help_menu_draw, true);
    else if (keycode == KEY_DELETE)
    {
        if (data->mouse.grabbed)
            ((t_body *)data->mouse.grabbed)->type = BODY_DELETED;
        data->mouse.grabbed = NULL;
        return (true);
    }
    else if (keycode == KEY_B)
    {
        scene->sky_sphere = !scene->sky_sphere;
        swap_2_ints(&scene->sky_color, &scene->ambient.color);
    }
    return (false);
}

int    key_move_camera(int keycode, t_camera *camera)
{
    t_vector    translation;

    calc_camera_space(camera);
    if (keycode == KEY_A || keycode == KEY_D)
        translation = scale_vector(camera->right, \
                (keycode - KEY_A - 1.5) * 0.1);
    else if (keycode == KEY_W || keycode == KEY_S)
        translation = scale_vector(camera->normal, (keycode - KEY_S - 2) * 0.1);
    else if (keycode == KEY_Q || keycode == KEY_E)
        translation = scale_vector(camera->up, (keycode - KEY_E - 6) * 0.1);
    else
        return (false);
    camera->position = add_vector(camera->position, translation);
    calc_camera_space(camera);
    return (true);
}

void    key_press_distribute(int keycode, t_data *data, t_scene *scene)
{
    if (move_body(keycode, scene->body_focus) || \
    key_move_camera(keycode, &scene->camera) || \
    calc_camera_rotation(keycode, &scene->camera) || \
    key_misc_function(keycode, scene, data) || \
    key_change_res(keycode, scene) || \
    key_move_light(keycode, scene) || \
    key_change_fov(keycode, &scene->camera))
        return ;
}

int    key_press(int keycode, void *data_ptr)
{
    t_scene    *scene;
    t_data    *data;

    data = data_ptr;
    scene = &data->scene;
    if (keycode == KEY_ESCAPE)
    {
        lst_memory(NULL, NULL, END);
        exit(0);
    }
    key_press_distribute(keycode, data, scene);
    return (1);
}
