/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   map_checker.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: tchampio <tchampio@student.42lehavre.      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/06/06 17:54:42 by tchampio          #+#    #+#             */
/*   Updated: 2025/06/24 11:31:48 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);
}

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

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

void	flood_fill(t_mapdata *map, int x, int y)
{
	if (map->mapflood[y][x] == '1')
		return ;
	if (map->mapflood[y][x] == ' ' || map->mapflood[y][x] == '\n')
		map->isvalid = false;
	map->mapflood[y][x] = '1';
	flood_fill(map, x + 1, y);
	flood_fill(map, x, y + 1);
	flood_fill(map, x - 1, y);
	flood_fill(map, x, y - 1);
}

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 (!check_bare_minimum(map))
		return (close(fd), false);
	map->isvalid = true;
	flood_fill(map, map->startx, map->starty);
	if (!map->isvalid)
		return (close(fd), ft_strlcpy(map->error, "Map has holes!", 16), false);
	return (true);
}
