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

#include "JHybridViewModelListPropertySpec.hpp"

// Forward declaration of `HybridViewModelInstanceSpec` to properly resolve imports.
namespace margelo::nitro::rive { class HybridViewModelInstanceSpec; }

#include <memory>
#include "HybridViewModelInstanceSpec.hpp"
#include <optional>
#include "JHybridViewModelInstanceSpec.hpp"
#include <NitroModules/Promise.hpp>
#include <NitroModules/JPromise.hpp>
#include <NitroModules/JUnit.hpp>
#include <functional>
#include "JFunc_void.hpp"
#include <NitroModules/JNICallable.hpp>

namespace margelo::nitro::rive {

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

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

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

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

  // Properties
  double JHybridViewModelListPropertySpec::getLength() {
    static const auto method = _javaPart->javaClassStatic()->getMethod<double()>("getLength");
    auto __result = method(_javaPart);
    return __result;
  }

  // Methods
  std::optional<std::shared_ptr<HybridViewModelInstanceSpec>> JHybridViewModelListPropertySpec::getInstanceAt(double index) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JHybridViewModelInstanceSpec::JavaPart>(double /* index */)>("getInstanceAt");
    auto __result = method(_javaPart, index);
    return __result != nullptr ? std::make_optional(__result->getJHybridViewModelInstanceSpec()) : std::nullopt;
  }
  std::shared_ptr<Promise<double>> JHybridViewModelListPropertySpec::getLengthAsync() {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>()>("getLengthAsync");
    auto __result = method(_javaPart);
    return [&]() {
      auto __promise = Promise<double>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<jni::JDouble>(__boxedResult);
        __promise->resolve(__result->value());
      });
      __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::optional<std::shared_ptr<HybridViewModelInstanceSpec>>>> JHybridViewModelListPropertySpec::getInstanceAtAsync(double index) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(double /* index */)>("getInstanceAtAsync");
    auto __result = method(_javaPart, index);
    return [&]() {
      auto __promise = Promise<std::optional<std::shared_ptr<HybridViewModelInstanceSpec>>>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<JHybridViewModelInstanceSpec::JavaPart>(__boxedResult);
        __promise->resolve(__result != nullptr ? std::make_optional(__result->getJHybridViewModelInstanceSpec()) : std::nullopt);
      });
      __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
        jni::JniException __jniError(__throwable);
        __promise->reject(std::make_exception_ptr(__jniError));
      });
      return __promise;
    }();
  }
  void JHybridViewModelListPropertySpec::addInstance(const std::shared_ptr<HybridViewModelInstanceSpec>& instance) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<void(jni::alias_ref<JHybridViewModelInstanceSpec::JavaPart> /* instance */)>("addInstance");
    method(_javaPart, std::dynamic_pointer_cast<JHybridViewModelInstanceSpec>(instance)->getJavaPart());
  }
  bool JHybridViewModelListPropertySpec::addInstanceAt(const std::shared_ptr<HybridViewModelInstanceSpec>& instance, double index) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jboolean(jni::alias_ref<JHybridViewModelInstanceSpec::JavaPart> /* instance */, double /* index */)>("addInstanceAt");
    auto __result = method(_javaPart, std::dynamic_pointer_cast<JHybridViewModelInstanceSpec>(instance)->getJavaPart(), index);
    return static_cast<bool>(__result);
  }
  void JHybridViewModelListPropertySpec::removeInstance(const std::shared_ptr<HybridViewModelInstanceSpec>& instance) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<void(jni::alias_ref<JHybridViewModelInstanceSpec::JavaPart> /* instance */)>("removeInstance");
    method(_javaPart, std::dynamic_pointer_cast<JHybridViewModelInstanceSpec>(instance)->getJavaPart());
  }
  void JHybridViewModelListPropertySpec::removeInstanceAt(double index) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<void(double /* index */)>("removeInstanceAt");
    method(_javaPart, index);
  }
  bool JHybridViewModelListPropertySpec::swap(double index1, double index2) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jboolean(double /* index1 */, double /* index2 */)>("swap");
    auto __result = method(_javaPart, index1, index2);
    return static_cast<bool>(__result);
  }
  std::shared_ptr<Promise<void>> JHybridViewModelListPropertySpec::addInstanceAsync(const std::shared_ptr<HybridViewModelInstanceSpec>& instance) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<JHybridViewModelInstanceSpec::JavaPart> /* instance */)>("addInstanceAsync");
    auto __result = method(_javaPart, std::dynamic_pointer_cast<JHybridViewModelInstanceSpec>(instance)->getJavaPart());
    return [&]() {
      auto __promise = Promise<void>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& /* unit */) {
        __promise->resolve();
      });
      __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<void>> JHybridViewModelListPropertySpec::addInstanceAtAsync(const std::shared_ptr<HybridViewModelInstanceSpec>& instance, double index) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<JHybridViewModelInstanceSpec::JavaPart> /* instance */, double /* index */)>("addInstanceAtAsync");
    auto __result = method(_javaPart, std::dynamic_pointer_cast<JHybridViewModelInstanceSpec>(instance)->getJavaPart(), index);
    return [&]() {
      auto __promise = Promise<void>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& /* unit */) {
        __promise->resolve();
      });
      __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<void>> JHybridViewModelListPropertySpec::removeInstanceAsync(const std::shared_ptr<HybridViewModelInstanceSpec>& instance) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<JHybridViewModelInstanceSpec::JavaPart> /* instance */)>("removeInstanceAsync");
    auto __result = method(_javaPart, std::dynamic_pointer_cast<JHybridViewModelInstanceSpec>(instance)->getJavaPart());
    return [&]() {
      auto __promise = Promise<void>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& /* unit */) {
        __promise->resolve();
      });
      __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<void>> JHybridViewModelListPropertySpec::removeInstanceAtAsync(double index) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(double /* index */)>("removeInstanceAtAsync");
    auto __result = method(_javaPart, index);
    return [&]() {
      auto __promise = Promise<void>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& /* unit */) {
        __promise->resolve();
      });
      __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<void>> JHybridViewModelListPropertySpec::swapAsync(double index1, double index2) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(double /* index1 */, double /* index2 */)>("swapAsync");
    auto __result = method(_javaPart, index1, index2);
    return [&]() {
      auto __promise = Promise<void>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& /* unit */) {
        __promise->resolve();
      });
      __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
        jni::JniException __jniError(__throwable);
        __promise->reject(std::make_exception_ptr(__jniError));
      });
      return __promise;
    }();
  }
  std::function<void()> JHybridViewModelListPropertySpec::addListener(const std::function<void()>& onChanged) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JFunc_void::javaobject>(jni::alias_ref<JFunc_void::javaobject> /* onChanged */)>("addListener_cxx");
    auto __result = method(_javaPart, JFunc_void_cxx::fromCpp(onChanged));
    return [&]() -> std::function<void()> {
      if (__result->isInstanceOf(JFunc_void_cxx::javaClassStatic())) [[likely]] {
        auto downcast = jni::static_ref_cast<JFunc_void_cxx::javaobject>(__result);
        return downcast->cthis()->getFunction();
      } else {
        auto __resultRef = jni::make_global(__result);
        return JNICallable<JFunc_void, void()>(std::move(__resultRef));
      }
    }();
  }
  void JHybridViewModelListPropertySpec::removeListeners() {
    static const auto method = _javaPart->javaClassStatic()->getMethod<void()>("removeListeners");
    method(_javaPart);
  }

} // namespace margelo::nitro::rive
