/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   setters.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: tchampio <tchampio@student.42lehavre.fr>   +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/06/21 19:35:43 by tchampio          #+#    #+#             */
/*   Updated: 2025/06/21 19:39:32 by tchampio         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../../includes/maputils.h"
#include "../../includes/libft.h"

unsigned long	set_color(const char *s, t_mapdata *map)
{
	int				i;
	unsigned long	finalcolor;
	char			**tab;
	int				rgb[100];

	i = 0;
	tab = ft_split(s, ',');
	while (tab[i])
	{
		if (i > 2)
			ft_strlcpy(map->error, "too many colors", 16);
		rgb[i] = ft_atoi(tab[i]);
		free(tab[i]);
		i++;
	}
	free(tab);
	if (rgb[0] < 0 || rgb[0] > 255
		|| rgb[1] < 0 || rgb[1] > 255
		|| rgb[2] < 0 || rgb[2] > 255)
		return (ft_strlcpy(map->error, "Invalid color", 14), 0);
	finalcolor = ((rgb[0] & 0xff) << 16)
		+ ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff);
	return (finalcolor);
}

bool	set_textures(char *line, t_mapdata *map)
{
	char	**tab;
	int		i;

	tab = ft_split(line, ' ');
	i = 0;
	if (tab[0][0] == '1')
		return (false);
	if (tab[0] && tab[1])
	{
		if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3))
			map->no_texture = ft_strdup(tab[1]);
		if (!ft_strncmp(tab[0], "SO", 3) || !ft_strncmp(tab[0], "so", 3))
			map->so_texture = ft_strdup(tab[1]);
		if (!ft_strncmp(tab[0], "WE", 3) || !ft_strncmp(tab[0], "we", 3))
			map->we_texture = ft_strdup(tab[1]);
		if (!ft_strncmp(tab[0], "EA", 3) || !ft_strncmp(tab[0], "ea", 3))
			map->ea_texture = ft_strdup(tab[1]);
		if (!ft_strncmp(tab[0], "F", 2) || !ft_strncmp(tab[0], "f", 2))
			map->f_color = set_color(tab[1], map);
		if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2))
			map->c_color = set_color(tab[1], map);
	}
	while (tab[i])
		free(tab[i++]);
	return (free(tab), true);
}

bool	add_textures(int fd, t_mapdata *map)
{
	char	*line;
	int		set_lines;

	line = get_next_line(fd);
	set_lines = 0;
	while (line && set_lines < 6)
	{
		if (!set_textures(line, map))
			return (free(line), false);
		else
			set_lines++;
		free(line);
		line = get_next_line(fd);
	}
	free(line);
	return (true);
}

int	copy_old_map(t_mapdata *map, char **newmap, char **newmapflood, int length)
{
	int	i;

	i = 0;
	if (!map->map)
		return (0);
	while (i < length)
	{
		newmap[i] = ft_strdup(map->map[i]);
		newmapflood[i] = ft_strdup(map->mapflood[i]);
		i++;
	}
	return (i);
}

void	add_map_line(const char *line, t_mapdata *map)
{
	static int	length = 0;
	char		**newmap;
	char		**newmapflood;
	int			i;

	newmap = ft_calloc(sizeof(char *), (length + 2));
	if (!newmap)
		return ;
	newmapflood = ft_calloc(sizeof(char *), (length + 2));
	if (!newmapflood)
		return ;
	i = copy_old_map(map, newmap, newmapflood, length);
	newmap[i] = ft_strdup(line);
	newmapflood[i] = ft_strdup(line);
	free_tab_length(map->map, length);
	free_tab_length(map->mapflood, length);
	map->mapheight = ++length;
	map->map = newmap;
	map->mapflood = newmapflood;
}
