/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   end_screen.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: qmennen <qmennen@student.codam.nl>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/06/03 16:20:35 by qmennen           #+#    #+#             */
/*   Updated: 2025/06/04 19:53:34 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 > 5 && 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;
	}
}

char	*get_score_text(char *prefix, char *suffix, int score)
{
	char	*score_text;
	char	*score_complete;
	char	*score_value;

	score_text = ft_itoa(score);
	if (!score_text)
		return (NULL);
	score_value = ft_strjoin(prefix, score_text);
	free(score_text);
	if (!score_value)
		return (NULL);
	if (!suffix)
		return (score_value);
	score_complete = ft_strjoin(score_value, suffix);
	free(score_value);
	return (score_complete);
}

static mlx_image_t	*draw_score_line(mlx_t *mlx, char *text, int x, int y)
{
	mlx_image_t	*image;

	if (!text)
		return (NULL);
	image = mlx_put_string(mlx, text, x, y);
	free(text);
	return (image);
}

void	draw_end_screen(t_game_manager *manager, t_menu *menu)
{
	char			*tiles_score;
	char			*battery_percentage;
	char			*collectibles_score;
	char			*enemies_score;
	t_game			*game;

	if (!manager || !manager->game)
		return ;
	game = manager->game;
	slideshow(game);
	if (menu->hidden)
	{
		menu_toggle(menu, game->screen);
		count_scores(game);
	}
	menu->selector->instances[0].y = manager->game->screen->height / 2
		+ menu->selected_option * 50;
	tiles_score = get_score_text("Discovered area: ", "%",
		(double) game->scoreboard->tiles_visited / game->scoreboard->total_tiles * 100);
	if (! game->scoreboard->tiles_text)
		game->scoreboard->tiles_text = draw_score_line(game->screen->mlx,
			tiles_score, 100, game->screen->height / 2 - 50);
	battery_percentage = get_score_text("Battery percentage: ",
		"%", (double) game->player->battery * 100);
	game->scoreboard->battery_text = draw_score_line(game->screen->mlx,
		battery_percentage, 100, game->screen->height / 2);
	collectibles_score = get_score_text("Batteries found: ", NULL,
		game->scoreboard->collectibles);
	game->scoreboard->collectibles_text = draw_score_line(game->screen->mlx,
		collectibles_score, 100, game->screen->height / 2 + 50);
	enemies_score = get_score_text("Enemies defeated: ", NULL,
		game->scoreboard->enemies);
	game->scoreboard->enemies_text = draw_score_line(game->screen->mlx,
		enemies_score, 100, game->screen->height / 2 + 100);
}

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);
}
