
#include "push_swap.h"

/*
** this function is called at each action (pushes, swapes, rotates)
** pointers to the stacks a and b are stored into two first element of list solution (because both lists a and b are not available from every functions, like sa() doesn't receive b in argument)
** the function access the list after the two first elements to fill a new element with three things :
** - the name of the action (sa, sp, rra, etc)
** - the stack a
** - the stack b
** like this :
**   name!a: nb nb nb nb nb!b: nb nb nb
** "!" are used to split, it will be turn into :
**   name
**        a: nb nb nb nb nb
**        b: nb nb nb
*/
void	fill_solution(t_list *solution, char *sp)
{
	t_stack	**tmp1;
	t_stack	**tmp2;
	t_stack	*a;
	t_stack	*b;
	char	*stack;

print_test(solution, "fill_solution (beginning)");

	tmp1 = solution->content;
	tmp2 = solution->next->content;
	a = *tmp1;
	b = *tmp2;
	stack = ft_strdup(sp);
	stack = ft_strjoinfree(stack, ft_strdup("!a:"));
	while(a != NULL)
	{
		stack = ft_strjoinfree(stack, ft_strdup(" "));
		stack = ft_strjoinfree(stack, ft_itoa(a->n));
		a = a->next;
	}
	stack = ft_strjoinfree(stack, ft_strdup("!b:"));
	while(b)
	{
		stack = ft_strjoinfree(stack, ft_strdup(" "));
		stack = ft_strjoinfree(stack, ft_itoa(b->n));
		b = b->next;
	}

print_test(solution, "fill_solu. (bfr add_back)");

	ft_lstadd_back(&solution, ft_lstnew(stack));

print_test(solution, "fill_solu. (aft add_back)");

}

void	print_result(t_list *result, int flag)
{
	char	**part;

	result = result->next->next;
	if (!flag)
		result = result->next;
	while (result)
	{
		part = ft_split(result->content, '!');
		ft_printf("%s\n", part[0]);
		if (flag)
		{
			ft_printf("     %s\n", part[1]);
			ft_printf("     %s\n", part[2]);
		}
		result = result->next;
		free(part);
	}
}
