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

#pragma once

#include <fbjni/fbjni.h>
#include <functional>

#include <string>
#include <NitroModules/Promise.hpp>
#include "OnGetLicensePayload.hpp"
#include <functional>
#include <NitroModules/JPromise.hpp>
#include "JOnGetLicensePayload.hpp"

namespace margelo::nitro::video {

  using namespace facebook;

  /**
   * Represents the Java/Kotlin callback `(payload: OnGetLicensePayload) -> Promise<Promise<String>>`.
   * This can be passed around between C++ and Java/Kotlin.
   */
  struct JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload: public jni::JavaClass<JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload> {
  public:
    static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/video/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload;";

  public:
    /**
     * Invokes the function this `JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload` instance holds through JNI.
     */
    std::shared_ptr<Promise<std::shared_ptr<Promise<std::string>>>> invoke(const OnGetLicensePayload& payload) const {
      static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<JOnGetLicensePayload> /* payload */)>("invoke");
      auto __result = method(self(), JOnGetLicensePayload::fromCpp(payload));
      return [&]() {
        auto __promise = Promise<std::shared_ptr<Promise<std::string>>>::create();
        __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
          auto __result = jni::static_ref_cast<JPromise::javaobject>(__boxedResult);
          __promise->resolve([&]() {
            auto __promise = Promise<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->toStdString());
            });
            __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
              jni::JniException __jniError(__throwable);
              __promise->reject(std::make_exception_ptr(__jniError));
            });
            return __promise;
          }());
        });
        __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
          jni::JniException __jniError(__throwable);
          __promise->reject(std::make_exception_ptr(__jniError));
        });
        return __promise;
      }();
    }
  };

  /**
   * An implementation of Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload that is backed by a C++ implementation (using `std::function<...>`)
   */
  struct JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload_cxx final: public jni::HybridClass<JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload_cxx, JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload> {
  public:
    static jni::local_ref<JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload::javaobject> fromCpp(const std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::string>>>>(const OnGetLicensePayload& /* payload */)>& func) {
      return JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload_cxx::newObjectCxxArgs(func);
    }

  public:
    /**
     * Invokes the C++ `std::function<...>` this `JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload_cxx` instance holds.
     */
    jni::local_ref<JPromise::javaobject> invoke_cxx(jni::alias_ref<JOnGetLicensePayload> payload) {
      std::shared_ptr<Promise<std::shared_ptr<Promise<std::string>>>> __result = _func(payload->toCpp());
      return [&]() {
        jni::local_ref<JPromise::javaobject> __localPromise = JPromise::create();
        jni::global_ref<JPromise::javaobject> __promise = jni::make_global(__localPromise);
        __result->addOnResolvedListener([=](const std::shared_ptr<Promise<std::string>>& __result) {
          __promise->cthis()->resolve([&]() {
            jni::local_ref<JPromise::javaobject> __localPromise = JPromise::create();
            jni::global_ref<JPromise::javaobject> __promise = jni::make_global(__localPromise);
            __result->addOnResolvedListener([=](const std::string& __result) {
              __promise->cthis()->resolve(jni::make_jstring(__result));
            });
            __result->addOnRejectedListener([=](const std::exception_ptr& __error) {
              auto __jniError = jni::getJavaExceptionForCppException(__error);
              __promise->cthis()->reject(__jniError);
            });
            return __localPromise;
          }());
        });
        __result->addOnRejectedListener([=](const std::exception_ptr& __error) {
          auto __jniError = jni::getJavaExceptionForCppException(__error);
          __promise->cthis()->reject(__jniError);
        });
        return __localPromise;
      }();
    }

  public:
    [[nodiscard]]
    inline const std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::string>>>>(const OnGetLicensePayload& /* payload */)>& getFunction() const {
      return _func;
    }

  public:
    static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/video/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload_cxx;";
    static void registerNatives() {
      registerHybrid({makeNativeMethod("invoke_cxx", JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload_cxx::invoke_cxx)});
    }

  private:
    explicit JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__string_____OnGetLicensePayload_cxx(const std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::string>>>>(const OnGetLicensePayload& /* payload */)>& func): _func(func) { }

  private:
    friend HybridBase;
    std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::string>>>>(const OnGetLicensePayload& /* payload */)> _func;
  };

} // namespace margelo::nitro::video
