/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   check_map_valid.c                                  :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: hexplor <marvin@42.fr>                     +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2023/12/25 13:09:59 by hexplor           #+#    #+#             */
/*   Updated: 2024/02/23 22:54:33 by yantoine         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */
#include "../so_long.h"

static int	check_element_map(char **map, int i, int j)
{
	if (map[i][j] >= '0' && map[i][j] < '5')
		return (1);
	ft_printf("pas les bons numeros\n");
	return (0);
}

static int	check_map(char **map)
{
	int	i;
	int	j;

	i = 0;
	while (map[i])
	{
		j = 0;
		while (map[i][j])
		{
			if (!check_element_map(map, i, j))
				return (0);
			j++;
		}
		i++;
	}
	return (1);
}

static int	check_map_size(char **map)
{
	int	i;
	int	j;
	int	size;

	i = 0;
	if (!map[i])
		return (0);
	size = ft_strlen(map[0]);
	while (map[i])
	{
		j = 0;
		while (map[i][j])
			j++;
		if (j != size)
		{
			ft_printf("la carte doit etre rectangulaire\n");
			return (0);
		}
		i++;
	}
	return (1);
}

static int	check_nb_epc(char **map, t_data *data)
{
	int	i;
	int	j;

	i = 0;
	while (map[i])
	{
		j = 0;
		while (map[i][j])
		{
			check_char(data, i, j);
			j++;
		}
		i++;
	}
	data->max_coin = data->nb_c;
	if (data->nb_e != 1 || data->nb_p != 1 || data->nb_c <= 0)
		return (0);
	return (1);
}

int	check_map_valid(char **map, t_data *data)
{
	if (!check_map(map))
		return (0);
	if (!check_map_size(map))
		return (0);
	if (!check_nb_epc(map, data))
		return (0);
	return (1);
}
