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

#include "JHybridOneMapServicesSpec.hpp"

// Forward declaration of `MapPlace` to properly resolve imports.
namespace margelo::nitro::one { struct MapPlace; }
// Forward declaration of `MapCoordinate` to properly resolve imports.
namespace margelo::nitro::one { struct MapCoordinate; }
// Forward declaration of `MapSuggestion` to properly resolve imports.
namespace margelo::nitro::one { struct MapSuggestion; }
// Forward declaration of `MapRoute` to properly resolve imports.
namespace margelo::nitro::one { struct MapRoute; }
// Forward declaration of `MapRouteStep` to properly resolve imports.
namespace margelo::nitro::one { struct MapRouteStep; }
// Forward declaration of `MapTransport` to properly resolve imports.
namespace margelo::nitro::one { enum class MapTransport; }

#include "MapPlace.hpp"
#include <vector>
#include <NitroModules/Promise.hpp>
#include <NitroModules/JPromise.hpp>
#include "JMapPlace.hpp"
#include <string>
#include "MapCoordinate.hpp"
#include "JMapCoordinate.hpp"
#include "MapSuggestion.hpp"
#include "JMapSuggestion.hpp"
#include "MapRoute.hpp"
#include "JMapRoute.hpp"
#include "MapRouteStep.hpp"
#include "JMapRouteStep.hpp"
#include "MapTransport.hpp"
#include "JMapTransport.hpp"

namespace margelo::nitro::one {

  std::shared_ptr<JHybridOneMapServicesSpec> JHybridOneMapServicesSpec::JavaPart::getJHybridOneMapServicesSpec() {
    auto hybridObject = JHybridObject::JavaPart::getJHybridObject();
    auto castHybridObject = std::dynamic_pointer_cast<JHybridOneMapServicesSpec>(hybridObject);
    if (castHybridObject == nullptr) [[unlikely]] {
      throw std::runtime_error("Failed to downcast JHybridObject to JHybridOneMapServicesSpec!");
    }
    return castHybridObject;
  }

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

  std::shared_ptr<JHybridObject> JHybridOneMapServicesSpec::CxxPart::createHybridObject(const jni::local_ref<JHybridObject::JavaPart>& javaPart) {
    auto castJavaPart = jni::dynamic_ref_cast<JHybridOneMapServicesSpec::JavaPart>(javaPart);
    if (castJavaPart == nullptr) [[unlikely]] {
      throw std::runtime_error("Failed to cast JHybridObject::JavaPart to JHybridOneMapServicesSpec::JavaPart!");
    }
    return std::make_shared<JHybridOneMapServicesSpec>(castJavaPart);
  }

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

  // Properties
  

  // Methods
  std::shared_ptr<Promise<std::vector<MapPlace>>> JHybridOneMapServicesSpec::search(const std::string& query, const MapCoordinate& center, double radiusMeters) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* query */, jni::alias_ref<JMapCoordinate> /* center */, double /* radiusMeters */)>("search");
    auto __result = method(_javaPart, jni::make_jstring(query), JMapCoordinate::fromCpp(center), radiusMeters);
    return [&]() {
      auto __promise = Promise<std::vector<MapPlace>>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<jni::JArrayClass<JMapPlace>>(__boxedResult);
        __promise->resolve([&](auto&& __input) {
          size_t __size = __input->size();
          std::vector<MapPlace> __vector;
          __vector.reserve(__size);
          for (size_t __i = 0; __i < __size; __i++) {
            auto __element = __input->getElement(__i);
            __vector.push_back(__element->toCpp());
          }
          return __vector;
        }(__result));
      });
      __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<MapSuggestion>>> JHybridOneMapServicesSpec::autocomplete(const std::string& query, const MapCoordinate& center, double radiusMeters) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* query */, jni::alias_ref<JMapCoordinate> /* center */, double /* radiusMeters */)>("autocomplete");
    auto __result = method(_javaPart, jni::make_jstring(query), JMapCoordinate::fromCpp(center), radiusMeters);
    return [&]() {
      auto __promise = Promise<std::vector<MapSuggestion>>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<jni::JArrayClass<JMapSuggestion>>(__boxedResult);
        __promise->resolve([&](auto&& __input) {
          size_t __size = __input->size();
          std::vector<MapSuggestion> __vector;
          __vector.reserve(__size);
          for (size_t __i = 0; __i < __size; __i++) {
            auto __element = __input->getElement(__i);
            __vector.push_back(__element->toCpp());
          }
          return __vector;
        }(__result));
      });
      __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<MapPlace>> JHybridOneMapServicesSpec::resolveSuggestion(const std::string& id) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* id */)>("resolveSuggestion");
    auto __result = method(_javaPart, jni::make_jstring(id));
    return [&]() {
      auto __promise = Promise<MapPlace>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<JMapPlace>(__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<MapRoute>> JHybridOneMapServicesSpec::directions(const MapCoordinate& origin, const MapCoordinate& destination, MapTransport transport) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<JMapCoordinate> /* origin */, jni::alias_ref<JMapCoordinate> /* destination */, jni::alias_ref<JMapTransport> /* transport */)>("directions");
    auto __result = method(_javaPart, JMapCoordinate::fromCpp(origin), JMapCoordinate::fromCpp(destination), JMapTransport::fromCpp(transport));
    return [&]() {
      auto __promise = Promise<MapRoute>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<JMapRoute>(__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;
    }();
  }

} // namespace margelo::nitro::one
