#include "arc/console/key.h"
#include "key.h"
#include "arc/std/bool.h"
#include <ncurses.h>
#include <stdlib.h>

void ARC_ConsoleKey_Create(ARC_ConsoleKey **consoleKey, ARC_ConsoleKey_Key *key){
    *consoleKey = (ARC_ConsoleKey *)malloc(sizeof(ARC_ConsoleKey));
    (*consoleKey)->key = 0; 

    if(key != NULL){
        (*consoleKey)->key = *key; 
    }
}

void ARC_ConsoleKey_Destroy(ARC_ConsoleKey *consoleKey){
    free(consoleKey);
}

ARC_Bool ARC_ConsoleKey_Equals(ARC_ConsoleKey consoleKey, ARC_ConsoleKey_Key key){
    return consoleKey.key == ARC_Keyboard_GetConsoleKey(key).key;
}

ARC_Bool ARC_ConsoleKey_EqualsPointer(ARC_ConsoleKey *consoleKey, ARC_ConsoleKey_Key key){
    return consoleKey->key == ARC_Keyboard_GetConsoleKey(key).key;
}

ARC_ConsoleKey ARC_Keyboard_GetConsoleKey(enum ARC_ConsoleKey_Key key){
    switch(key){
        case ARC_CONSOLE_KEY_A:
            return (ARC_ConsoleKey){ (int32_t)'a' };

        case ARC_CONSOLE_KEY_B:
            return (ARC_ConsoleKey){ (int32_t)'b' };

        case ARC_CONSOLE_KEY_C:
            return (ARC_ConsoleKey){ (int32_t)'c' };

        case ARC_CONSOLE_KEY_D:
            return (ARC_ConsoleKey){ (int32_t)'d' };

        case ARC_CONSOLE_KEY_E:
            return (ARC_ConsoleKey){ (int32_t)'e' };

        case ARC_CONSOLE_KEY_F:
            return (ARC_ConsoleKey){ (int32_t)'f' };

        case ARC_CONSOLE_KEY_G:
            return (ARC_ConsoleKey){ (int32_t)'g' };

        case ARC_CONSOLE_KEY_H:
            return (ARC_ConsoleKey){ (int32_t)'h' };

        case ARC_CONSOLE_KEY_I:
            return (ARC_ConsoleKey){ (int32_t)'i' };

        case ARC_CONSOLE_KEY_J:
            return (ARC_ConsoleKey){ (int32_t)'j' };

        case ARC_CONSOLE_KEY_K:
            return (ARC_ConsoleKey){ (int32_t)'k' };

        case ARC_CONSOLE_KEY_L:
            return (ARC_ConsoleKey){ (int32_t)'l' };

        case ARC_CONSOLE_KEY_M:
            return (ARC_ConsoleKey){ (int32_t)'m' };

        case ARC_CONSOLE_KEY_N:
            return (ARC_ConsoleKey){ (int32_t)'n' };

        case ARC_CONSOLE_KEY_O:
            return (ARC_ConsoleKey){ (int32_t)'o' };

        case ARC_CONSOLE_KEY_P:
            return (ARC_ConsoleKey){ (int32_t)'p' };

        case ARC_CONSOLE_KEY_Q:
            return (ARC_ConsoleKey){ (int32_t)'q' };

        case ARC_CONSOLE_KEY_R:
            return (ARC_ConsoleKey){ (int32_t)'r' };

        case ARC_CONSOLE_KEY_S:
            return (ARC_ConsoleKey){ (int32_t)'s' };

        case ARC_CONSOLE_KEY_T:
            return (ARC_ConsoleKey){ (int32_t)'t' };

        case ARC_CONSOLE_KEY_U:
            return (ARC_ConsoleKey){ (int32_t)'u' };

        case ARC_CONSOLE_KEY_V:
            return (ARC_ConsoleKey){ (int32_t)'v' };

        case ARC_CONSOLE_KEY_W:
            return (ARC_ConsoleKey){ (int32_t)'w' };

        case ARC_CONSOLE_KEY_X:
            return (ARC_ConsoleKey){ (int32_t)'x' };

        case ARC_CONSOLE_KEY_Y:
            return (ARC_ConsoleKey){ (int32_t)'y' };

        case ARC_CONSOLE_KEY_Z:
            return (ARC_ConsoleKey){ (int32_t)'z' };

        case ARC_CONSOLE_KEY_0:
            return (ARC_ConsoleKey){ (int32_t)'0' };

        case ARC_CONSOLE_KEY_1:
            return (ARC_ConsoleKey){ (int32_t)'1' };

        case ARC_CONSOLE_KEY_2:
            return (ARC_ConsoleKey){ (int32_t)'2' };

        case ARC_CONSOLE_KEY_3:
            return (ARC_ConsoleKey){ (int32_t)'3' };

        case ARC_CONSOLE_KEY_4:
            return (ARC_ConsoleKey){ (int32_t)'4' };

        case ARC_CONSOLE_KEY_5:
            return (ARC_ConsoleKey){ (int32_t)'5' };

        case ARC_CONSOLE_KEY_6:
            return (ARC_ConsoleKey){ (int32_t)'6' };

        case ARC_CONSOLE_KEY_7:
            return (ARC_ConsoleKey){ (int32_t)'7' };

        case ARC_CONSOLE_KEY_8:
            return (ARC_ConsoleKey){ (int32_t)'8' };

        case ARC_CONSOLE_KEY_9:
            return (ARC_ConsoleKey){ (int32_t)'9' };

        case ARC_CONSOLE_KEY_UP:
            return (ARC_ConsoleKey){ KEY_UP };

        case ARC_CONSOLE_KEY_DOWN:
            return (ARC_ConsoleKey){ KEY_DOWN };

        case ARC_CONSOLE_KEY_LEFT:
            return (ARC_ConsoleKey){ KEY_LEFT };

        case ARC_CONSOLE_KEY_RIGHT:
            return (ARC_ConsoleKey){ KEY_RIGHT };

        case ARC_CONSOLE_KEY_FORWARD_SLASH:
            return (ARC_ConsoleKey){ (int32_t)'/' };

        case ARC_CONSOLE_KEY_BACKSPACE:
            return (ARC_ConsoleKey){ KEY_BACKSPACE };

        //TODO: This should probs be KEY_ENTER, but idk why it wasn't working
        case ARC_CONSOLE_KEY_ENTER:
            return (ARC_ConsoleKey){ 10 };

        //TODO: This is escape and alt, need to fix
        case ARC_CONSOLE_KEY_ESC:
            return (ARC_ConsoleKey){ 27 };

        default:
            return (ARC_ConsoleKey){ 0 };
    }
}

uint8_t ARC_ConsoleKey_GetCharFromKey(ARC_ConsoleKey *consoleKey){
    return consoleKey->key;
}
