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

#include "JHybridOneBatterySpec.hpp"

// Forward declaration of `BatteryState` to properly resolve imports.
namespace margelo::nitro::one { enum class BatteryState; }
// Forward declaration of `BatteryPowerState` to properly resolve imports.
namespace margelo::nitro::one { struct BatteryPowerState; }
// Forward declaration of `BatteryLevelEvent` to properly resolve imports.
namespace margelo::nitro::one { struct BatteryLevelEvent; }
// Forward declaration of `BatteryStateEvent` to properly resolve imports.
namespace margelo::nitro::one { struct BatteryStateEvent; }
// Forward declaration of `BatteryPowerModeEvent` to properly resolve imports.
namespace margelo::nitro::one { struct BatteryPowerModeEvent; }

#include <NitroModules/Promise.hpp>
#include <NitroModules/JPromise.hpp>
#include "BatteryState.hpp"
#include "JBatteryState.hpp"
#include "BatteryPowerState.hpp"
#include "JBatteryPowerState.hpp"
#include <functional>
#include "JFunc_void.hpp"
#include <NitroModules/JNICallable.hpp>
#include "BatteryLevelEvent.hpp"
#include "JFunc_void_BatteryLevelEvent.hpp"
#include "JBatteryLevelEvent.hpp"
#include "BatteryStateEvent.hpp"
#include "JFunc_void_BatteryStateEvent.hpp"
#include "JBatteryStateEvent.hpp"
#include "BatteryPowerModeEvent.hpp"
#include "JFunc_void_BatteryPowerModeEvent.hpp"
#include "JBatteryPowerModeEvent.hpp"

namespace margelo::nitro::one {

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

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

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

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

  // Properties
  

  // Methods
  std::shared_ptr<Promise<double>> JHybridOneBatterySpec::getLevel() {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>()>("getLevel");
    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<BatteryState>> JHybridOneBatterySpec::getState() {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>()>("getState");
    auto __result = method(_javaPart);
    return [&]() {
      auto __promise = Promise<BatteryState>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<JBatteryState>(__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<BatteryPowerState>> JHybridOneBatterySpec::getPowerState() {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>()>("getPowerState");
    auto __result = method(_javaPart);
    return [&]() {
      auto __promise = Promise<BatteryPowerState>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<JBatteryPowerState>(__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<bool>> JHybridOneBatterySpec::isLowPowerModeEnabled() {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>()>("isLowPowerModeEnabled");
    auto __result = method(_javaPart);
    return [&]() {
      auto __promise = Promise<bool>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<jni::JBoolean>(__boxedResult);
        __promise->resolve(static_cast<bool>(__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;
    }();
  }
  bool JHybridOneBatterySpec::isAvailable() {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jboolean()>("isAvailable");
    auto __result = method(_javaPart);
    return static_cast<bool>(__result);
  }
  std::function<void()> JHybridOneBatterySpec::addLevelListener(const std::function<void(const BatteryLevelEvent& /* event */)>& listener) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JFunc_void::javaobject>(jni::alias_ref<JFunc_void_BatteryLevelEvent::javaobject> /* listener */)>("addLevelListener_cxx");
    auto __result = method(_javaPart, JFunc_void_BatteryLevelEvent_cxx::fromCpp(listener));
    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));
      }
    }();
  }
  std::function<void()> JHybridOneBatterySpec::addStateListener(const std::function<void(const BatteryStateEvent& /* event */)>& listener) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JFunc_void::javaobject>(jni::alias_ref<JFunc_void_BatteryStateEvent::javaobject> /* listener */)>("addStateListener_cxx");
    auto __result = method(_javaPart, JFunc_void_BatteryStateEvent_cxx::fromCpp(listener));
    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));
      }
    }();
  }
  std::function<void()> JHybridOneBatterySpec::addLowPowerModeListener(const std::function<void(const BatteryPowerModeEvent& /* event */)>& listener) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JFunc_void::javaobject>(jni::alias_ref<JFunc_void_BatteryPowerModeEvent::javaobject> /* listener */)>("addLowPowerModeListener_cxx");
    auto __result = method(_javaPart, JFunc_void_BatteryPowerModeEvent_cxx::fromCpp(listener));
    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));
      }
    }();
  }

} // namespace margelo::nitro::one
