/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_case_d_utils_bonus.c                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: thrieg <thrieg@student.42mulhouse.fr>      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/10/27 15:59:04 by thrieg            #+#    #+#             */
/*   Updated: 2025/02/16 19:07:45 by thrieg           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../libft.h"
#include "ft_printf.h"
#include "ft_printf_bonus.h"

int	padding_if_needed_before(
		t_arglist *arglist,
		char caller,
		size_t len,
		t_vector *vec);
int	padding_if_needed_after(
		t_arglist *arglist,
		char caller,
		size_t len,
		t_vector *vec);

char	*init_str_to_cat(t_arglist *arglist, unsigned int nbr)
{
	if (nbr == 0 && arglist->precision == 0)
		return (ft_strdup(""));
	return (ft_itoa(nbr));
}

void	convert_precision_if_flag_zero(t_arglist *arglist, int nbr)
{
	if ((arglist->precision < 0) && !(arglist->flag_justified_left)
		&& arglist->flag_zero)
	{
		arglist->precision = arglist->width_mini;
		arglist->width_mini = 0;
		if (nbr < 0 || arglist->flag_plus || arglist->flag_space)
			arglist->precision--;
	}
}

int	ft_handle_if_precision(
t_arglist *arglist,
int nbr,
t_vector *vec,
int *size)
{
	if (nbr < 0 || arglist->flag_plus || arglist->flag_space)
	{
		if (padding_if_needed_before(arglist, 'd',
				arglist->precision + 1, vec) < 0)
			return (0);
	}
	else
		if (padding_if_needed_before(arglist, 'd', arglist->precision, vec) < 0)
			return (0);
	if (arglist->flag_plus && (nbr >= 0) && !ft_vector_pushback(vec, '+'))
		return (0);
	if (arglist->flag_space && (nbr >= 0) && !(arglist->flag_plus) && nbr >= 0)
		if (!ft_vector_pushback(vec, ' '))
			return (0);
	if (nbr < 0 && !ft_vector_pushback(vec, '-'))
		return (0);
	while ((*size) < arglist->precision)
	{
		if (!ft_vector_pushback(vec, '0'))
			return (0);
		(*size)++;
	}
	if (nbr < 0 || arglist->flag_plus || arglist->flag_space)
		(*size)++;
	return (1);
}

int	ft_handle_if_not_precision(
t_arglist *arglist,
int nbr,
t_vector *vec,
int *size)
{
	if (nbr < 0 || arglist->flag_space || arglist->flag_plus)
		(*size)++;
	if (padding_if_needed_before(arglist, 'd', (*size), vec) < 0)
		return (0);
	if (arglist->flag_plus && (nbr >= 0))
		if (!ft_vector_pushback(vec, '+'))
			return (0);
	if (arglist->flag_space && (nbr >= 0) && !(arglist->flag_plus))
		if (nbr >= 0)
			if (!ft_vector_pushback(vec, ' '))
				return (0);
	if (nbr < 0)
		if (!ft_vector_pushback(vec, '-'))
			return (0);
	return (1);
}

int	ft_handle_concat_and_padding(
t_arglist *arglist,
char *str_to_cat,
t_vector *vec,
int size)
{
	if (!ft_vector_concat(vec, str_to_cat + ft_strnonchr(str_to_cat, '-'),
			ft_strlen(str_to_cat) - ft_strnonchr(str_to_cat, '-')))
		return (0);
	if (padding_if_needed_after(arglist, 'd', size, vec) < 0)
		return (0);
	return (1);
}
