#include <stdio.h>
#include <string.h>

#include "boot/answer_code.hpp"
#include "drivers/fs/handlers.hpp"
#include "drivers/fs/fs.hpp"

Handler_vfs boot_handler("/boot");

Handler_vfs::Handler_vfs(const char *path) {
    strcpy(this->path, path);
    count_fn = 0;
    count_ds = 0;
    capacity_fn = 10;
    capacity_ds = 10;
    functions = (function_file*)malloc(capacity_fn * sizeof(function_file));
    descriptors = (descriptor*)malloc(capacity_ds * sizeof(descriptor));
};

Handler_vfs::~Handler_vfs() {
    if (functions != nullptr) {
        free(functions);
    }
}

int Handler_vfs::link_function(const char *name, func_t func) {
    if (count_fn + 1 > capacity_fn) {
        int new_capacity = capacity_fn + 10;
        size_t new_size = new_capacity * sizeof(function_file);
        
        function_file* temp = (function_file*)realloc(functions, new_size);
        
        if (temp != nullptr) {
            functions = temp;
            capacity_fn = new_capacity;
        } else {
            return -1;
        }
    }

    strncpy(functions[count_fn].name, name, 32);
    functions[count_fn].handler = func;

    count_fn++;

    return 0;
}
int Handler_vfs::unlink_function(const char *name) {
    int index = -1;
    for (int i = 0; i < count_fn; i++) {
        if (strcmp(functions[i].name, name) == 0) {
            index = i;
            break;
        }
    }

    if (index == -1) return -1;

    for (int i = index; i < count_fn - 1; i++) {
        functions[i] = functions[i + 1];
    }

    count_fn--;

    if (capacity_fn - count_fn >= 15 && capacity_fn > 10) {
        int new_capacity = capacity_fn - 10;
        size_t new_size = new_capacity * sizeof(function_file);
        
        function_file* temp = (function_file*)realloc(functions, new_size);
        if (temp != nullptr || new_size == 0) {
            functions = temp;
            capacity_fn = new_capacity;
        }
    }

    return 0;
}

int Handler_vfs::open(const char *name, int flags, int mode) {
    int fn_idx = -1;
    for (int i = 0; i < count_fn; i++) {
        if (strcmp(functions[i].name, name) == 0) { { fn_idx = i; break; } }
    }
    if (fn_idx == -1) return OS_ERR_NOT_FOUND;

    int target_fd = -1;
    for (int i = 0; i < count_ds; i++) {
        if (descriptors[i].handler == nullptr) { 
            target_fd = i; 
            break; 
        }
    }

    if (target_fd == -1) {
        if (count_ds + 1 > capacity_ds) {
            int new_capacity = capacity_ds + 10;
            descriptor* temp = (descriptor*)realloc(descriptors, new_capacity * sizeof(descriptor));
            if (temp == nullptr) return OS_ERR_NO_MEMORY;
            descriptors = temp;
            capacity_ds = new_capacity;
        }
        target_fd = count_ds;
        count_ds++;
    }

    memset(descriptors[target_fd].name, 0, 32);
    strncpy(descriptors[target_fd].name, name, 31);
    descriptors[target_fd].handler = functions[fn_idx].handler;

    return target_fd;
}
int Handler_vfs::open_vfs(void* ctx, const char *path, int flags, int mode) {
    Handler_vfs* self = (Handler_vfs*)ctx;
    return self->open(path, flags, mode);
}

ssize_t Handler_vfs::read(int fd, void *dst, size_t size) {

    if (fd < 0 || fd >= count_ds || descriptors[fd].handler == nullptr) return OS_ERR_NOT_FOUND; 
    if (dst == nullptr) return OS_ERR_NOT_FOUND;

    if (size < sizeof(Result)) return OS_ERR_NO_MEMORY;

    Result res = descriptors[fd].handler(&descriptors[fd]);

    memcpy(dst, &res, sizeof(res));
    
    return sizeof(res);
}

ssize_t Handler_vfs::read_vfs(void* ctx, int fd, void *dst, size_t size) {
    Handler_vfs* self = (Handler_vfs*)ctx;
    return self->read(fd, dst, size);
}

ssize_t Handler_vfs::write(int fd, const void *src, size_t size) {
    //printf("Handler write [%d]\n", fd);
    return 0;
}
ssize_t Handler_vfs::write_vfs(void* ctx, int fd, const void *src, size_t size) {
    Handler_vfs* self = (Handler_vfs*)ctx;
    return self->write(fd, src, size);
}

int Handler_vfs::close(int fd) {
    if (fd < 0 || fd >= count_ds) return OS_ERR_INVALID_ARG;
    descriptors[fd].handler = nullptr;
    return 0;
}
int Handler_vfs::close_vfs(void* ctx, int fd) {
    Handler_vfs* self = (Handler_vfs*)ctx;
    return self->close(fd);
}

int Handler_vfs::rename(const char *src, const char *dst) {
    printf("Handler rename [%s] [%s]\n", src, dst);
    return 0;
}
int Handler_vfs::rename_vfs(void* ctx, const char *src, const char *dst) {
    Handler_vfs* self = (Handler_vfs*)ctx;
    return self->rename(src, dst);
}