///
/// JPTSearchResult.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 "PTSearchResult.hpp"

#include <optional>
#include <string>
#include <vector>

namespace margelo::nitro::espprovtoolkit {

  using namespace facebook;

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

  public:
    /**
     * Convert this Java/Kotlin-based struct to the C++ struct PTSearchResult by copying all values to C++.
     */
    [[maybe_unused]]
    [[nodiscard]]
    PTSearchResult toCpp() const {
      static const auto clazz = javaClassStatic();
      static const auto fieldSuccess = clazz->getField<jboolean>("success");
      jboolean success = this->getFieldValue(fieldSuccess);
      static const auto fieldDeviceNames = clazz->getField<jni::JArrayClass<jni::JString>>("deviceNames");
      jni::local_ref<jni::JArrayClass<jni::JString>> deviceNames = this->getFieldValue(fieldDeviceNames);
      static const auto fieldError = clazz->getField<jni::JDouble>("error");
      jni::local_ref<jni::JDouble> error = this->getFieldValue(fieldError);
      return PTSearchResult(
        static_cast<bool>(success),
        deviceNames != nullptr ? std::make_optional([&](auto&& __input) {
          size_t __size = __input->size();
          std::vector<std::string> __vector;
          __vector.reserve(__size);
          for (size_t __i = 0; __i < __size; __i++) {
            auto __element = __input->getElement(__i);
            __vector.push_back(__element->toStdString());
          }
          return __vector;
        }(deviceNames)) : std::nullopt,
        error != nullptr ? std::make_optional(error->value()) : 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<JPTSearchResult::javaobject> fromCpp(const PTSearchResult& value) {
      using JSignature = JPTSearchResult(jboolean, jni::alias_ref<jni::JArrayClass<jni::JString>>, jni::alias_ref<jni::JDouble>);
      static const auto clazz = javaClassStatic();
      static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
      return create(
        clazz,
        value.success,
        value.deviceNames.has_value() ? [&](auto&& __input) {
          size_t __size = __input.size();
          jni::local_ref<jni::JArrayClass<jni::JString>> __array = jni::JArrayClass<jni::JString>::newArray(__size);
          for (size_t __i = 0; __i < __size; __i++) {
            const auto& __element = __input[__i];
            auto __elementJni = jni::make_jstring(__element);
            __array->setElement(__i, *__elementJni);
          }
          return __array;
        }(value.deviceNames.value()) : nullptr,
        value.error.has_value() ? jni::JDouble::valueOf(value.error.value()) : nullptr
      );
    }
  };

} // namespace margelo::nitro::espprovtoolkit
