/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   map_checker.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: tchampio <tchampio@student.42lehavre.      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/06/06 17:54:42 by tchampio          #+#    #+#             */
/*   Updated: 2025/06/20 12:43:33 by tchampio         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../../includes/maputils.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "../../includes/libft.h"

void	print_mapdata(const t_mapdata *data)
{
	ft_printf(BOLD CYAN "=== Map Data ===\n" RESET);
	ft_printf(BOLD "Filename: " RESET "%s\n", data->filename);
	ft_printf(BOLD "NO Texture: " RESET "%s\n", data->NO_texture);
	ft_printf(BOLD "SO Texture: " RESET "%s\n", data->SO_texture);
	ft_printf(BOLD "WE Texture: " RESET "%s\n", data->WE_texture);
	ft_printf(BOLD "EA Texture: " RESET "%s\n", data->EA_texture);
	ft_printf(BOLD "F color: " RESET "%x\n", data->f_color);
	ft_printf(BOLD "C color: " RESET "%x\n", data->c_color);
	ft_printf(BOLD "Validity: " RESET);
	if (data->isvalid)
		ft_printf(GREEN "VALID\n" RESET);
	else
		ft_printf(RED "INVALID\n" RESET);
	if (!data->isvalid && data->error[0] != '\0')
	{
		ft_printf(BOLD RED "Error: " RESET "%s\n", data->error);
	}
	ft_printf(CYAN "=================\n" RESET);
}

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

bool	check_filename(t_mapdata *map, char *file)
{
	int		filename_size;
	int		i;
	int		j;
	char	end[5];

	filename_size = ft_strlen(file);
	i = filename_size;
	j = 4;
	ft_bzero(end, 5);
	while (i > (filename_size - 5))
	{
		end[j] = file[i];
		i--;
		j--;
	}
	if (ft_strncmp(end, ".cub", 5))
		return (false);
	map->filename = ft_strdup(file);
	return (true);
}

void	free_tab_length(char **tab, int length)
{
	int	i;

	if (!tab)
		return ;
	i = 0;
	while (i < length)
	{
		free(tab[i]);
		++i;
	}
	free(tab);
}

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 + 1));
	if (!newmap)
		return ;
	newmapflood = ft_calloc(sizeof(char *), (length + 1));
	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;
}

void	populate_maps(t_mapdata *map, int fd)
{
	char	*line;

	line = get_next_line(fd);
	while (line)
	{
		if (line[0] != '\n')
			add_map_line(line, map);
		free(line);
		line = get_next_line(fd);
	}
	free(line);
}

bool	check_walls(t_mapdata *map)
{
	int	i;

	i = 0;
	while (i < map->mapheight)
	{
		ft_printf("line: %s", map->map[i]);
		i++;
	}
	return (true);
}

void print_map(char **map)
{
	int		i;

	i = 0;
	while (map && map[i])
	{
		ft_printf("line2: %s", map[i]);
		i++;
	}
	ft_printf("(%p) %s", map[i], map[i]);
}

bool	check_cubfile(char *file, t_mapdata *map)
{
	int	fd;

	if (!check_filename(map, file))
		return (ft_strlcpy(map->error, "File is not a .cub file", 25), false);
	fd = open(file, O_RDONLY);
	if (fd < 0)
		return (ft_strlcpy(map->error, "Can't open file", 16), false);
	if (!add_textures(fd, map))
		return (close(fd), ft_strlcpy(map->error,
				"Map started before all the textures", 37), false);
	if (map->error[0])
		return (close(fd), false);
	populate_maps(map, fd);
	if (!check_walls(map))
		return (close(fd), ft_strlcpy(map->error,
				"Map is malformed (invalid chars or missing walls)", 51), false);
	//if (!flood_fill(map->mapflood))
	//	return (close(fd), ft_strlcpy(map->error,
	//			"Map is not possible (flood fill failed)", 41), false);
	return (true);
}
