/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parse_handlers.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: qmennen <qmennen@student.codam.nl>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/06/03 14:38:12 by whaffman          #+#    #+#             */
/*   Updated: 2025/06/04 17:57:18 by qmennen          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "cub3d.h"

int	handle_wall(char *token, t_map *map)
{
	const char	*wall_tokens[] = {
		"NO", "SO", "WE", "EA", "D", NULL
	};
	char		*texture_path;
	int			i;

	i = 0;
	texture_path = ft_strtok(NULL, " ");
	if (texture_path == NULL)
		return (ft_putstr_fd("Error: Missing texture path\n", 2), FAILURE);
	if (ft_strtok(NULL, " ") != NULL)
		return (ft_putstr_fd("Error: Extra tokens after path\n", 2), FAILURE);
	while (wall_tokens[i])
	{
		if (ft_strcmp(token, wall_tokens[i]) == 0)
		{
			if (map->textures[i] != NULL)
				return (FAILURE);
			map->textures[i] = load_texture(texture_path);
			if (map->textures[i] == NULL)
				return (FAILURE);
		}
		i++;
	}
	return (SUCCESS);
}

int	handle_fc_color(char *token, t_map *map)
{
	char	*color_str;
	int		color;

	color_str = ft_strtok(NULL, " ");
	if ((ft_strcmp(token, "F") == 0 && map->floor_color != 0x0)
		|| (ft_strcmp(token, "C") == 0 && map->ceiling_color != 0x0))
		return (ft_putstr_fd("Error: Color already set \n", 2), FAILURE);
	if (!color_str)
		return (ft_putstr_fd("Error: Missing color value\n", 2), FAILURE);
	if (ft_strtok(NULL, " "))
		return (ft_putstr_fd("Error: Extra tokens after color value\n", 2),
			FAILURE);
	color = parse_color(color_str);
	if (!color)
		return (ft_putstr_fd("Error: Invalid color value\n", 2), FAILURE);
	if (ft_strcmp(token, "F") == 0)
		map->floor_color = color;
	else if (ft_strcmp(token, "C") == 0)
		map->ceiling_color = color;
	return (SUCCESS);
}

int	handle_fc_texture(char *token, t_map *map)
{
	char	*texture_path;

	texture_path = ft_strtok(NULL, " ");
	if ((ft_strcmp(token, "FT") == 0 && map->texture_floor != 0x0)
		|| (ft_strcmp(token, "CT") == 0 && map->texture_ceiling != 0x0))
		return (ft_putstr_fd("Error: Texture already set\n", 2), FAILURE);
	if (!texture_path)
		return (ft_putstr_fd("Error: Missing texture path\n", 2), FAILURE);
	if (ft_strtok(NULL, " "))
		return (ft_putstr_fd("Error: Extra tokens after path\n", 2), FAILURE);
	if (ft_strcmp(token, "FT") == 0)
	{
		map->texture_floor = load_texture(texture_path);
		if (map->texture_floor == NULL)
			return (FAILURE);
	}
	else if (ft_strcmp(token, "CT") == 0)
	{
		map->texture_ceiling = load_texture(texture_path);
		if (map->texture_ceiling == NULL)
			return (FAILURE);
	}
	return (SUCCESS);
}

int	set_sprite_type(char *token, t_sprite_lib *sprite)
{
	if (token[1] == 'c')
	{
		sprite->collectible = 1;
		sprite->type = SPRITE_TYPE_COLLECTIBLE;
	}
	else if (token[1] == 'e')
	{
		sprite->collectible = 0;
		sprite->type = SPRITE_TYPE_ENEMY;
	}
	else
	{
		sprite->collectible = 0;
		sprite->type = SPRITE_TYPE_DEFAULT;
	}
	return (SUCCESS);
}

int	handle_sprite(char *token, t_map *map)
{
	t_sprite_lib	sprite;
	char			*symbol;
	char			*texture_path;

	if (!set_sprite_type(token, &sprite))
		return (ft_putstr_fd("Error: Invalid sprite type\n", 2), FAILURE);
	symbol = ft_strtok(NULL, " ");
	if (*symbol < 'a' || *symbol > 'z' || ft_strlen(symbol) != 1)
		return (ft_putstr_fd("Error: Invalid sprite symbol\n", 2), FAILURE);
	texture_path = ft_strtok(NULL, " ");
	if (texture_path == NULL)
		return (ft_putstr_fd("Error: Missing texture path\n", 2), FAILURE);
	if (ft_strtok(NULL, " ") != NULL)
		return (ft_putstr_fd("Error: Extra tokens after path\n", 2), FAILURE);
	sprite.texture = load_texture(texture_path);
	if (sprite.texture == NULL)
		return (FAILURE);
	if (map->sprite_lib[*symbol - 'a'].texture != NULL)
		return (ft_putstr_fd(
				"Error: Sprite already defined for this symbol\n", 2), FAILURE);
	map->sprite_lib[*symbol - 'a'] = sprite;
	return (SUCCESS);
}
