/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   game.c                                             :+:    :+:            */
/*                                                     +:+                    */
/*   By: qmennen <qmennen@student.codam.nl>           +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/04/15 15:46:08 by qmennen       #+#    #+#                 */
/*   Updated: 2025/06/04 16:43:14 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "game.h"

int	game_create(t_game **game)
{
	*game = malloc(sizeof(t_game));
	if (!game)
		return (FAILURE);
	ft_memset(*game, 0, sizeof(t_game));
	(*game)->fps = 20;
	(*game)->scoreboard = malloc(sizeof(t_scoreboard));
	if (!(*game)->scoreboard)
	{
		return (free(*game), FAILURE);
	}
		return (SUCCESS);
}

void	game_over(t_game_manager *manager)
{
	t_game	*game;

	game = manager->game;
	game->screen->hud->instances[0].enabled = false;
	game->screen->minimap->instances[0].enabled = false;
	game->screen->img->instances[0].enabled = false;
	manager->state = GAME_STATE_END_SCREEN;
	manager->active_menu = &manager->end_screen;
	set_uniforms(game);
}

void	game_run(t_game *game)
{
	static int	fps = 0;

	fps += (int)(1.f / game->screen->mlx->delta_time);
	set_uniforms(game);
	player_update(game, game->screen->mlx->delta_time);
	cast_rays(game);
	update_monsters(game);
	render_map(game);
	if (game->player->is_moving)
	{
		game->screen->img->instances[0].x = sin(mlx_get_time() * 10) * 20;
		game->screen->img->instances[0].y = cos(mlx_get_time() * 18) * 10;
	}
	handle_battery(game);
	handle_record(game);
	handle_mouse(game);
	collision_sprite(game, 0, 0);
	if (game->player->battery <= 0 || count_tiles(game->map, TILE_EMPTY) == 0)
		game_over(game->manager);

}

void	game_free(t_game *game)
{
	if (game->screen)
	{
		if (game->screen->hud)
		{
			mlx_delete_image(game->screen->mlx, game->screen->hud);
			game->screen->hud = NULL;
		}
		mlx_delete_image(game->screen->mlx, game->screen->img);
	}
	if (game->player)
		free(game->player);
	if (game->map)
		map_free(game->map);
	if (game->keyboard)
		free(game->keyboard);
	if (game->scoreboard)
	{
		if (game->scoreboard->tiles_text)
			mlx_delete_image(game->screen->mlx, game->scoreboard->tiles_text);
		if (game->scoreboard->collectibles_text)
			mlx_delete_image(game->screen->mlx, game->scoreboard->collectibles_text);
		if (game->scoreboard->battery_text)
			mlx_delete_image(game->screen->mlx, game->scoreboard->battery_text);
		if (game->scoreboard->enemies_text)
			mlx_delete_image(game->screen->mlx, game->scoreboard->enemies_text);
		free(game->scoreboard);
	}
}

void	game_terminate(t_game *game)
{
	print_scores(game);
	game_free(game);
	exit(EXIT_SUCCESS);
}
