
#include <gdextension_interface.h>
#include <godot_cpp/classes/ref_counted.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/godot.hpp>

using namespace godot;

class Test : public RefCounted {
  GDCLASS(Test, RefCounted);

  int counter = 0;

  static void _bind_methods() {
    ClassDB::bind_method(D_METHOD("increment"), &Test::increment);
    ClassDB::bind_method(D_METHOD("get_counter"), &Test::get_counter);
    ClassDB::bind_method(D_METHOD("set_counter", "value"), &Test::set_counter);
    ClassDB::add_property("Test", PropertyInfo(Variant::INT, "counter"),
                          "set_counter", "get_counter");
  }

public:
  int increment() {
    counter += 1;
    return counter;
  }

  int get_counter() const { return counter; }

  void set_counter(int p_value) { counter = p_value; }
};

extern "C" GDExtensionBool GDE_EXPORT
gol_library_init(GDExtensionInterfaceGetProcAddress get_proc_address,
                 GDExtensionClassLibraryPtr library,
                 GDExtensionInitialization *r_initialization) {

  static GDExtensionBinding::InitObject init(get_proc_address, library,
                                             r_initialization);

  init.register_initializer([](ModuleInitializationLevel p_level) {
    if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
      return;
    }
    ClassDB::register_class<Test>();
  });

  init.register_terminator([](ModuleInitializationLevel /*p_level*/) {
    // no-op
  });

  init.set_minimum_library_initialization_level(
      MODULE_INITIALIZATION_LEVEL_SCENE);
  return init.init();
}
