#ifndef LIBAGENT_H
#define LIBAGENT_H

#include "agent.h"
#include <dlfcn.h>

class Libagent {
public:
  Libagent(void *handle);
  ~Libagent();

  static Libagent *load(const char *base);

  libagent_Agent *new_agent(const char *type, const char *version, libagent_Config *config) {
    typedef libagent_Agent *(*libagent_new_agent_t)(const char *, const char *, libagent_Config *);
    libagent_new_agent_t f = (libagent_new_agent_t)dlsym(handle, "libagent_new_agent");
    return f(type, version, config);
  };
  void close_agent(libagent_Agent *agent) {
    typedef void (*libagent_close_agent_t)(libagent_Agent *);
    libagent_close_agent_t f = (libagent_close_agent_t)dlsym(handle, "libagent_close_agent");
    f(agent);
  };
  libagent_Config *new_config() {
    typedef libagent_Config *(*libagent_new_config_t)();
    libagent_new_config_t f = (libagent_new_config_t)dlsym(handle, "libagent_new_config");
    return f();
  };
  int set_config(libagent_Config *config, const char *field, const char *value) {
    typedef int (*libagent_set_config_t)(libagent_Config *, const char *, const char *);
    libagent_set_config_t f = (libagent_set_config_t)dlsym(handle, "libagent_set_config");
    return f(config, field, value);
  };
  int is_enabled(libagent_Agent *agent) {
    typedef int (*libagent_is_enabled_t)(libagent_Agent *);
    libagent_is_enabled_t f = (libagent_is_enabled_t)dlsym(handle, "libagent_is_enabled");
    return f(agent);
  };
  void log(libagent_Agent *agent, const char *target, const char *file, int line, int level, const char *m) {
    typedef void (*libagent_log_t)(libagent_Agent *, const char *, const char *, int, int, const char *);
    libagent_log_t f = (libagent_log_t)dlsym(handle, "libagent_log");
    f(agent, target, file, line, level, m);
  };
  int is_log_enabled(libagent_Agent *agent, int level) {
    typedef int (*libagent_is_log_enabled_t)(libagent_Agent *, int);
    libagent_is_log_enabled_t f = (libagent_is_log_enabled_t)dlsym(handle, "libagent_is_log_enabled");
    return f(agent, level);
  };
  int is_plugin_enabled(libagent_Agent *agent, const char *plugin) {
    typedef int (*libagent_is_plugin_enabled_t)(libagent_Agent *agent, const char *plugin);
    libagent_is_plugin_enabled_t f = (libagent_is_plugin_enabled_t)dlsym(handle, "libagent_is_plugin_enabled");
    return f(agent, plugin);
  };
  int report(libagent_Agent *agent, const char *type, const char *name, const char *version) {
    typedef int (*libagent_report_t)(libagent_Agent *, const char *, const char *, const char *);
    libagent_report_t f = (libagent_report_t)dlsym(handle, "libagent_report");
    return f(agent, type, name, version);
  };
  int report_plugin(libagent_Agent *agent, const char *name, const char *hooks, const char *status, const char *version) {
    typedef int (*libagent_report_plugin_t)(libagent_Agent *, const char *, const char *, const char *, const char *);
    libagent_report_plugin_t f = (libagent_report_plugin_t)dlsym(handle, "libagent_report_plugin");
    return f(agent, name, hooks, status, version);
  };
  libagent_Transaction *start_transaction(libagent_Agent *agent, const char *uuid) {
    typedef libagent_Transaction *(*libagent_start_transaction_t)(libagent_Agent *, const char *);
    libagent_start_transaction_t f = (libagent_start_transaction_t)dlsym(handle, "libagent_start_transaction");
    return f(agent, uuid);
  };
  void finish_transaction(libagent_Transaction *transaction) {
    typedef void (*libagent_finish_transaction_t)(libagent_Transaction *);
    libagent_finish_transaction_t f = (libagent_finish_transaction_t)dlsym(handle, "libagent_finish_transaction");
    f(transaction);
  };
  const char *transaction_uuid(libagent_Transaction *transaction) {
    typedef const char *(*libagent_transaction_uuid_t)(libagent_Transaction *);
    libagent_transaction_uuid_t f = (libagent_transaction_uuid_t)dlsym(handle, "libagent_transaction_uuid");
    return f(transaction);
  };
  libagent_Table *run_hook(libagent_Transaction *transaction, const char *plugin, const char *hook, libagent_Table *meta) {
    typedef libagent_Table *(*libagent_run_hook_t)(libagent_Transaction *, const char *, const char *, libagent_Table *);
    libagent_run_hook_t f = (libagent_run_hook_t)dlsym(handle, "libagent_run_hook");
    return f(transaction, plugin, hook, meta);
  };
  int has_hook(libagent_Transaction *transaction, const char *hook) {
    typedef int (*libagent_has_hook_t)(libagent_Transaction *, const char *);
    libagent_has_hook_t f = (libagent_has_hook_t)dlsym(handle, "libagent_has_hook");
    return f(transaction, hook);
  };
  int hook_ran(libagent_Transaction *transaction, const char *hook) {
    typedef int (*libagent_hook_ran_t)(libagent_Transaction *, const char *);
    libagent_hook_ran_t f = (libagent_hook_ran_t)dlsym(handle, "libagent_hook_ran");
    return f(transaction, hook);
  };
  libagent_Table *create_array(libagent_Transaction *transaction, size_t array_size) {
    typedef libagent_Table *(*libagent_create_array_t)(libagent_Transaction *, size_t);
    libagent_create_array_t f = (libagent_create_array_t)dlsym(handle, "libagent_create_array");
    return f(transaction, array_size);
  };
  libagent_Table *create_map(libagent_Transaction *transaction, size_t map_size) {
    typedef libagent_Table *(*libagent_create_map_t)(libagent_Transaction *, size_t);
    libagent_create_map_t f = (libagent_create_map_t)dlsym(handle, "libagent_create_map");
    return f(transaction, map_size);
  };
  void drop_table(libagent_Table *table) {
    typedef void (*libagent_drop_table_t)(libagent_Table *);
    libagent_drop_table_t f = (libagent_drop_table_t)dlsym(handle, "libagent_drop_table");
    return f(table);
  };
  void drop_string(const char *str) {
    typedef void (*libagent_drop_string_t)(const char *);
    libagent_drop_string_t f = (libagent_drop_string_t)dlsym(handle, "libagent_drop_string");
    f(str);
  };
  void set_nil(libagent_Table *table, const char *key) {
    typedef void (*libagent_set_nil_t)(libagent_Table *, const char *);
    libagent_set_nil_t f = (libagent_set_nil_t)dlsym(handle, "libagent_set_nil");
    f(table, key);
  };
  void set_boolean(libagent_Table *table, const char *key, int value) {
    typedef void (*libagent_set_boolean_t)(libagent_Table *, const char *, int);
    libagent_set_boolean_t f = (libagent_set_boolean_t)dlsym(handle, "libagent_set_boolean");
    f(table, key, value);
  };
  void set_number(libagent_Table *table, const char *key, double value) {
    typedef void (*libagent_set_number_t)(libagent_Table *, const char *, double);
    libagent_set_number_t f = (libagent_set_number_t)dlsym(handle, "libagent_set_number");
    f(table, key, value);
  };
  void set_table(libagent_Table *table, const char *key, libagent_Table *value) {
    typedef void (*libagent_set_table_t)(libagent_Table *, const char *, libagent_Table *);
    libagent_set_table_t f = (libagent_set_table_t)dlsym(handle, "libagent_set_table");
    f(table, key, value);
  };
  void set_string(libagent_Table *table, const char *key, const char *value, size_t len) {
    typedef void (*libagent_set_string_t)(libagent_Table *, const char *, const char *, size_t);
    libagent_set_string_t f = (libagent_set_string_t)dlsym(handle, "libagent_set_string");
    f(table, key, value, len);
  };
  void seti_nil(libagent_Table *table, int index) {
    typedef void (*libagent_seti_nil_t)(libagent_Table *, int);
    libagent_seti_nil_t f = (libagent_seti_nil_t)dlsym(handle, "libagent_seti_nil");
    f(table, index);
  };
  void seti_boolean(libagent_Table *table, int index, int value) {
    typedef void (*libagent_seti_boolean_t)(libagent_Table *, int, int);
    libagent_seti_boolean_t f = (libagent_seti_boolean_t)dlsym(handle, "libagent_seti_boolean");
    f(table, index, value);
  };
  void seti_number(libagent_Table *table, int index, double value) {
    typedef void (*libagent_seti_number_t)(libagent_Table *, int, double);
    libagent_seti_number_t f = (libagent_seti_number_t)dlsym(handle, "libagent_seti_number");
    f(table, index, value);
  };
  void seti_table(libagent_Table *table, int index, libagent_Table *value) {
    typedef void (*libagent_seti_table_t)(libagent_Table *, int, libagent_Table *);
    libagent_seti_table_t f = (libagent_seti_table_t)dlsym(handle, "libagent_seti_table");
    f(table, index, value);
  };
  void seti_string(libagent_Table *table, int index, const char *value, size_t len) {
    typedef void (*libagent_seti_string_t)(libagent_Table *, int, const char *, size_t);
    libagent_seti_string_t f = (libagent_seti_string_t)dlsym(handle, "libagent_seti_string");
    f(table, index, value, len);
  };

  size_t len(libagent_Table *table) {
    typedef size_t (*libagent_len_t)(libagent_Table *);
    libagent_len_t f = (libagent_len_t)dlsym(handle, "libagent_len");
    return f(table);
  };
  int get_type(libagent_Table *table, const char *key) {
    typedef int (*libagent_get_type_t)(libagent_Table *, const char *);
    libagent_get_type_t f = (libagent_get_type_t)dlsym(handle, "libagent_get_type");
    return f(table, key);
  };
  int geti_type(libagent_Table *table, int index) {
    typedef int (*libagent_geti_type_t)(libagent_Table *table, int);
    libagent_geti_type_t f = (libagent_geti_type_t)dlsym(handle, "libagent_geti_type");
    return f(table, index);
  };
  int get_boolean(libagent_Table *table, const char *key) {
    typedef int (*libagent_get_boolean_t)(libagent_Table *, const char *);
    libagent_get_boolean_t f = (libagent_get_boolean_t)dlsym(handle, "libagent_get_boolean");
    return f(table, key);
  };
  double get_number(libagent_Table *table, const char *key) {
    typedef double (*libagent_get_number_t)(libagent_Table *, const char *);
    libagent_get_number_t f = (libagent_get_number_t)dlsym(handle, "libagent_get_number");
    return f(table, key);
  };
  libagent_Table *get_table(libagent_Table *table, const char *key) {
    typedef libagent_Table *(*libagent_get_table_t)(libagent_Table *, const char *);
    libagent_get_table_t f = (libagent_get_table_t)dlsym(handle, "libagent_get_table");
    return f(table, key);
  };
  const char *get_string(libagent_Table *table, const char *key) {
    typedef const char *(*libagent_get_string_t)(libagent_Table *, const char *);
    libagent_get_string_t f = (libagent_get_string_t)dlsym(handle, "libagent_get_string");
    return f(table, key);
  };
  const char *get_bytes(libagent_Table *table, const char *key, size_t *len) {
    typedef const char *(*libagent_get_bytes_t)(libagent_Table *, const char *, size_t *);
    libagent_get_bytes_t f = (libagent_get_bytes_t)dlsym(handle, "libagent_get_bytes");
    return f(table, key, len);
  };
  int geti_boolean(libagent_Table *table, int index) {
    typedef int (*libagent_geti_boolean_t)(libagent_Table *, int );
    libagent_geti_boolean_t f = (libagent_geti_boolean_t)dlsym(handle, "libagent_geti_boolean");
    return f(table, index);
  };
  double geti_number(libagent_Table *table, int index) {
    typedef double (*libagent_geti_number_t)(libagent_Table *, int);
    libagent_geti_number_t f = (libagent_geti_number_t)dlsym(handle, "libagent_geti_number");
    return f(table, index);
  };
  libagent_Table *geti_table(libagent_Table *table, int index) {
    typedef libagent_Table *(*libagent_geti_table_t)(libagent_Table *, int);
    libagent_geti_table_t f = (libagent_geti_table_t)dlsym(handle, "libagent_geti_table");
    return f(table, index);
  };
  const char *geti_string(libagent_Table *table, int index) {
    typedef const char *(*libagent_geti_string_t)(libagent_Table *, int);
    libagent_geti_string_t f = (libagent_geti_string_t)dlsym(handle, "libagent_geti_string");
    return f(table, index);
  };
  const char *geti_bytes(libagent_Table *table, int index, size_t *len) {
    typedef const char *(*libagent_geti_bytes_t)(libagent_Table *, int, size_t *);
    libagent_geti_bytes_t f = (libagent_geti_bytes_t)dlsym(handle, "libagent_geti_bytes");
    return f(table, index, len);
  };
  const char *debug(libagent_Table *table) {
    typedef const char *(*libagent_debug_t)(libagent_Table *);
    libagent_debug_t f = (libagent_debug_t)dlsym(handle, "libagent_debug");
    return f(table);
  };
  libagent_Timer *new_timer(libagent_Transaction *transaction, const char *kind, const char *name) {
    typedef libagent_Timer *(*libagent_new_timer_t)(libagent_Transaction *, const char *, const char *);
    libagent_new_timer_t f = (libagent_new_timer_t)dlsym(handle, "libagent_new_timer");
    return f(transaction, kind, name);
  };
  void start_timer(libagent_Timer *timer) {
    typedef void (*libagent_start_timer_t)(libagent_Timer *);
    libagent_start_timer_t f = (libagent_start_timer_t)dlsym(handle, "libagent_start_timer");
    f(timer);
  };
  void stop_timer(libagent_Timer *timer) {
    typedef void (*libagent_stop_timer_t)(libagent_Timer *);
    libagent_stop_timer_t f = (libagent_stop_timer_t)dlsym(handle, "libagent_stop_timer");
    f(timer);
  };
  void finish_timer(libagent_Timer *timer, libagent_Transaction *transaction) {
    typedef void (*libagent_finish_timer_t)(libagent_Timer *, libagent_Transaction *);
    libagent_finish_timer_t f = (libagent_finish_timer_t)dlsym(handle, "libagent_finish_timer");
    f(timer, transaction);
  };

private:
  static const char *get_arch();
  static const char *get_base();
  static const char *get_os();

  void *handle;
};

#endif
