/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   initialize.c                                       :+:    :+:            */
/*                                                     +:+                    */
/*   By: qmennen <qmennen@student.codam.nl>           +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/04/22 17:08:26 by qmennen       #+#    #+#                 */
/*   Updated: 2025/05/23 15:05:50 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "cub3d.h"

static t_sprite	make_sprite(char *path, double x, double y, int collectible)
{
	t_sprite	sprite;

	ft_memset(&sprite, 0, sizeof(t_sprite));
	sprite.texture = mlx_load_png(path);
	if (!sprite.texture)
	{
		fprintf(stderr, "Error loading texture: %s\n", path);
		exit(EXIT_FAILURE);
	}
	if (sprite.texture->width  % 64 != 0)
	{
		fprintf(stderr, "Error: Texture %s width is not a multiple of 64\n", path);
		exit(EXIT_FAILURE);
	}
	if (sprite.texture->height != 64)
	{
		fprintf(stderr, "Error: Texture %s height is not 64\n", path);
		exit(EXIT_FAILURE);
	}
	sprite.n_frames = sprite.texture->width / 64;
	sprite.pos.x = x;
	sprite.pos.y = y;
	sprite.visible = 1;
	sprite.collectible = collectible;
	return (sprite);
}

static int	init_temp(t_game **game)
{
	t_sprite	*sprites;

	(*game)->map->sprites = malloc(sizeof(t_sprite) * 10);
	if (!(*game)->map->sprites)
		return (FAILURE);
	sprites = (*game)->map->sprites;
	sprites[0] = make_sprite("./assets/battery.png", 3.5, 3.5, 1);
	sprites[1] = make_sprite("./assets/plant.png", 2.5, 3.5, 0);
	sprites[2] = make_sprite("./assets/plant.png", 1.5, 3.5, 0);
	sprites[3] = make_sprite("./assets/broken_mirror.png", 3.5, 4.5, 0);
	sprites[4] = make_sprite("./assets/lamp.png", 2.5, 5.5, 0);
	sprites[5] = make_sprite("./assets/battery.png", 10.5, 6.5, 1);
	sprites[6] = make_sprite("./assets/plant.png", 14.5, 3.5, 0);
	sprites[7] = make_sprite("./assets/battery.png", 2.5, 7.5, 1);
	sprites[8] = make_sprite("./assets/broken_mirror.png", 42.5, 4.5, 0);
	sprites[9] = make_sprite("./assets/lamp.png", 9.5, 10.5, 0);
	(*game)->map->n_sprites = 10;
	(*game)->screen->hud = mlx_texture_to_image((*game)->screen->mlx,
			mlx_load_png("./assets/overlay2.png"));
	return (SUCCESS);
}

int	initialize_cub3d(t_game **game, const char *mapfile)
{
	if (!game_create(game))
		return (FAILURE);
	if (!player_create(game))
		return (FAILURE);
	if (!map_create(game, mapfile))
		return (FAILURE);
	if (!texture_load(*game))
		return (FAILURE);
	if (!screen_create(game))
		return (FAILURE);
	if (!keyboard_create(game))
		return (FAILURE);
	if (!init_temp(game))
		return (FAILURE);
	if (!screen_display((*game)->screen))
		return (FAILURE);
	mlx_key_hook((*game)->screen->mlx, keyhandle, *game);
	mlx_loop_hook((*game)->screen->mlx, game_loop, *game);
	return (SUCCESS);
}
