/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_printf_bonus.c                                  :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: thrieg <thrieg@student.42mulhouse.fr>      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/10/23 12:55:41 by thrieg            #+#    #+#             */
/*   Updated: 2025/02/16 19:08:00 by thrieg           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

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

int	ft_case_c_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int	ft_case_percent_bonus(t_vector *vec);
int	ft_case_s_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int	ft_case_p_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int	ft_case_u_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int	ft_case_x_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int	ft_case_upperx_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int	ft_case_d_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
int	ft_case_i_bonus(t_vector *vec, va_list *args, t_arglist *arglist);

static int	ft_handle_percent_bonus_i(
const char *str,
t_vector *vec,
va_list *args,
t_arglist *arglist)
{
	int	offset;

	offset = arglist->size_of_argument_string;
	if (str[0] == 'i')
	{
		if (ft_case_i_bonus(vec, args, arglist))
			return (free(arglist), offset);
		else
			return (free(arglist), 0);
	}
	return (free(arglist), 0);
}

static int	ft_handle_percent_bonus_xd(
const char *str,
t_vector *vec,
va_list *args,
t_arglist *arglist)
{
	int	offset;

	offset = arglist->size_of_argument_string;
	if (str[0] == 'x')
	{
		if (ft_case_x_bonus(vec, args, arglist))
			return (free(arglist), offset);
		else
			return (free(arglist), 0);
	}
	if (str[0] == 'X')
	{
		if (ft_case_upperx_bonus(vec, args, arglist))
			return (free(arglist), offset);
		else
			return (free(arglist), 0);
	}
	if (str[0] == 'd')
	{
		if (ft_case_d_bonus(vec, args, arglist))
			return (free(arglist), offset);
		else
			return (free(arglist), 0);
	}
	return (ft_handle_percent_bonus_i(str, vec, args, arglist));
}

static int	ft_handle_percent_bonus_spu(
const char *str,
t_vector *vec,
va_list *args,
t_arglist *arglist)
{
	int	offset;

	offset = arglist->size_of_argument_string;
	if (str[0] == 's')
	{
		if (ft_case_s_bonus(vec, args, arglist))
			return (free(arglist), offset);
		else
			return (free(arglist), 0);
	}
	if (str[0] == 'p')
	{
		if (ft_case_p_bonus(vec, args, arglist))
			return (free(arglist), offset);
		else
			return (free(arglist), 0);
	}
	if (str[0] == 'u')
	{
		if (ft_case_u_bonus(vec, args, arglist))
			return (free(arglist), offset);
		else
			return (free(arglist), 0);
	}
	return (ft_handle_percent_bonus_xd(str, vec, args, arglist));
}

//returns the number of characters to skip after the % sign
//or 0 if something goes wrong
int	handle_percent_bonus(const char *str, t_vector *vec, va_list *args)
{
	t_arglist	*arglist;
	int			offset;

	arglist = ft_create_arglist(str, args);
	if (!arglist)
		return (0);
	offset = arglist->size_of_argument_string;
	str += offset;
	if (str[0] == 'c')
	{
		if (ft_case_c_bonus(vec, args, arglist))
			return (free(arglist), offset);
		else
			return (free(arglist), 0);
	}
	if (str[0] == '%')
	{
		if (ft_case_percent_bonus(vec))
			return (free(arglist), offset);
		else
			return (free(arglist), 0);
	}
	return (ft_handle_percent_bonus_spu(str, vec, args, arglist));
}
