/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_case_uppx_bonus.c                               :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: thrieg <thrieg@student.42mulhouse.fr>      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/10/25 17:11:07 by thrieg            #+#    #+#             */
/*   Updated: 2025/02/16 19:07:50 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);

static int	handle_hashtag_prefix(
t_vector *vec,
t_arglist *arglist,
unsigned int nbr,
int flag_zero)
{
	if (arglist->flag_hashtag && !flag_zero && nbr != 0)
	{
		if (!ft_vector_concat(vec, "0X", 2))
			return (0);
	}
	return (1);
}

static int	apply_precision_padding(t_vector *vec, size_t *size, int precision)
{
	while ((int)(*size)++ < precision)
	{
		if (!ft_vector_pushback(vec, '0'))
			return (0);
	}
	(*size)--;
	return (1);
}

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

static int	handle_padding_and_prefix(
t_vector *vec,
t_arglist *arglist,
size_t size,
unsigned int nbr)
{
	if (arglist->flag_hashtag && arglist->flag_zero && nbr != 0)
	{
		if (!ft_vector_concat(vec, "0X", 2))
			return (0);
	}
	if (((int)size < arglist->precision) && (arglist->precision >= 0))
	{
		if (padding_if_needed_before(arglist, 'X', arglist->precision
				+ (arglist->flag_hashtag * 2 * (nbr != 0)), vec) < 0)
			return (0);
		if (!handle_hashtag_prefix(vec, arglist, nbr, arglist->flag_zero))
			return (0);
	}
	else
	{
		if (arglist->flag_hashtag && nbr != 0)
			size += 2;
		if (padding_if_needed_before(arglist, 'X', size, vec) < 0)
			return (0);
		if (!handle_hashtag_prefix(vec, arglist, nbr, arglist->flag_zero))
			return (0);
	}
	return (1);
}

int	ft_case_upperx_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
	char			*str_to_cat;
	size_t			size;
	unsigned int	nbr;

	nbr = va_arg(*args, unsigned int);
	str_to_cat = init_str_to_cat(arglist, nbr);
	if (!str_to_cat)
		return (0);
	size = ft_strlen(str_to_cat);
	if (!handle_padding_and_prefix(vec, arglist, size, nbr))
		return (free(str_to_cat), 0);
	if (((int)size < arglist->precision) && (arglist->precision >= 0))
	{
		if (!apply_precision_padding(vec, &size, arglist->precision))
			return (free(str_to_cat), 0);
	}
	if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
		return (free(str_to_cat), 0);
	free(str_to_cat);
	if (padding_if_needed_after(arglist, 'X', size, vec) < 0)
		return (0);
	return (1);
}
