// ===<>===<>===-< <<< src/io.c >>> >-===<>===<>===

#include <include.h>
#include <io.h>

#include "include.h"
#include "io.h"

static CMD_GRP_LIST cmd_list = {0, NULL};

void init_io(void){
    cmd_list.list = NULL;
    cmd_list.n = 0;
}

[[nodiscard]] int create_cmd_list(char* get[], int n){
    if (n <= 0) return -1;

    CMD_LIST* newlist = malloc(sizeof(CMD_LIST));
    if (!newlist) return -1;

    newlist->n = n;
    newlist->str = malloc(n * sizeof(char*));

    if (!newlist->str) {
        free(newlist);
        return -1;
    }

    for (int i = 0; i < n; i++) {
        newlist->str[i] = malloc(strlen(get[i]) + 1);

        if (!newlist->str[i]) {
            for (int j = 0; j < i; j++)
                free(newlist->str[j]);

            free(newlist->str);
            free(newlist);
            return -1;
        }

        strcpy(newlist->str[i], get[i]);
    }

    CMD_LIST** tmp = realloc(cmd_list.list, (cmd_list.n + 1) * sizeof(CMD_LIST*));

    if (!tmp) {
        for (int i = 0; i < n; i++)
            free(newlist->str[i]);

        free(newlist->str);
        free(newlist);
        return -1;
    }

    cmd_list.list = tmp;
    cmd_list.list[cmd_list.n] = newlist;

    cmd_list.n++;

    return cmd_list.n - 1;
}

void del_cmd_list(int id){
    if (id < 0 || (unsigned int)id >= cmd_list.n)
        return;

    CMD_LIST* list = cmd_list.list[id];

    if (list == NULL)
        return;

    for (unsigned int i = 0; i < list->n; i++) {
        free(list->str[i]);
    }

    free(list->str);
    free(list);

    for (unsigned int i = id; i + 1 < cmd_list.n; i++) {
        cmd_list.list[i] = cmd_list.list[i + 1];
    }

    cmd_list.n--;

    if (cmd_list.n == 0) {
        free(cmd_list.list);
        cmd_list.list = NULL;
        return;
    }

    CMD_LIST** tmp = realloc(cmd_list.list, cmd_list.n * sizeof(CMD_LIST*));

    if (tmp != NULL) {
        cmd_list.list = tmp;
    }
}

char querry_input(char *inquerry, char *_querry){
    int ch;
    *inquerry = 0;

    memset(_querry, 0, QUERRY_MAX);

    while ((ch = getchar()) > ' ' && *inquerry < QUERRY_MAX - 1) {
        _querry[(int)*inquerry] = (char)ch;
        (*inquerry)++;
    }

    _querry[(int)*inquerry] = '\0';

    if (ch == EOF)
        return -1;

    return 0;
}

[[nodiscard]] char cmd_querry(char* _querry, int id){
    if (id < 0 || (unsigned int)id >= cmd_list.n)
        return -1;

    CMD_LIST* list = cmd_list.list[id];

    if (!list)
        return -1;

    for (unsigned int i = 0; i < list->n; i++) {
        if (strcmp(_querry, list->str[i]) == 0)
            return i;
    }

    return -1;
}

[[nodiscard]] int get_int(void){
    char inquerry;
    char _querry[QUERRY_MAX];
    while(querry_input(&inquerry,_querry) > 0 && *_querry > ' ');

    long int num = 0;
    char flagIsDec = 1;
    { // GetNum
        int i = 0;
        if(_querry[0]=='-'){i++;flagIsDec = -1;}

        for(; i<inquerry;i++)
            if(_querry[i]>= '0' && _querry[i] <= '9')
                num = (num*10) + (_querry[i]-'0');
        num *= flagIsDec;
    }
        if (num>INT_MAX)num = INT_MAX;
        if (num<INT_MIN)num = INT_MIN;
        return (int)num;
}

