/* ************************************************************************** */
/*                                                             */
/*                                             :::     ::::::::   */
/*   mouse_press.c                                      :+:      :+:    :+:   */
/*                                          +:+ +:+        +:+    */
/*   By: vvobis <marvin@42.fr>                  +#+  +:+      +#+       */
/*                                       +#+#+#+#+#+   +#+         */
/*   Created: 2024/11/25 13:36:13 by vvobis          #+#    #+#           */
/*   Updated: 2025/05/31 15:08:57 by victor           ###   ########.fr       */
/*                                                             */
/* ************************************************************************** */

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

void    mouse_click_left(int x, int y, t_scene *scene, t_mouse *mouse)
{
    uint    id_group;
    t_body    *body;

    mouse->prev_x = x;
    mouse->prev_y = y;
    mouse->left_is_pressed = true;
    mouse->data->scene.body_focus = NULL;
    mouse->data->should_rerender = true;
    id_group = id_group_get(scene->pixel[y * data.scene.w + x].id);
    mouse->data->func_ptr = NULL;
    if (id_group == ID_GROUP_ITEM && mouse->data->current_menu)
    {
        container_item_get_by_id(mouse->data->current_menu, \
                scene->pixel[y * data.scene.w + x].id, x, y);
    }
    else if (id_group > 0 && id_group < ID_GROUP_ITEM)
    {
        body = body_get_by_id(scene->pixel[y * data.scene.w + x].id, scene);
        scene->body_focus = NULL;
        mouse_grab(mouse, body);
    } else {
        mouse->data->current_menu = NULL;
    }
}

void    mouse_click_right(int x, int y, t_data *data, t_mouse *mouse)
{
    uint    id_group;
    t_body    *body;

    id_group = id_group_get(data->pixel[y * data->scene.w + x].id);
    data->scene.body_focus = NULL;
    mouse->right_is_pressed = true;
    data->should_rerender = true;
    if (id_group >= ID_GROUP_SPHERE && id_group <= ID_GROUP_CONE)
    {
        body = body_get_by_id(data->pixel[y * data->scene.w + x].id, &data->scene);
        data->scene.body_focus = body;
        if (id_group == ID_GROUP_PLANE)
            plane_menu_map(&data->menu[ID_GROUP_PLANE], body, &body->color);
        else if (id_group == ID_GROUP_SPHERE)
            sphere_menu_map(&data->menu[ID_GROUP_SPHERE], body, &body->color);
        else if (id_group == ID_GROUP_CYLINDER)
            cylinder_menu_map(&data->menu[ID_GROUP_CYLINDER], \
                body, &body->color);
        else if (id_group == ID_GROUP_CONE)
            cone_menu_map(&data->menu[ID_GROUP_CONE], body, &body->color);
        else if (id_group == ID_GROUP_DISC)
            disk_menu_map(&data->menu[ID_GROUP_DISC], body, &body->color);
    }
    else
        mouse->grabbed = NULL;
    data->current_menu = &data->menu[id_group];
}

void    menu_scroll_items(t_container *param, uint scaler)
{
    uint    i;

    i = 0;
    while (i < param->item_count)
    {
        param->item[i].rect.y += scaler;
        i++;
    }
}

int    mouse_press(uint x, uint y, t_data *data)
{
    uint    id_group;

    if (x > INT_MAX || x >= data->scene.w || y > INT_MAX || y >= data->scene.h)
        return (0);
    id_group = id_group_get(data->scene.pixel[y * data->scene.w + x].id);
    if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !data->mouse.left_is_pressed)
        mouse_click_left(x, y, &data->scene, &data->mouse);
    else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) && !data->mouse.right_is_pressed)
        mouse_click_right(x, y, data, &data->mouse);

    float mouse_wheel = GetMouseWheelMove();

    if (mouse_wheel > 0)
    {
        data->should_rerender = true;
        if (id_group == ID_GROUP_MENU_MAIN || id_group == ID_GROUP_ITEM)
            menu_scroll_items(data->current_menu, SCROLL_UP_DISTANCE);
        else
            key_change_fov(KEY_SLASH, &data->scene.camera);
    }
    else if (mouse_wheel < 0)
    {
        data->should_rerender = true;
        if (id_group == ID_GROUP_MENU_MAIN || id_group == ID_GROUP_ITEM)
            menu_scroll_items(data->current_menu, SCROLL_DOWN_DISTANCE);
        else
            key_change_fov(KEY_PERIOD, &data->scene.camera);
    }
    return (0);
}

int    mouse_release(uint x, uint y, t_data *data)
{
    if (data->mouse.left_is_pressed && !IsMouseButtonDown(MOUSE_BUTTON_LEFT))
    {
        data->mouse.left_is_pressed = false;
        data->scene.resolution_x = max_res_x, data->scene.resolution_y = max_res_y;
        if (data->mouse.grabbed)
        {
            (void)x;
            (void)y;
            ((t_body *)data->mouse.grabbed)->color = data->mouse.color_store;
        }
        data->mouse.grabbed = NULL;
        data->mouse.slider = NULL;
        data->should_rerender = true;
    }
    if (data->mouse.right_is_pressed && !IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
    {
        data->mouse.right_is_pressed = false;
        data->should_rerender = true;
    }
    return (0);
}
