/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   map_checker.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: tchampio <tchampio@student.42lehavre.      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/06/06 17:54:42 by tchampio          #+#    #+#             */
/*   Updated: 2025/06/17 16:33:43 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(char **tab)
{
	int	i;

	i = 0;
	while (tab[i])
	{
		free(tab[i]);
		i++;
	}
	free(tab);
}

void	get_map_height(t_mapdata *map)
{
	int		fd;
	char	**splitted;
	char	*line;
	int		i;
	int		count;

	fd = open(map->filename, O_RDONLY);
	line = get_next_line(fd);
	splitted = ft_split(line, ' ');
	i = 0;
	count = 0;
	while (splitted[i] && splitted[i][0] != '1')
	{
		free_tab(splitted);
		free(line);
		line = get_next_line(fd);
		i++;
		splitted = ft_split(line, ' ');
	}
	free_tab(splitted);
	while (line)
	{
		free(line);
		count++;
		line = get_next_line(fd);
	}
	map->mapheight = count + 1;
	close(fd);
}

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

	get_map_height(map);
	ft_printf("%d\n", map->mapheight);
	map->map = malloc(sizeof(char *) * map->mapheight);
	if (!map->map)
		return ;
	map->mapflood = malloc(sizeof(char *) * map->mapheight);
	if (!map->mapflood)
		return ;
	line = get_next_line(fd);
	ft_printf("%s", line);
	i = 0;
	while (line)
	{
		map->map[i] = ft_strdup(line);
		map->mapflood[i] = ft_strdup(line);
		i++;
		free(line);
		line = get_next_line(fd);
	}
	free(line);
}

bool	check_walls(int fd, t_mapdata *map)
{
	(void)fd;
	ft_printf("%d\n", map->mapheight);
	return (true);
}

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

	i = 0;
	while (map && map[i])
	{
		ft_printf("%s", map[i]);
		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);
	close(fd);
	fd = open(file, O_RDONLY);
	populate_maps(map, fd);
	//if (!check_walls(fd, map))
	//	return (close(fd), ft_strlcpy(map->error,
	//			"Map is malformed (invalid chars or missing walls)", 51), false);
	print_map(map->map);
	return (true);
}
