/* printer.c */

#include "computorv1.h"

void print_debug(const char *description, ...)
{
    if (!flag_debug_mode)
        return;

    // print the formatted description
    va_list args;
    va_start(args, description);
    // ft_vdprintf(STDERR_FILENO, description, args); // it's not handling floats for the moment
    vdprintf(STDERR_FILENO, description, args);
    va_end(args);
}

/**
- [x] : ./computorv1 -b "3x² + 0x -0 = x"
        Reduced form: 3x² - = 0
- [x] : ./computorv1 -b "3x² + 0x -7 = x"
        Reduced form: 3x² - - 7 = 0
- [x] : ./computorv1 -b "-3x² + 2x -7 = x"
        Reduced form: -+ x - 7 = 0
 */
static void print_reduced_form_beautify(s_polynom *polynom)
{
    int i;
    bool is_first_term;
    double coefficient;
    int exponent;
    char *default_form;

    default_form = "0 ";
    ft_putstr("Reduced form: ");

    i = 0;
    is_first_term = true;
    while (polynom[i].sign != TERM_SIGN_END)
    {
        coefficient = ft_fabs(polynom[i].coefficient);
        exponent = polynom[i].exponent;

        // if term is zero dont output
        if (coefficient == 0.0)
        {
            i++;
            continue;
        }

        // default_form is not nedded anymore since something will be printed
        default_form = "";

        // print sign
        // don't output '+' if is first term
        if (!(polynom[i].sign == TERM_PLUS && is_first_term))
        {
            ft_putchar(polynom[i].sign);
        }

        // print term
        if (!is_first_term)
        {
            ft_putchar(' ');
        }

        // for x⁰
        if (exponent == 0)
        {
            printf("%g ", coefficient);
        }

        // for x¹
        if (exponent == 1)
        {
            if (coefficient == 1)
                printf("x ");
            if (coefficient > 1)
                printf("%gx ", coefficient);
        }

        // for x²
        if (exponent >= 2)
        {
            if (coefficient == 1)
                printf("x%s ", ft_superscript(exponent + '0'));
            if (coefficient > 1)
                printf("%gx%s ", coefficient, ft_superscript(exponent + '0'));
        }
        fflush(stdout);

        i++;
        is_first_term = false;
    }
    ft_printf("%s= 0\n", default_form);
}

void print_reduced_form(s_polynom *polynom)
{
    int i;
    bool is_first_term;

    if (flag_beautify_mode)
        return print_reduced_form_beautify(polynom);

    // reduced form
    ft_putstr("Reduced form: ");
    is_first_term = true;
    i = 0;
    while (polynom[i].sign != TERM_SIGN_END)
    {
        // print sign
        // don't output '+' if is first term
        if (!(polynom[i].sign == TERM_PLUS && is_first_term))
        {
            ft_putchar(polynom[i].sign);
        }

        // print term
        if (!is_first_term)
        {
            ft_putchar(' ');
        }
        printf("%g * x^%i ", ft_fabs(polynom[i].coefficient), polynom[i].exponent);
        fflush(stdout);

        i++;
        is_first_term = false;
    }
    ft_putstr("= 0\n");
}

void print_degree(s_polynom *polynom, int degree)
{
    if (degree == 0)
    {
        if (polynom[0].coefficient == 0)
        {
            stop_errors("Any real number is a solution.\n");
        }
        else if (polynom[0].coefficient != 0)
        {
            stop_errors("No solution.\n");
        }
    }
    else if (degree == 1)
    {
        ft_printf("Polynomial degree: %i\n", degree);
    }
    else if (degree == 2)
    {
        ft_printf("Polynomial degree: %i\n", degree);
    }
    else
    {
        ft_printf("Polynomial degree: %i\n", degree);
        stop_errors("The polynomial degree is strictly greater than 2, I can't solve.\n");
    }
}