/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_case_d_bonus.c                                  :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: thrieg <thrieg@student.42mulhouse.fr>      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/10/25 17:12:38 by thrieg            #+#    #+#             */
/*   Updated: 2025/02/16 19:07:44 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);
void		convert_precision_if_flag_zero(t_arglist *arglist, int nbr);
int			ft_handle_if_precision(
				t_arglist *arglist,
				int nbr,
				t_vector *vec,
				int *size);
int			ft_handle_if_not_precision(
				t_arglist *arglist,
				int nbr,
				t_vector *vec,
				int *size);
int			ft_handle_concat_and_padding(
				t_arglist *arglist,
				char *str_to_cat,
				t_vector *vec,
				int size);

//flags to handle: zero, space, plus, precision, justified left... hard mode
//flag zero padding without precision behaves just like a precision
//(zeroes after the sign) and when it's spaces they're before the sign
int	ft_case_d_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
	char	*str_to_cat;
	int		nbr;
	int		size;

	nbr = va_arg(*args, int);
	str_to_cat = init_str_to_cat(arglist, nbr);
	if (!str_to_cat)
		return (0);
	convert_precision_if_flag_zero(arglist, nbr);
	size = (int)ft_strlen(str_to_cat);
	if (nbr < 0)
		size--;
	if ((size < arglist->precision) && (arglist->precision >= 0))
	{
		if (!ft_handle_if_precision(arglist, nbr, vec, &size))
			return (free(str_to_cat), 0);
	}
	else
	{
		if (!ft_handle_if_not_precision(arglist, nbr, vec, &size))
			return (free(str_to_cat), 0);
	}
	if (!ft_handle_concat_and_padding(arglist, str_to_cat, vec, size))
		return (free(str_to_cat), 0);
	return (free(str_to_cat), 1);
}
