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

#include "JHybridPlacesSpec.hpp"

// Forward declaration of `PlaceAutocompleteResult` to properly resolve imports.
namespace margelo::nitro::googleplaces { struct PlaceAutocompleteResult; }
// Forward declaration of `PlaceDetails` to properly resolve imports.
namespace margelo::nitro::googleplaces { struct PlaceDetails; }
// Forward declaration of `AddressComponent` to properly resolve imports.
namespace margelo::nitro::googleplaces { struct AddressComponent; }
// Forward declaration of `AutocompleteOptions` to properly resolve imports.
namespace margelo::nitro::googleplaces { struct AutocompleteOptions; }
// Forward declaration of `CircularLocationBounds` to properly resolve imports.
namespace margelo::nitro::googleplaces { struct CircularLocationBounds; }
// Forward declaration of `RectangularLocationBounds` to properly resolve imports.
namespace margelo::nitro::googleplaces { struct RectangularLocationBounds; }
// Forward declaration of `GetPlaceOptions` to properly resolve imports.
namespace margelo::nitro::googleplaces { struct GetPlaceOptions; }

#include "PlaceAutocompleteResult.hpp"
#include <vector>
#include <NitroModules/Promise.hpp>
#include <NitroModules/JPromise.hpp>
#include "JPlaceAutocompleteResult.hpp"
#include <string>
#include <NitroModules/Null.hpp>
#include "PlaceDetails.hpp"
#include <variant>
#include "JVariant_NullType_PlaceDetails.hpp"
#include <NitroModules/JNull.hpp>
#include "JPlaceDetails.hpp"
#include "AddressComponent.hpp"
#include "JAddressComponent.hpp"
#include "AutocompleteOptions.hpp"
#include <optional>
#include "JAutocompleteOptions.hpp"
#include "CircularLocationBounds.hpp"
#include "RectangularLocationBounds.hpp"
#include "JVariant_CircularLocationBounds_RectangularLocationBounds.hpp"
#include "JCircularLocationBounds.hpp"
#include "JRectangularLocationBounds.hpp"
#include "GetPlaceOptions.hpp"
#include "JGetPlaceOptions.hpp"

namespace margelo::nitro::googleplaces {

  jni::local_ref<JHybridPlacesSpec::jhybriddata> JHybridPlacesSpec::initHybrid(jni::alias_ref<jhybridobject> jThis) {
    return makeCxxInstance(jThis);
  }

  void JHybridPlacesSpec::registerNatives() {
    registerHybrid({
      makeNativeMethod("initHybrid", JHybridPlacesSpec::initHybrid),
    });
  }

  size_t JHybridPlacesSpec::getExternalMemorySize() noexcept {
    static const auto method = javaClassStatic()->getMethod<jlong()>("getMemorySize");
    return method(_javaPart);
  }

  bool JHybridPlacesSpec::equals(const std::shared_ptr<HybridObject>& other) {
    if (auto otherCast = std::dynamic_pointer_cast<JHybridPlacesSpec>(other)) {
      return _javaPart == otherCast->_javaPart;
    }
    return false;
  }

  void JHybridPlacesSpec::dispose() noexcept {
    static const auto method = javaClassStatic()->getMethod<void()>("dispose");
    method(_javaPart);
  }

  std::string JHybridPlacesSpec::toString() {
    static const auto method = javaClassStatic()->getMethod<jni::JString()>("toString");
    auto javaString = method(_javaPart);
    return javaString->toStdString();
  }

  // Properties
  

  // Methods
  std::shared_ptr<Promise<std::vector<PlaceAutocompleteResult>>> JHybridPlacesSpec::autocomplete(const std::string& query, const std::optional<AutocompleteOptions>& options) {
    static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* query */, jni::alias_ref<JAutocompleteOptions> /* options */)>("autocomplete");
    auto __result = method(_javaPart, jni::make_jstring(query), options.has_value() ? JAutocompleteOptions::fromCpp(options.value()) : nullptr);
    return [&]() {
      auto __promise = Promise<std::vector<PlaceAutocompleteResult>>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<jni::JArrayClass<JPlaceAutocompleteResult>>(__boxedResult);
        __promise->resolve([&]() {
          size_t __size = __result->size();
          std::vector<PlaceAutocompleteResult> __vector;
          __vector.reserve(__size);
          for (size_t __i = 0; __i < __size; __i++) {
            auto __element = __result->getElement(__i);
            __vector.push_back(__element->toCpp());
          }
          return __vector;
        }());
      });
      __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
        jni::JniException __jniError(__throwable);
        __promise->reject(std::make_exception_ptr(__jniError));
      });
      return __promise;
    }();
  }
  std::shared_ptr<Promise<std::variant<nitro::NullType, PlaceDetails>>> JHybridPlacesSpec::getPlace(const std::string& placeId, const std::optional<GetPlaceOptions>& options) {
    static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* placeId */, jni::alias_ref<JGetPlaceOptions> /* options */)>("getPlace");
    auto __result = method(_javaPart, jni::make_jstring(placeId), options.has_value() ? JGetPlaceOptions::fromCpp(options.value()) : nullptr);
    return [&]() {
      auto __promise = Promise<std::variant<nitro::NullType, PlaceDetails>>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<JVariant_NullType_PlaceDetails>(__boxedResult);
        __promise->resolve(__result->toCpp());
      });
      __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
        jni::JniException __jniError(__throwable);
        __promise->reject(std::make_exception_ptr(__jniError));
      });
      return __promise;
    }();
  }
  std::shared_ptr<Promise<std::vector<PlaceDetails>>> JHybridPlacesSpec::autocompleteWithDetails(const std::string& query, const std::optional<AutocompleteOptions>& options) {
    static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* query */, jni::alias_ref<JAutocompleteOptions> /* options */)>("autocompleteWithDetails");
    auto __result = method(_javaPart, jni::make_jstring(query), options.has_value() ? JAutocompleteOptions::fromCpp(options.value()) : nullptr);
    return [&]() {
      auto __promise = Promise<std::vector<PlaceDetails>>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<jni::JArrayClass<JPlaceDetails>>(__boxedResult);
        __promise->resolve([&]() {
          size_t __size = __result->size();
          std::vector<PlaceDetails> __vector;
          __vector.reserve(__size);
          for (size_t __i = 0; __i < __size; __i++) {
            auto __element = __result->getElement(__i);
            __vector.push_back(__element->toCpp());
          }
          return __vector;
        }());
      });
      __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
        jni::JniException __jniError(__throwable);
        __promise->reject(std::make_exception_ptr(__jniError));
      });
      return __promise;
    }();
  }

} // namespace margelo::nitro::googleplaces
