/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   end_screen.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: qmennen <qmennen@student.codam.nl>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/06/03 16:20:35 by qmennen           #+#    #+#             */
/*   Updated: 2025/06/11 17:44:51 by qmennen          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "game_menu.h"

static void	slideshow(t_game *game)
{
	static int		frame_idx = 0;
	static int		last_frame_time;

	if (game->screenshots[frame_idx])
		game->screenshots[frame_idx]->instances[0].enabled = true;
	if (last_frame_time == 0)
		last_frame_time = mlx_get_time();
	if (mlx_get_time() - last_frame_time > SLIDESHOW_DURATION
		&& frame_idx < game->screenshot_idx
		&& frame_idx < MAX_SCREENSHOTS)
	{
		game->screenshots[frame_idx++]->instances[0].enabled = true;
		game->screenshots[frame_idx - 1]->instances[0].enabled = false;
		last_frame_time = mlx_get_time();
		frame_idx %= (MAX_SCREENSHOTS < game->screenshot_idx) * MAX_SCREENSHOTS
			+ (MAX_SCREENSHOTS >= game->screenshot_idx) * game->screenshot_idx;
	}
}

static void	draw_scoreboard(t_game_manager *manager)
{
	const int	s_val[] = {
		(int)(manager->game->scoreboard->tiles_visited
			/ (double)manager->game->scoreboard->total_tiles * 100),
		(int)(manager->game->player->battery * 100),
		manager->game->scoreboard->collectibles,
		manager->game->scoreboard->enemies,
		manager->game->scoreboard->end_time
		- manager->game->scoreboard->start_time
	};
	t_game		*game;
	int			i;
	char		*s_text;

	i = -1;
	game = manager->game;
	while (++i < 5)
	{
		s_text = get_score_text(get_score_prefix(i), get_score_suffix(i),
				s_val[i]);
		if (s_text)
			draw_score_line(game->screen->mlx, &game->scoreboard->texts[i],
				s_text, (t_vec2_int){350,
				game->screen->height / 2 - 100 + i * 30});
	}
}

void	draw_end_screen(t_game_manager *manager, t_menu *menu)
{
	t_game			*game;

	if (!manager || !manager->game)
		return ;
	game = manager->game;
	slideshow(game);
	if (menu->hidden)
	{
		menu_toggle(menu);
		count_scores(game);
	}
	menu->selector->instances[0].enabled = false;
	draw_scoreboard(manager);
}

static void	game_exit(struct s_menu_item *item, t_game_manager *manager)
{
	(void)item;
	game_manager_destroy(manager);
}

t_menu	*create_end_screen(t_game_manager *manager)
{
	const t_menu_item	*menu_items[] = {
		menu_item_create(manager->game->screen, "Room discovery: ", game_exit),
		NULL
	};
	t_menu				*menu;

	menu = menu_create(manager, "assets/menu/end_screen.png", menu_items);
	menu->items[0]->image->instances[0].x = 100;
	menu->items[0]->image->instances[0].y = manager->game->screen->height / 2;
	if (!menu)
		return (NULL);
	return (menu);
}
