///
/// JLocator.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 "Locator.hpp"

#include "JLocatorLocations.hpp"
#include "JLocatorText.hpp"
#include "LocatorLocations.hpp"
#include "LocatorText.hpp"
#include <optional>
#include <string>

namespace margelo::nitro::readium {

  using namespace facebook;

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

  public:
    /**
     * Convert this Java/Kotlin-based struct to the C++ struct Locator by copying all values to C++.
     */
    [[maybe_unused]]
    [[nodiscard]]
    Locator toCpp() const {
      static const auto clazz = javaClassStatic();
      static const auto fieldHref = clazz->getField<jni::JString>("href");
      jni::local_ref<jni::JString> href = this->getFieldValue(fieldHref);
      static const auto fieldType = clazz->getField<jni::JString>("type");
      jni::local_ref<jni::JString> type = this->getFieldValue(fieldType);
      static const auto fieldTarget = clazz->getField<jni::JDouble>("target");
      jni::local_ref<jni::JDouble> target = this->getFieldValue(fieldTarget);
      static const auto fieldTitle = clazz->getField<jni::JString>("title");
      jni::local_ref<jni::JString> title = this->getFieldValue(fieldTitle);
      static const auto fieldLocations = clazz->getField<JLocatorLocations>("locations");
      jni::local_ref<JLocatorLocations> locations = this->getFieldValue(fieldLocations);
      static const auto fieldText = clazz->getField<JLocatorText>("text");
      jni::local_ref<JLocatorText> text = this->getFieldValue(fieldText);
      return Locator(
        href->toStdString(),
        type->toStdString(),
        target != nullptr ? std::make_optional(target->value()) : std::nullopt,
        title != nullptr ? std::make_optional(title->toStdString()) : std::nullopt,
        locations != nullptr ? std::make_optional(locations->toCpp()) : std::nullopt,
        text != nullptr ? std::make_optional(text->toCpp()) : std::nullopt
      );
    }

  public:
    /**
     * Create a Java/Kotlin-based struct by copying all values from the given C++ struct to Java.
     */
    [[maybe_unused]]
    static jni::local_ref<JLocator::javaobject> fromCpp(const Locator& value) {
      using JSignature = JLocator(jni::alias_ref<jni::JString>, jni::alias_ref<jni::JString>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JString>, jni::alias_ref<JLocatorLocations>, jni::alias_ref<JLocatorText>);
      static const auto clazz = javaClassStatic();
      static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
      return create(
        clazz,
        jni::make_jstring(value.href),
        jni::make_jstring(value.type),
        value.target.has_value() ? jni::JDouble::valueOf(value.target.value()) : nullptr,
        value.title.has_value() ? jni::make_jstring(value.title.value()) : nullptr,
        value.locations.has_value() ? JLocatorLocations::fromCpp(value.locations.value()) : nullptr,
        value.text.has_value() ? JLocatorText::fromCpp(value.text.value()) : nullptr
      );
    }
  };

} // namespace margelo::nitro::readium
