#include "tstServiceImpl.h"

#ifdef __cplusplus
#define JNICALL0(call, j_env) j_env->call()
#define JNICALL1(call, j_env, v1) j_env->call(v1)
#define JNICALL2(call, j_env, v1, v2) j_env->call(v1, v2)
#define JNICALL3(call, j_env, v1, v2, v3) j_env->call(v1, v2, v3)
#define JNICALL4(call, j_env, v1, v2, v3, v4) j_env->call(v1, v2, v3, v4)
#define JNICALL5(call, j_env, v1, v2, v3, v4, v5) j_env->call(v1, v2, v3, v4, v5)
#else
#define JNICALL0(call, j_env) (*j_env)->call(j_env)
#define JNICALL1(call, j_env, v1) (*j_env)->call(j_env, v1)
#define JNICALL2(call, j_env, v1, v2) (*j_env)->call(j_env, v1, v2)
#define JNICALL3(call, j_env, v1, v2, v3) (*j_env)->call(j_env, v1, v2, v3)
#define JNICALL4(call, j_env, v1, v2, v3, v4) (*j_env)->call(j_env, v1, v2, v3, v4)
#define JNICALL5(call, j_env, v1, v2, v3, v4, v5) (*j_env)->call(j_env, v1, v2, v3, v4, v5)
#endif

jobject glistener;
jobject gfunction;
jmethodID gconstructor;
jclass gconstructor_class;

JNIEXPORT void JNICALL
Java_tstServiceImpl_NaddTstEventListener (JNIEnv *env, jobject obj, jobject listener)
{
  jclass cls = JNICALL1 (GetObjectClass, env, listener);
  glistener = JNICALL1 (NewGlobalRef, env, listener);
  gfunction = JNICALL3 (GetMethodID, env, cls, "processEvent", "(LtstListener;)V");
  gconstructor_class = JNICALL1 (FindClass, env, "tstListenerImpl");
  gconstructor_class = JNICALL1 (NewGlobalRef, env, gconstructor_class);
  gconstructor = JNICALL3 (GetMethodID, env, gconstructor_class, "<init>", "(I)V");
}

JNIEXPORT void JNICALL Java_tstServiceImpl_NinvokeTstListener (JNIEnv *env, jobject obj, jint value)
{
  jobject object = JNICALL3 (NewObject, env, gconstructor_class, gconstructor, value);
  JNICALL3 (CallVoidMethod, env, glistener, gfunction, object);
}
