#include "computorv1.h"

void remove_spaces(char *s)
{
    char *read = s;
    char *write = s;

    // copy all non-space chars
    while (*read)
    {
        if (!ft_isspace(*read))
        {
            *write++ = *read;
        }
        read++;
    }
    *write = '\0';

    // zero the rest of the buffer
    while (write != read)
    {
        *write++ = '\0';
    }
}

int count_any_of(const char *s, const char *set)
{
    int count = 0;
    for (; *s != '\0'; s++)
    {
        if (ft_strchr(set, *s) != NULL)
        {
            count++;
        }
    }
    return count;
}

int main(int ac, char **av)
{
    int i;
    int arg_len;
    int terms_count_prediction;
    char *input;

    if (ac < 2)
    {
        return 0;
    }

    // tmp debug output
    ft_putstr("-> received args :\n"); // debug
    i = 0;
    while (i < ac)
    {
        ft_putstr("   ");
        ft_putnbr(i);
        ft_putstr(" : ");
        ft_putstr(av[i]);
        ft_putchar('\n');
        i++;
    }

    input = av[1];
    remove_spaces(input);
    arg_len = ft_strlen(input);

    ft_putstr("-> input without space : "); // debug
    ft_putstr(input);                       // debug
    ft_putchar('\n');                       // debug
    ft_putstr("-> arg_len : ");             // debug
    ft_putnbr(arg_len);                     // debug
    ft_putchar('\n');                       // debug

    token tokens[arg_len];
    int tokens_count = lexerize(input, tokens);

    ft_putstr("-> tokens_count : "); // debug
    ft_putnbr(tokens_count);         // debug
    ft_putchar('\n');                // debug

    // tmp debug output
    ft_putchar('\n'); // debug
    i = 0;
    while (tokens[i].type != TOKEN_END)
    {
        ft_printf("token %2i - type : ", i);

        if (tokens[i].type == TOKEN_VARIABLE)
            ft_printf("%20s", "TOKEN_VARIABLE");
        else if (tokens[i].type == TOKEN_NUMBER)
            ft_printf("%20s", "TOKEN_NUMBER");
        else if (tokens[i].type == TOKEN_POWER)
            ft_printf("%20s", "TOKEN_POWER");
        else if (tokens[i].type == TOKEN_SIGN)
            ft_printf("%20s", "TOKEN_SIGN");
        else if (tokens[i].type == TOKEN_FACTOR)
            ft_printf("%20s", "TOKEN_FACTOR");
        else if (tokens[i].type == TOKEN_EQUAL)
            ft_printf("%20s", "TOKEN_EQUAL");
        else if (tokens[i].type == TOKEN_END)
            ft_printf("%20s", "TOKEN_END");

        ft_putstr(" - value : ");

        if (tokens[i].type == TOKEN_NUMBER)
        {
            printf("%g\n", tokens[i].value_double);
        }
        else
        {
            ft_printf("%c\n", tokens[i].value_char);
        }
        i++;
    }
    ft_putchar('\n'); // debug
    // END tmp debug output

    terms_count_prediction = count_any_of(input, "-+=") + 1; // +1 for first term that can have no leading '+'

    ft_putstr("-> terms_count_prediction : "); // debug
    ft_putnbr(terms_count_prediction);         // debug
    ft_putchar('\n');                          // debug

    term terms[terms_count_prediction];
    int term_count = parse(tokens, terms, terms_count_prediction);

    ft_putstr("-> terms_count : "); // debug
    ft_putnbr(term_count);          // debug
    ft_putchar('\n');               // debug

    return (0);
}
