#ifndef HANDLERS_HPP
#define HANDLERS_HPP

#include <sys/stat.h>
#include <sys/dirent.h>
#include <unistd.h>

struct descriptor; 
struct Result;

typedef Result (*func_t)(descriptor*);

enum arg_t { argINT_t, argFLOAT_t, argSTRING_t };

struct arg {
    arg_t type;
    union {
        int valueINT;
        float valueFLOAT;
        char valueSTRING[32];
    } value;
};

struct descriptor
{
    char name[32];
    arg args[8];
    union {
        int int_v;
        float float_v;
        char string_v[32];
    } ret;
    func_t handler;
};

struct function_file {
    char name[32];
    func_t handler;
    descriptor descriptors;
};

struct Result {
    int code;
    descriptor data;
};

class Handler_vfs {
    public:
        char path[32];

        Handler_vfs(const char *path);
        ~Handler_vfs();
        int link_function(const char *name, func_t func);
        int unlink_function(const char *name);

        int open(const char *name, int flags, int mode);
        static int open_vfs(void* ctx, const char *path, int flags, int mode);
        
        ssize_t read(int fd, void *dst, size_t size);
        static ssize_t read_vfs(void* ctx, int fd, void *dst, size_t size);
        
        ssize_t write(int fd, const void *src, size_t size);
        static ssize_t write_vfs(void* ctx, int fd, const void *src, size_t size);
        
        int close(int fd);
        static int close_vfs(void* ctx, int fd);
        
        int rename(const char *src, const char *dst);
        static int rename_vfs(void* ctx, const char *src, const char *dst);

    private:
        function_file* functions;
        descriptor* descriptors;
        int count_fn = 0;
        int count_ds = 0;
        int capacity_fn = 0;
        int capacity_ds = 0;
        static Handler_vfs* instance;
};

#endif