///
/// JNativeBuffer.hpp
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © Marc Rousavy @ Margelo
///

#pragma once

#include <fbjni/fbjni.h>
#include "NativeBuffer.hpp"

#include "JFunc_void.hpp"
#include <NitroModules/JNICallable.hpp>
#include <functional>

namespace margelo::nitro::camera {

  using namespace facebook;

  /**
   * The C++ JNI bridge between the C++ struct "NativeBuffer" and the the Kotlin data class "NativeBuffer".
   */
  struct JNativeBuffer final: public jni::JavaClass<JNativeBuffer> {
  public:
    static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/camera/NativeBuffer;";

  public:
    /**
     * Convert this Java/Kotlin-based struct to the C++ struct NativeBuffer by copying all values to C++.
     */
    [[maybe_unused]]
    [[nodiscard]]
    NativeBuffer toCpp() const {
      static const auto clazz = javaClassStatic();
      static const auto fieldPointer = clazz->getField<jlong>("pointer");
      jlong pointer = this->getFieldValue(fieldPointer);
      static const auto fieldRelease = clazz->getField<JFunc_void::javaobject>("release");
      jni::local_ref<JFunc_void::javaobject> release = this->getFieldValue(fieldRelease);
      return NativeBuffer(
        static_cast<uint64_t>(pointer),
        [&]() -> std::function<void()> {
          if (release->isInstanceOf(JFunc_void_cxx::javaClassStatic())) [[likely]] {
            auto downcast = jni::static_ref_cast<JFunc_void_cxx::javaobject>(release);
            return downcast->cthis()->getFunction();
          } else {
            auto releaseRef = jni::make_global(release);
            return JNICallable<JFunc_void, void()>(std::move(releaseRef));
          }
        }()
      );
    }

  public:
    /**
     * Create a Java/Kotlin-based struct by copying all values from the given C++ struct to Java.
     */
    [[maybe_unused]]
    static jni::local_ref<JNativeBuffer::javaobject> fromCpp(const NativeBuffer& value) {
      using JSignature = JNativeBuffer(jlong, jni::alias_ref<JFunc_void::javaobject>);
      static const auto clazz = javaClassStatic();
      static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
      return create(
        clazz,
        value.pointer,
        JFunc_void_cxx::fromCpp(value.release)
      );
    }
  };

} // namespace margelo::nitro::camera
