#include "../so_long.h"

void	*read_map(char *argv[], t_data *data)
{
	char	*tmp_map;
	char	*map;
	int		i;
	int		fd;

	if (argv[1][ft_strlen(argv[1]) - 1] != 'r' ||
			argv[1][ft_strlen(argv[1]) - 2] != 'e' ||
			argv[1][ft_strlen(argv[1]) - 3] != 'b' ||
			argv[1][ft_strlen(argv[1]) - 4] != '.')
		return (NULL);
	fd = open(argv[1], O_RDONLY);
	if (fd == -1)
		return NULL;
	i = 0;
	map = NULL;
	tmp_map = NULL;
	while (i == 0 || tmp_map != NULL)
	{
		tmp_map = get_next_line(fd);
		if (tmp_map && map)
			map = ft_strjoin_free(map, tmp_map, 1);
		else if (tmp_map)
			map = ft_strdup(tmp_map);
		free(tmp_map);
		i++;
	}
	data->map = ft_split(map, '\n');
	data->h_map = i - 1;
	free(map);
	return (&data->map);	
}

int	is_rectangular(t_data *data, char **map)
{
	int	x;
	int	y;
	int	w_map;

	x = 0;
	w_map = -1;
	while (map[x])
	{
		y = 0;
		while (map[x][y])
		{
			if (map[x][y] == ' ')
				return (0);
			y++;
		}
		if (w_map != -1 && y != w_map)
			return (0);
		w_map = y;
		x++;
	}
	data->w_map = w_map;
	return (1);
}

int	is_surrounded_by_wall(char **map, int h_map)
{
	int	x;
	int	y;
	
	x = 0;
	y = 0;

	while (map[x] && map[x][y])
	{
		if (map[x][y] != '1')
			return (0);
		y++;
		if (!map[x][y] && h_map - 1 != x)
		{
			x = h_map - 1;
			y = 0;
		}
	}
	while(x > 0)
	{
		if (!map[x] || map[x][0] != '1' || map[x][y - 1] != '1')
			return (0);
		x--;
	}
	return (1);
}

int	is_possible(char **map_cpy, int x, int y, int *check)
{
	if (map_cpy[x][y] == 'E')
		return (1);
	map_cpy[x][y] = 'x';
	if (map_cpy[x][y + 1] != '1' && map_cpy[x][y + 1] != 'x')
		if (is_possible(map_cpy, x, y + 1, check))
			*check = 1;
	if (map_cpy[x + 1][y] != '1' && map_cpy[x + 1][y] != 'x')
		if (is_possible(map_cpy, x + 1, y, check))
			*check = 1;
	if (map_cpy[x - 1][y] != '1' && map_cpy[x - 1][y] != 'x')
		if (is_possible(map_cpy, x - 1, y, check))
			*check = 1;
	if (map_cpy[x][y - 1] != '1' && map_cpy[x][y - 1] != 'x')
		if (is_possible(map_cpy, x, y - 1, check))
			*check = 1;
	if (!has_element(map_cpy, 'C') && *check)
		return (1);
	return (0);
}

/*int	is_possible(t_data *data)
{
	int	x;
	int	y;
	char	**map;

	x = data->x_player;
	y = data->y_player;
	map = ft_strdups(data->map);
	while (data->map[x][y] != 'E')
	{
		map[x][y] = 'x';
		if (map[x][y + 1] != '1' && map[x][y + 1] != 'x')
			y++;
		else if (map[x + 1][y] != '1' && map[x + 1][y] != 'x')
			x++;
		else if (map[x - 1][y] != '1' && map[x - 1][y] != 'x')
			x--;
		else if (map[x][y - 1] != '1' && map[x][y - 1] != 'x')
			y--;
		else
			return(0);
	}
	return (1);
}*/

void	*get_map(char *argv[], t_data *data)
{
	int	i;
	char	**map_cpy;

	if (read_map(argv, data) == NULL)
		return (NULL);
	if (!has_element(data->map, 'C') || !has_element(data->map, 'E') || !has_element(data->map, 'P') || !has_element(data->map, '1'))
	{
		write(1, "Error\nIl manque des elements", 28);
		return (NULL);
	}
	if (!is_rectangular(data, data->map))
	{
		write(1, "Error\nLa carte n'est pas rectangulaire", 38);
		return (NULL);
	}
	if (!is_surrounded_by_wall(data->map, data->h_map))
	{
		write(1, "Error\nLa carte n'est pas entoure de mur", 39);
		return (NULL);
	}
	data = set_position_player(data, get_position(data->map, 'P'));
	i = 0;
	map_cpy = ft_strdups(data->map);
	if (!is_possible(map_cpy, data->y_player, data->x_player, &i))
	{
		write(1, "Error\nLa carte est impossible a faire", 37);
		return (NULL);
	}
	i = 0;
	while (map_cpy[i])
		free(map_cpy[i++]);
	free(map_cpy);
	return (data);
}

