#ifndef COMMON_H
#define COMMON_H

#include "jsi-helpers.h"
#include <jni.h>
#include <logger.h>
#include <sys/types.h>
#include "pthread.h"
#include <thread>
#include <plug-dream-auth-react-native.h>

using namespace facebook::jsi;
namespace jsi = facebook::jsi;

using namespace std;

static jsi::Value getDreamAuthPluginSync(
        jsi::Runtime &runtime, const jsi::Value &thisValue, const jsi::Value *arguments,
        std::size_t count) {
    jsi::Value retVal = executeJNIFunction(runtime, thisValue, arguments, count,
                                           "getDreamAuthPluginSync",
                                           "()Ljava/lang/Boolean;",
                                           false);
    return retVal;
}

// Installing Methods in JSI Runtime
void install(Runtime &jsiRuntime)
{
  Object module = Object(jsiRuntime);
  registerCxxFunction(jsiRuntime,module, "getDreamAuthPluginSync", 0, getDreamAuthPluginSync);
  jsiRuntime.global().setProperty(jsiRuntime, "PluggerDreamAuthJSIModule", std::move(module));

}


extern "C"
JNIEXPORT void JNICALL
Java_com_dreamauthlogin_LoginWithDAuthModule_nativeInstall(JNIEnv *env, jobject thiz,
                                                           jlong jsi) {
    auto runtime = reinterpret_cast<jsi::Runtime *>(jsi);
    if (runtime)
    {
        LOGI("invoking plugdreamauthreactnative::install");
        plugdreamauthreactnative::install(*runtime);
        install(*runtime);
    }
    env->GetJavaVM(&java_vm);
    java_object = env->NewGlobalRef(thiz);
}

#endif /* COMMON_H */
