/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   BFS_ALGORITHM_PATH_E_S_C.c                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: yantoine <yantoine@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/02/20 19:55:42 by yantoine          #+#    #+#             */
/*   Updated: 2024/02/22 02:33:36 by yantoine         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */
#include "../../so_long.h"

static int	check_number(char number, int max_c, int checking)
{
	static int	coin;
	static int	sortie;

	if (number == '3' && !checking)
	{
		ft_printf("+1 exit\n");
		sortie += 1;
	}
	else if (number == '2' && !checking)
	{
		ft_printf("+1 coin\n");
		coin += 1;
		ft_printf("coin: %d/%d\n", coin, max_c);
	}
	else if (number == '1')
		return (0);
	if (sortie == 1 && coin == max_c)
		return (2);
	return (1);
}

static int	check_checked(t_Element *actuel, t_position position)
{
	if (actuel->crdn.x == position.x && actuel->crdn.y == position.y)
		return (1);
	else
	{
		if (actuel->suivant != NULL)
			if (check_checked(actuel->suivant, position) == 1)
				return (1);
	}
	return (0);
}

static int	check_adjacent(int max_c, t_position position,
	char **map, t_Liste *liste)
{
	if (check_checked(liste->premier, position) != 1)
	{
		insertion(liste, position);
		if (check_number(map[position.y][position.x], max_c, 0) == 1)
		{
			position.x += 1;
			if (check_adjacent(max_c, position, map, liste) == 1)
				return (1);
			position.x -= 2;
			if (check_adjacent(max_c, position, map, liste) == 1)
				return (1);
			position.x += 1;
			position.y += 1;
			if (check_adjacent(max_c, position, map, liste) == 1)
				return (1);
			position.y -= 2;
			if (check_adjacent(max_c, position, map, liste) == 1)
				return (1);
		}
		else if (check_number(map[position.y][position.x], max_c, 1) == 2)
			return (1);
	}
	return (0);
}

static t_position	trouve_depart(t_data *data)
{
	t_position	depart;
	char		**map;
	int			i;
	int			j;

	map = data->map;
	i = 0;
	while (map[i])
	{
		j = 0;
		while (map[i][j])
		{
			if (map[i][j] == '4')
			{
				depart.x = j;
				depart.y = i;
				return (depart);
			}
			j++;
		}
		i++;
	}
	depart.x = -1;
	return (depart);
}

int	pathfinder(t_data *data)
{
	t_Liste	*liste;

	liste = initialise_liste();
	data->depart = trouve_depart(data);
	if (check_adjacent(data->max_coin, data->depart, data->map, liste) == 1)
	{
		data->path_finder_memory = liste;
		ft_printf("COMPLETE\n");
		return (1);
	}
	data->path_finder_memory = liste;
	return (0);
}
