/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   menu.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: qmennen <qmennen@student.codam.nl>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/05/27 14:31:53 by qmennen           #+#    #+#             */
/*   Updated: 2025/06/03 20:16:52 by qmennen          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "game_menu.h"

t_menu	*menu_create(t_game_manager *manager, char *b_path,
					const t_menu_item *options[])
{
	t_menu	*menu;
	int		i;

	menu = malloc(sizeof(t_menu));
	if (!menu)
		return (NULL);
	ft_memset(menu, 0, sizeof(t_menu));
	if (b_path)
		menu->background = menu_load_background(
				manager->game->screen->mlx, b_path);
	if (b_path && mlx_image_to_window(
			manager->game->screen->mlx, menu->background, 0, 0) < 0)
	{
		mlx_delete_image(manager->game->screen->mlx, menu->background);
		return (free(menu), NULL);
	}
	if (menu->background)
		menu->background->instances[0].enabled = false;
	menu->hidden = 1;
	i = -1;
	while (options[++i])
		menu->items[i] = (t_menu_item *)options[i];
	menu->selector = mlx_put_string(manager->game->screen->mlx, ">", 0, 0);
	menu->selector->instances[0].enabled = false;
	menu->num_options = i;
	return (menu);
}

void	menu_display(t_menu *menu, t_screen *screen)
{
	int	i;
	int	half_w;
	int	half_h;

	half_w = screen->width / 2;
	half_h = screen->height / 2;
	if (menu->hidden)
		menu_toggle(menu, screen);
	menu->selector->instances[0].x = 150;
	menu->selector->instances[0].y = half_h + menu->selected_option * 50;
	i = 0;
	while (i < menu->num_options)
	{
		menu->items[i]->image->instances[0].x = 200;
		menu->items[i]->image->instances[0].y = half_h + i * 50;
		i++;
	}
}

void	menu_toggle(t_menu *menu, t_screen *screen)
{
	int	i;

	if (!menu)
		return ;
	menu->hidden = !menu->hidden;
	if (menu->selector)
		menu->selector->instances[0].enabled = !menu->hidden;
	if (menu->background)
		menu->background->instances[0].enabled = !menu->hidden;
	i = -1;
	while (++i < menu->num_options)
	{
		menu->items[i]->image->instances[0].enabled = !menu->hidden;
		mlx_set_instance_depth(menu->items[i]->image->instances, 7);
	}
}

void	menu_free(t_menu *menu, t_screen *screen)
{
	int	i;

	i = 0;
	if (!menu)
		return ;
	while (i < menu->num_options)
	{
		if (menu->items[i]->image)
			mlx_delete_image(screen->mlx, menu->items[i]->image);
		free(menu->items[i]);
		i++;
	}
	if (menu->selector)
		mlx_delete_image(screen->mlx, menu->selector);
	if (menu->background)
		mlx_delete_image(screen->mlx, menu->background);
	free(menu);
}
