#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h> // For isspace()
#include <stdarg.h>

#include <table.h>
#include <array_1d.h>

/*
 * int_create() - make a dynamic copy of an integer
 * @i: Integer to copy
 *
 * Returns: A pointer to a dynamic copy of i
 */
int *int_create(int i)
{
    // Allocate memory for an integer and set the value
    int *v = malloc(sizeof(*v));
    *v = i;
    return v;
}

int key_cmp_func(const void *a, const void *b)
{

    const int *pntkey1 = a;
    const int key1 = *pntkey1;
    const int *pntkey2 = b;
    const int key2 = *pntkey2;

    if (key1 == key2)
        return 0;
    if (key1 < key2)
        return -1;
    return 1;
}

// int *print_func(table *t)

// Return the memory used by the integer.
void int_kill(void *v)
{
    int *p = v;
    free(p);
}

// Interpret the supplied key and value pointers and print their content.
void print_int(const void *key,const void *value)
{
    const int *k = key;
    const int *s = value;
    printf("[%d, %d]\n", *k, *s);
}

int main(void)
{

    printf("================ TABLETESTER ================\n");
    table *t = table_empty(key_cmp_func, int_kill, int_kill);





    /**/
    printf("Empty table created...\n");

    int choice = 1;
    while (choice != 0)
    {
        printf("Choose Next Action!\n[0} Exit\n[1] table_is_empty\n[2] table_insert\n");
        printf("[3] table_lookup\n[4] table_remove\n[5] table_print\n");
        scanf("%d",&choice);
        printf("\nChoice[%d]\n\n",choice);
        if (choice == 1)
        {
            bool empty = table_is_empty(t);
            if(empty)
            {
                printf("Table is empty...\n");
            }
            else
            {
                printf("Table is not empty...\n");
            }
            
        }
        if (choice == 2)
        {
            printf("table_insert()\nKey...");
            int key;
            scanf("%d", &key);
            printf("\nValue...");
            int value;
            scanf("%d", &value);
            table_insert(t, int_create(key), int_create(value));
            printf("\nInsert Complete!\n\n");
        }
        if (choice == 3)
        {
            printf("table_lookup()\nKey...");
            int key;
            scanf("%d", &key);
            int *pntr = table_lookup(t, int_create(key));
            //int val = *pntr;
            printf("\nLookup complete...[%d]\n\n", *pntr);
        }
        if (choice == 4)
        {
            printf("table_remove()\nKey...");
            int key;
            scanf("%d", &key);
            table_remove(t, int_create(key));
            printf("\nRemoval complete...\n\n");
        }
        if (choice == 5)
        {
            table_print(t, print_int);
        }
        if (choice > 5)
        {
            printf("invalid choice...\n\n");
        }
    }


    return 0;
}