///
/// JNitroRequest.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 "NitroRequest.hpp"

#include "JNitroFormDataPart.hpp"
#include "JNitroHeader.hpp"
#include "JNitroRequestMethod.hpp"
#include "NitroFormDataPart.hpp"
#include "NitroHeader.hpp"
#include "NitroRequestMethod.hpp"
#include <optional>
#include <string>
#include <vector>

namespace margelo::nitro::nitrofetch {

  using namespace facebook;

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

  public:
    /**
     * Convert this Java/Kotlin-based struct to the C++ struct NitroRequest by copying all values to C++.
     */
    [[maybe_unused]]
    [[nodiscard]]
    NitroRequest toCpp() const {
      static const auto clazz = javaClassStatic();
      static const auto fieldUrl = clazz->getField<jni::JString>("url");
      jni::local_ref<jni::JString> url = this->getFieldValue(fieldUrl);
      static const auto fieldMethod = clazz->getField<JNitroRequestMethod>("method");
      jni::local_ref<JNitroRequestMethod> method = this->getFieldValue(fieldMethod);
      static const auto fieldHeaders = clazz->getField<jni::JArrayClass<JNitroHeader>>("headers");
      jni::local_ref<jni::JArrayClass<JNitroHeader>> headers = this->getFieldValue(fieldHeaders);
      static const auto fieldBodyString = clazz->getField<jni::JString>("bodyString");
      jni::local_ref<jni::JString> bodyString = this->getFieldValue(fieldBodyString);
      static const auto fieldBodyBytes = clazz->getField<jni::JString>("bodyBytes");
      jni::local_ref<jni::JString> bodyBytes = this->getFieldValue(fieldBodyBytes);
      static const auto fieldBodyFormData = clazz->getField<jni::JArrayClass<JNitroFormDataPart>>("bodyFormData");
      jni::local_ref<jni::JArrayClass<JNitroFormDataPart>> bodyFormData = this->getFieldValue(fieldBodyFormData);
      static const auto fieldTimeoutMs = clazz->getField<jni::JDouble>("timeoutMs");
      jni::local_ref<jni::JDouble> timeoutMs = this->getFieldValue(fieldTimeoutMs);
      static const auto fieldFollowRedirects = clazz->getField<jni::JBoolean>("followRedirects");
      jni::local_ref<jni::JBoolean> followRedirects = this->getFieldValue(fieldFollowRedirects);
      static const auto fieldPrefetchCacheTtlMs = clazz->getField<jni::JDouble>("prefetchCacheTtlMs");
      jni::local_ref<jni::JDouble> prefetchCacheTtlMs = this->getFieldValue(fieldPrefetchCacheTtlMs);
      static const auto fieldRequestId = clazz->getField<jni::JString>("requestId");
      jni::local_ref<jni::JString> requestId = this->getFieldValue(fieldRequestId);
      return NitroRequest(
        url->toStdString(),
        method != nullptr ? std::make_optional(method->toCpp()) : std::nullopt,
        headers != nullptr ? std::make_optional([&]() {
          size_t __size = headers->size();
          std::vector<NitroHeader> __vector;
          __vector.reserve(__size);
          for (size_t __i = 0; __i < __size; __i++) {
            auto __element = headers->getElement(__i);
            __vector.push_back(__element->toCpp());
          }
          return __vector;
        }()) : std::nullopt,
        bodyString != nullptr ? std::make_optional(bodyString->toStdString()) : std::nullopt,
        bodyBytes != nullptr ? std::make_optional(bodyBytes->toStdString()) : std::nullopt,
        bodyFormData != nullptr ? std::make_optional([&]() {
          size_t __size = bodyFormData->size();
          std::vector<NitroFormDataPart> __vector;
          __vector.reserve(__size);
          for (size_t __i = 0; __i < __size; __i++) {
            auto __element = bodyFormData->getElement(__i);
            __vector.push_back(__element->toCpp());
          }
          return __vector;
        }()) : std::nullopt,
        timeoutMs != nullptr ? std::make_optional(timeoutMs->value()) : std::nullopt,
        followRedirects != nullptr ? std::make_optional(static_cast<bool>(followRedirects->value())) : std::nullopt,
        prefetchCacheTtlMs != nullptr ? std::make_optional(prefetchCacheTtlMs->value()) : std::nullopt,
        requestId != nullptr ? std::make_optional(requestId->toStdString()) : 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<JNitroRequest::javaobject> fromCpp(const NitroRequest& value) {
      using JSignature = JNitroRequest(jni::alias_ref<jni::JString>, jni::alias_ref<JNitroRequestMethod>, jni::alias_ref<jni::JArrayClass<JNitroHeader>>, jni::alias_ref<jni::JString>, jni::alias_ref<jni::JString>, jni::alias_ref<jni::JArrayClass<JNitroFormDataPart>>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JBoolean>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JString>);
      static const auto clazz = javaClassStatic();
      static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
      return create(
        clazz,
        jni::make_jstring(value.url),
        value.method.has_value() ? JNitroRequestMethod::fromCpp(value.method.value()) : nullptr,
        value.headers.has_value() ? [&]() {
          size_t __size = value.headers.value().size();
          jni::local_ref<jni::JArrayClass<JNitroHeader>> __array = jni::JArrayClass<JNitroHeader>::newArray(__size);
          for (size_t __i = 0; __i < __size; __i++) {
            const auto& __element = value.headers.value()[__i];
            auto __elementJni = JNitroHeader::fromCpp(__element);
            __array->setElement(__i, *__elementJni);
          }
          return __array;
        }() : nullptr,
        value.bodyString.has_value() ? jni::make_jstring(value.bodyString.value()) : nullptr,
        value.bodyBytes.has_value() ? jni::make_jstring(value.bodyBytes.value()) : nullptr,
        value.bodyFormData.has_value() ? [&]() {
          size_t __size = value.bodyFormData.value().size();
          jni::local_ref<jni::JArrayClass<JNitroFormDataPart>> __array = jni::JArrayClass<JNitroFormDataPart>::newArray(__size);
          for (size_t __i = 0; __i < __size; __i++) {
            const auto& __element = value.bodyFormData.value()[__i];
            auto __elementJni = JNitroFormDataPart::fromCpp(__element);
            __array->setElement(__i, *__elementJni);
          }
          return __array;
        }() : nullptr,
        value.timeoutMs.has_value() ? jni::JDouble::valueOf(value.timeoutMs.value()) : nullptr,
        value.followRedirects.has_value() ? jni::JBoolean::valueOf(value.followRedirects.value()) : nullptr,
        value.prefetchCacheTtlMs.has_value() ? jni::JDouble::valueOf(value.prefetchCacheTtlMs.value()) : nullptr,
        value.requestId.has_value() ? jni::make_jstring(value.requestId.value()) : nullptr
      );
    }
  };

} // namespace margelo::nitro::nitrofetch
