#include "keydriver.h"
#include "print.h" // Make sure you have a print function available


// Define the keyboard buffer and index
static char keyboard_buffer[KEYBOARD_BUFFER_SIZE];
static unsigned int buffer_index = 0;
static int shift_pressed = 0;

// Updated ASCII conversion table (scan code to ASCII)
const char ascii_table[128] =
{
    0,  27, '1', '2', '3', '4', '5', '6', '7', '8', // 9
  '9', '0', '-', '=', '\b', // Backspace
  '\t', // Tab
  'q', 'w', 'e', 'r', // 19
  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', // Enter key
    0, // 29   - Control
  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', // 39
 '\'', '`',   0, // Left shift
 '\\', 'z', 'x', 'c', 'v', 'b', 'n', // 49
  'm', ',', '.', '/',   0, // Right shift
  '*',
    0, // Alt
  ' ', // Space bar
    0, // Caps lock
    0, // 59 - F1 key ... >
    0,   0,   0,   0,   0,   0,   0,   0,
    0, // < ... F10
    0, // 69 - Num lock
    0, // Scroll Lock
    0, // Home key
    0, // Up Arrow
    0, // Page Up
  '-',
    0, // Left Arrow
    0,
    0, // Right Arrow
  '+',
    0, // 79 - End key
    0, // Down Arrow
    0, // Page Down
    0, // Insert Key
    0, // Delete Key
    0,   0,   0,
    0, // F11 Key
    0, // F12 Key
    0, // All other keys are undefined
};

// Uppercase ASCII conversion table (shift key pressed)
const char shift_ascii_table[128] =
{
    0,  27, '!', '@', '#', '$', '%', '^', '&', '*', // 9
  '(', ')', '_', '+', '\b', // Backspace
  '\t', // Tab
  'Q', 'W', 'E', 'R', // 19
  'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', // Enter key
    0, // 29   - Control
  'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', // 39
 '"', '~',   0, // Left shift
 '|', 'Z', 'X', 'C', 'V', 'B', 'N', // 49
  'M', '<', '>', '?',   0, // Right shift
  '*',
    0, // Alt
  ' ', // Space 
    0, // Caps lock
    0, // 59 - F1 key ... >
    0,   0,   0,   0,   0,   0,   0,   0,
    0, // < ... F10
    0, // 69 - Num lock
    0, // Scroll Lock
    0, // Home key
    0, // Up Arrow
    0, // Page Up
  '-',
    0, // Left Arrow
    0,
    0, // Right Arrow
  '+',
    0, // 79 - End key
    0, // Down Arrow
    0, // Page Down
    0, // Insert Key
    0, // Delete Key
    0,   0,   0,
    0, // F11 Key
    0, // F12 Key
    0, // All other keys
};

// Initialize the keyboard driver
void init_keyboard_driver() {
    print_set_color(PRINT_COLOR_LIGHT_GRAY, PRINT_COLOR_BLACK);
    print_str("[ init ] KEYDRIVER\n");
    
    buffer_index = 0;
    shift_pressed = 0;

    print_set_color(PRINT_COLOR_GREEN, PRINT_COLOR_BLACK);
    print_str("[ OK ] KEYDRIVER\n");
}

// Read a single character from the keyboard
char read_key() {
    char key = 0;

    // Read a key from port 0x60
    __asm__("inb $0x60, %0" : "=a"(key));

    // Check for key release (scan codes 0x80 and above are typically releases)
    if (key & 0x80) {
        // Handle key release
        key &= 0x7F; // Remove the high bit to get the original scan code
        if (key == 0x2A || key == 0x36) { // Left or right shift
            shift_pressed = 0;
        }
        return 0;
    } else {
        // Handle key press
        if (key == 0x2A || key == 0x36) { // Left or right shift
            shift_pressed = 1;
            return 0;
        }

        // Convert scan code to ASCII
        if (key < 128) {
            if (shift_pressed) {
                key = shift_ascii_table[(unsigned char)key];
            } else {
                key = ascii_table[(unsigned char)key];
            }
        }

        // Store the key in the buffer if there's space
        if (buffer_index < KEYBOARD_BUFFER_SIZE) {
            keyboard_buffer[buffer_index++] = key;
        }

        return key;
    }
}

// Get the current buffer index
unsigned int get_buffer_index() {
    return buffer_index;
}

// Retrieve the contents of the keyboard buffer
const char* get_keyboard_buffer() {
    return keyboard_buffer;
}
