/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   framed_by_wall.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: yantoine <yantoine@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/02/20 21:44:33 by yantoine          #+#    #+#             */
/*   Updated: 2024/02/23 17:10:59 by yantoine         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */
#include "../so_long.h"

static int	same_character(char *string, char character)
{
	int	i;

	i = 0;
	if (!string)
	{
		ft_printf("LA STRING EST NULL\n");
		return (0);
	}
	while (string[i])
	{
		if (string[i] != character)
		{
			ft_printf("string[i]:%c est diff de character:%c\n",
				string[i], character);
			return (0);
		}
		i++;
	}
	return (1);
}

static int	search_last_string(char **map)
{
	int	i;

	if (map == NULL)
		return (0);
	else
	{
		i = 0;
		while (map[i] != NULL)
			i++;
	}
	return (i - 1);
}

static int	check_cote(char **map, t_data *data)
{
	char	*string;
	int		number_height;
	int		number_width;

	string = map[0];
	ft_printf("check_cote\n");
	number_height = 0;
	number_width = ft_strlen(string) - 1;
	if (map == NULL)
		return (0);
	while (map[number_height])
	{
		if (map[number_height][0] != '1'
			|| map[number_height][number_width] != '1')
		{
			ft_printf("map[%d][%d]:%c == '1'?\n",
				number_height, number_width, map[number_height][number_width]);
			return (0);
		}
		number_height++;
	}
	data->tile.number_height_tile = number_height;
	data->tile.number_width_tile = number_width;
	return (1);
}

int	is_framed(char **map, t_data *data)
{
	char	reference_wall;

	reference_wall = map[0][0];
	if (same_character(*map, reference_wall) != 1 \
	|| same_character(map[search_last_string(map)], reference_wall) != 1 \
	|| check_cote(map, data) != 1)
		return (0);
	ft_printf("FRAMED OK\n");
	return (1);
}
