/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   lst_textures.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: aortigos <aortigos@student.42malaga.com    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/12/01 08:48:55 by aortigos          #+#    #+#             */
/*   Updated: 2026/01/11 16:05:14 by aortigos         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../../includes/cub3d.h"

int	get_index(char *line, int i)
{
	while (ft_isspace(line[i]))
		i++;
	return (i);
}

t_texture_list	*new_texture(char *line)
{
	t_texture_list	*list;

	list = (t_texture_list *)ft_calloc(sizeof(t_texture_list), 1);
	if (!list)
		return (NULL);
	while (ft_isspace(*line))
		line++;
	if ((!ft_strncmp(line, "NO", 2) || !ft_strncmp(line, "SO", 2))
		|| !ft_strncmp(line, "WE", 2) || !ft_strncmp(line, "EA", 2))
	{
		list->name = ft_substr(line, 0, 2);
		list->value = ft_substr(line, get_index(line, 2), ft_strlen(line));
	}
	else if ((!ft_strncmp(line, "F", 1) || !ft_strncmp(line, "C", 1)))
	{
		list->name = ft_substr(line, 0, 1);
		list->value = ft_substr(line, get_index(line, 1), ft_strlen(line));
	}
	list->next = NULL;
	return (list);
}

void	lst_back_ture(t_texture_list **l_ture, t_texture_list *new)
{
	t_texture_list	*tmp;

	if (!*l_ture)
	{
		(*l_ture) = new;
		return ;
	}
	tmp = *l_ture;
	while (tmp->next)
		tmp = tmp->next;
	tmp->next = new;
}

int	lst_ture(t_data *m, t_texture_list	**l_ture)
{
	int				i;
	t_texture_list	*tmp;

	i = 0;
	while (m->texture2d[i])
	{
		tmp = new_texture(m->texture2d[i++]);
		if (!tmp)
			return (0);
		lst_back_ture(l_ture, tmp);
	}
	return (1);
}
