/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   b_h.c                                              :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>

#include "d_define.h"
#include "f_fail.h"
#include "s_struct.h"
#include "u_utils.h"

static uint64_t	b_get_total_hist(t_msh *msh)
{
	uint64_t	total;
	char		*ptr;

	total = 0;
	if (msh->prev_hist != NULL)
	{
		ptr = msh->prev_hist;
		while (*ptr != C_NUL)
		{
			if (*ptr == C_LF)
				total += 1;
			ptr++;
		}
	}
	ptr = msh->curr_hist;
	while (*ptr != C_NUL)
	{
		if (*ptr == C_LF)
			total += 1;
		ptr++;
	}
	return (total);
}

static uint32_t	b_print_prev_history(uint64_t asked, uint64_t total, t_msh *msh)
{
	char	*hist;
	char	*tok;
	int64_t	to_print;
	int64_t	i;

	to_print = (total - asked < 0) ? (0) : (total - asked);
	i = 1;
	if (msh->prev_hist != NULL)
	{
		if ((hist = ft_strdup(msh->prev_hist)) == NULL)
			return (i);
		tok = ft_strtok(hist, "\n");
		while (tok != NULL)
		{
			if (i > to_print)
				ft_printf("%5lu  %s\n", i, tok);
			i++;
			tok = ft_strtok(NULL, "\n");
		}
		ft_memdel((void*)&hist);
	}
	return (i);
}

static void		b_print_history(uint64_t asked, uint64_t total, t_msh *msh)
{
	char	*hist;
	char	*tok;
	int64_t	to_print;
	int64_t	i;

	to_print = (total - asked < 0) ? (0) : (total - asked);
	i = b_print_prev_history(asked, total, msh);
	if ((hist = ft_strdup(msh->curr_hist)) == NULL)
		return ;
	tok = ft_strtok(hist, "\n");
	while (tok != NULL)
	{
		if (i > to_print)
			ft_printf("%5lu  %s\n", i, tok);
		i++;
		tok = ft_strtok(NULL, "\n");
	}
	ft_memdel((void*)&hist);
}

static t_bool	b_check_numeric(const char arg[])
{
	char	*ptr;

	ptr = (char*)arg;
	while (*ptr != C_NUL)
	{
		if (ft_isdigit(*ptr) == FALSE)
		{
			return (FALSE);
		}
		ptr++;
	}
	return (TRUE);
}

uint8_t			b_h(char *args[], t_msh *msh)
{
	const uint64_t	argc = u_builtins_get_argc((const char **)args);

	if (argc > 1)
	{
		f_fail_too_many_args("h", msh);
		return (127);
	}
	if (argc == 1 && b_check_numeric(*args) == FALSE)
	{
		ft_dprintf(STDERR_FILENO,
			"minishell: history: %s: numeric argument required\n",
			*args);
		return (1);
	}
	else
	{
		if (argc == 1)
			b_print_history(ft_atoi(*args), b_get_total_hist(msh), msh);
		else
			b_print_history(0, 0, msh);
	}
	return (0);
}