#include "pxl8_io.h"

#include <stdlib.h>
#include <string.h>

#include "pxl8_cart.h"
#include "pxl8_types.h"

static inline char pxl8_to_lower(char c) {
    return (c >= 'A' && c <= 'Z') ? c + 32 : c;
}

pxl8_result pxl8_io_read_file(const char* path, char** content, size_t* size) {
    if (!path || !content || !size) return PXL8_ERROR_NULL_POINTER;

    pxl8_cart* cart = pxl8_get_cart();
    if (cart && pxl8_cart_is_packed(cart)) {
        u8* data = NULL;
        u32 cart_size = 0;
        pxl8_result result = pxl8_cart_read_file(cart, path, &data, &cart_size);
        if (result == PXL8_OK) {
            *content = realloc(data, cart_size + 1);
            if (!*content) {
                pxl8_cart_free_file(data);
                return PXL8_ERROR_OUT_OF_MEMORY;
            }
            (*content)[cart_size] = '\0';
            *size = cart_size;
            return PXL8_OK;
        }
    }

    FILE* file = fopen(path, "rb");
    if (!file) {
        return PXL8_ERROR_FILE_NOT_FOUND;
    }

    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    fseek(file, 0, SEEK_SET);

    if (file_size < 0) {
        fclose(file);
        return PXL8_ERROR_SYSTEM_FAILURE;
    }

    *content = malloc(file_size + 1);
    if (!*content) {
        fclose(file);
        return PXL8_ERROR_OUT_OF_MEMORY;
    }

    size_t bytes_read = fread(*content, 1, file_size, file);
    (*content)[bytes_read] = '\0';
    *size = bytes_read;

    fclose(file);
    return PXL8_OK;
}

pxl8_result pxl8_io_write_file(const char* path, const char* content, size_t size) {
    if (!path || !content) return PXL8_ERROR_NULL_POINTER;
    
    FILE* file = fopen(path, "wb");
    if (!file) {
        return PXL8_ERROR_SYSTEM_FAILURE;
    }
    
    size_t bytes_written = fwrite(content, 1, size, file);
    fclose(file);
    
    return (bytes_written == size) ? PXL8_OK : PXL8_ERROR_SYSTEM_FAILURE;
}

pxl8_result pxl8_io_read_binary_file(const char* path, u8** data, size_t* size) {
    return pxl8_io_read_file(path, (char**)data, size);
}

pxl8_result pxl8_io_write_binary_file(const char* path, const u8* data, size_t size) {
    return pxl8_io_write_file(path, (const char*)data, size);
}

bool pxl8_io_file_exists(const char* path) {
    if (!path) return false;
    
    struct stat st;
    return stat(path, &st) == 0;
}

f64 pxl8_io_get_file_modified_time(const char* path) {
    if (!path) return 0.0;
    
    struct stat st;
    if (stat(path, &st) == 0) {
        return st.st_mtime;
    }
    return 0.0;
}

pxl8_result pxl8_io_create_directory(const char* path) {
    if (!path) return PXL8_ERROR_NULL_POINTER;
    
#ifdef _WIN32
    if (mkdir(path) != 0) {
#else
    if (mkdir(path, 0755) != 0) {
#endif
        return PXL8_ERROR_SYSTEM_FAILURE;
    }
    return PXL8_OK;
}

void pxl8_io_free_file_content(char* content) {
    if (content) {
        free(content);
    }
}

void pxl8_io_free_binary_data(u8* data) {
    if (data) {
        free(data);
    }
}

static i32 pxl8_key_code(const char* key_name) {
    if (!key_name || !key_name[0]) return 0;

    typedef struct { const char* name; i32 code; } KeyMapping;
    static const KeyMapping keys[] = {
        {"a", 4}, {"b", 5}, {"c", 6}, {"d", 7}, {"e", 8}, {"f", 9}, {"g", 10}, {"h", 11},
        {"i", 12}, {"j", 13}, {"k", 14}, {"l", 15}, {"m", 16}, {"n", 17}, {"o", 18}, {"p", 19},
        {"q", 20}, {"r", 21}, {"s", 22}, {"t", 23}, {"u", 24}, {"v", 25}, {"w", 26}, {"x", 27},
        {"y", 28}, {"z", 29},
        {"1", 30}, {"2", 31}, {"3", 32}, {"4", 33}, {"5", 34},
        {"6", 35}, {"7", 36}, {"8", 37}, {"9", 38}, {"0", 39},
        {"return", 40}, {"escape", 41}, {"backspace", 42}, {"tab", 43}, {"space", 44},
        {"-", 45}, {"=", 46}, {"`", 53},
        {"left", 80}, {"right", 79}, {"up", 82}, {"down", 81},
        {"f1", 58}, {"f2", 59}, {"f3", 60}, {"f4", 61}, {"f5", 62}, {"f6", 63},
        {"f7", 64}, {"f8", 65}, {"f9", 66}, {"f10", 67}, {"f11", 68}, {"f12", 69},
        {"capslock", 57}, {"lshift", 225}, {"rshift", 229},
        {"lctrl", 224}, {"rctrl", 228}, {"lalt", 226}, {"ralt", 230},
        {NULL, 0}
    };

    char lower_name[64];
    size_t i;
    for (i = 0; i < sizeof(lower_name) - 1 && key_name[i]; i++) {
        lower_name[i] = pxl8_to_lower(key_name[i]);
    }
    lower_name[i] = '\0';

    for (i = 0; keys[i].name; i++) {
        if (strcmp(lower_name, keys[i].name) == 0) {
            return keys[i].code;
        }
    }

    return 0;
}

bool pxl8_key_down(const pxl8_input_state* input, const char* key_name) {
    if (!input) return false;
    i32 key = pxl8_key_code(key_name);
    if (key < 0 || key >= PXL8_MAX_KEYS) return false;
    return input->keys_down[key];
}

bool pxl8_key_pressed(const pxl8_input_state* input, const char* key_name) {
    if (!input) return false;
    i32 key = pxl8_key_code(key_name);
    if (key < 0 || key >= PXL8_MAX_KEYS) return false;
    return input->keys_pressed[key];
}

bool pxl8_key_released(const pxl8_input_state* input, const char* key_name) {
    if (!input) return false;
    i32 key = pxl8_key_code(key_name);
    if (key < 0 || key >= PXL8_MAX_KEYS) return false;
    return input->keys_released[key];
}

i32 pxl8_mouse_wheel_x(const pxl8_input_state* input) {
    if (!input) return 0;
    return input->mouse_wheel_x;
}

i32 pxl8_mouse_wheel_y(const pxl8_input_state* input) {
    if (!input) return 0;
    return input->mouse_wheel_y;
}

bool pxl8_mouse_pressed(const pxl8_input_state* input, i32 button) {
    if (!input || button < 1 || button > 3) return false;
    return input->mouse_buttons_pressed[button - 1];
}

bool pxl8_mouse_released(const pxl8_input_state* input, i32 button) {
    if (!input || button < 1 || button > 3) return false;
    return input->mouse_buttons_released[button - 1];
}

i32 pxl8_mouse_x(const pxl8_input_state* input) {
    if (!input) return 0;
    return input->mouse_x;
}

i32 pxl8_mouse_y(const pxl8_input_state* input) {
    if (!input) return 0;
    return input->mouse_y;
}

i32 pxl8_mouse_dx(const pxl8_input_state* input) {
    if (!input) return 0;
    return input->mouse_dx;
}

i32 pxl8_mouse_dy(const pxl8_input_state* input) {
    if (!input) return 0;
    return input->mouse_dy;
}
