/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   parse_config_line.c                                :+:    :+:            */
/*                                                     +:+                    */
/*   By: whaffman <whaffman@student.codam.nl>         +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/04/22 13:10:06 by whaffman      #+#    #+#                 */
/*   Updated: 2025/05/23 14:56:02 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "cub3d.h"


mlx_texture_t	*load_texture(const char *path)
{
	mlx_texture_t	*texture;

	printf("Loading texture: |%s|\n", path);
	texture = mlx_load_png(path);
	if (texture == NULL)
	{
		ft_putstr_fd("Error: Failed to load texture\n", 2);
		return (NULL);
	}
	return (texture);
}

int	handle_wall(char *token, t_map *map)
{
	const char	*wall_tokens[] = {
		"NO", "SO", "WE", "EA", 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)
		{
			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 (!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 (!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	handle_sprite(char *token, t_map *map)
{
	return (SUCCESS);
}

t_token_handler	handle_config_token(const char *token, t_map *map)
{
	const char				*config_tokens[] = {
		"NO", "SO", "WE", "EA", "F", "C", "FT", "CT", "-c", "-s", NULL
	};
	const t_token_handler	handlers[] = {
		handle_wall, handle_wall, handle_wall, handle_wall,
		handle_fc_color, handle_fc_color, handle_fc_texture, handle_fc_texture,
		handle_sprite,	handle_sprite, NULL
	};
	int						i;

	i = 0;
	printf("Token: %s\n", token);
	while (config_tokens[i] != NULL)
	{
		if (ft_strcmp(token, config_tokens[i]) == 0)
			return (handlers[i]);
		i++;
	}
	return (NULL);
}


int	parse_config_line(char *line, t_map *map)
{
	char			*token;
	t_token_handler	handler;

	token = ft_strtok(line, " ");
	if (token == NULL)
		return (0);
	handler = handle_config_token(token, map);
	if (handler == NULL)
		return (ft_putstr_fd("Error: Invalid config token\n", 2), FAILURE);
	if (handler(token, map) == 0)
		return (ft_putstr_fd("Error: Failed to handle config token\n", 2),
			FAILURE);
	return (SUCCESS);
}
