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

#include "JHybridOneSecureStoreSpec.hpp"

// Forward declaration of `SecureStoreOptions` to properly resolve imports.
namespace margelo::nitro::one { struct SecureStoreOptions; }
// Forward declaration of `SecureStoreAccessibility` to properly resolve imports.
namespace margelo::nitro::one { enum class SecureStoreAccessibility; }

#include <string>
#include <optional>
#include <NitroModules/Promise.hpp>
#include <NitroModules/JPromise.hpp>
#include <NitroModules/JUnit.hpp>
#include "SecureStoreOptions.hpp"
#include "JSecureStoreOptions.hpp"
#include "SecureStoreAccessibility.hpp"
#include "JSecureStoreAccessibility.hpp"

namespace margelo::nitro::one {

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

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

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

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

  // Properties
  

  // Methods
  std::shared_ptr<Promise<std::optional<std::string>>> JHybridOneSecureStoreSpec::getItem(const std::string& key, const SecureStoreOptions& options) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* key */, jni::alias_ref<JSecureStoreOptions> /* options */)>("getItem");
    auto __result = method(_javaPart, jni::make_jstring(key), JSecureStoreOptions::fromCpp(options));
    return [&]() {
      auto __promise = Promise<std::optional<std::string>>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<jni::JString>(__boxedResult);
        __promise->resolve(__result != nullptr ? std::make_optional(__result->toStdString()) : 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;
    }();
  }
  std::shared_ptr<Promise<void>> JHybridOneSecureStoreSpec::setItem(const std::string& key, const std::string& value, const SecureStoreOptions& options) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* key */, jni::alias_ref<jni::JString> /* value */, jni::alias_ref<JSecureStoreOptions> /* options */)>("setItem");
    auto __result = method(_javaPart, jni::make_jstring(key), jni::make_jstring(value), JSecureStoreOptions::fromCpp(options));
    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>> JHybridOneSecureStoreSpec::deleteItem(const std::string& key, const SecureStoreOptions& options) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<jni::JString> /* key */, jni::alias_ref<JSecureStoreOptions> /* options */)>("deleteItem");
    auto __result = method(_javaPart, jni::make_jstring(key), JSecureStoreOptions::fromCpp(options));
    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::optional<std::string> JHybridOneSecureStoreSpec::getItemSync(const std::string& key, const SecureStoreOptions& options) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<jni::JString>(jni::alias_ref<jni::JString> /* key */, jni::alias_ref<JSecureStoreOptions> /* options */)>("getItemSync");
    auto __result = method(_javaPart, jni::make_jstring(key), JSecureStoreOptions::fromCpp(options));
    return __result != nullptr ? std::make_optional(__result->toStdString()) : std::nullopt;
  }
  void JHybridOneSecureStoreSpec::setItemSync(const std::string& key, const std::string& value, const SecureStoreOptions& options) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* key */, jni::alias_ref<jni::JString> /* value */, jni::alias_ref<JSecureStoreOptions> /* options */)>("setItemSync");
    method(_javaPart, jni::make_jstring(key), jni::make_jstring(value), JSecureStoreOptions::fromCpp(options));
  }
  void JHybridOneSecureStoreSpec::deleteItemSync(const std::string& key, const SecureStoreOptions& options) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* key */, jni::alias_ref<JSecureStoreOptions> /* options */)>("deleteItemSync");
    method(_javaPart, jni::make_jstring(key), JSecureStoreOptions::fromCpp(options));
  }

} // namespace margelo::nitro::one
