/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parse_config_line.c                                :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: qmennen <qmennen@student.codam.nl>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/04/22 13:10:06 by whaffman          #+#    #+#             */
/*   Updated: 2025/06/04 17:57:29 by qmennen          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

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

t_token_handler	handle_config_token(const char *token, t_map *map)
{
	const char				*config_tokens[] = {
		"NO", "SO", "WE", "EA", "D", "F", "C", "FT", "CT", "-c", "-s", "-e", NULL
	};
	const t_token_handler	handlers[] = {
		handle_wall, handle_wall, handle_wall, handle_wall, handle_wall,
		handle_fc_color, handle_fc_color, handle_fc_texture, handle_fc_texture,
		handle_sprite,	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);
}
