#include <stdio.h>
#include <stdlib.h>
#include "hash.h"

int main(int argc, char **argv) {
   int i, profile;
   struct htab *curhash;
   char curcommand[MAXCOMMAND];
   char op[MAXCOMMAND];
   char op2[MAXCOMMAND];

   profile = 0;
 
   if (argc > 1) {
      profile = (atoi(argv[1]));
   }

   printf("Profiling set to %d\n", profile);
   
   while (i = gettoken(curcommand)) {
      if (!strcmp(curcommand, "add")) {
         i = gettoken(op);
         i = gettoken(op2);
         printf("Adding \'%s\' to key \'%s\'\n", op2, op);
         curhash = addhash(op, op2);
         printf("Added \'%s\' to \'%s\'\n", curhash->data, curhash->key);
         printf("curhash: %lx\n", curhash);
         if (curhash->parent == NULL) {
            printf("No Parent!\n");
         }
         else {
            printf("Parent: %lx\n", curhash->parent);
         }
         if (curhash->child == NULL) {
            printf("No Child!\n");
         } 
         else {
            printf("Parent: %lx\n", curhash->child);
         }
      }
      else if (!strcmp(curcommand, "del")) {
         i = gettoken(op);
         printf("Removing key \'%s\'\n", op);
         delhash(op) ? printf("Removed Key\n") : printf("Could not remove key\n");
      }
      else if (!strcmp(curcommand, "find")) {
         i = gettoken(op);
         
         curhash=findhash(op);
         if (curhash != NULL) {
            printf("curhash: %lx\n", curhash);
            printf("key: %s\n", curhash->key);
            printf("data: %s\n", curhash->data);

            if (curhash->parent == NULL) {
               printf("No Parent!\n");
            }
            else {
               printf("Parent: %lx\n", curhash->parent);
            }
            if (curhash->child == NULL) {
               printf("No Child!\n");
            }
            else {
               printf("Child: %lx\n", curhash->child);
            }
         }
         else {
            printf("NULL\n");
         }
      }
      else if (!strcmp(curcommand, "hash")) {
         i = gettoken(op);
         printf("%s --> %d\n", op, hash(op));
      }
      else if (!strcmp(curcommand, "quit")) {
         if (profile > 0) {
            printf("profile:\n");
            if (profile > 3) {
               profile = 3;
            }
            hashprofile(profile);
         }

         return 0;
      }
      else {
         printf("ERR: Invalid Command: %s\n", curcommand);
      }
   }


   return 0;
}
