#include "GameScreen.h"

#include <jni.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>

#include <memory>

extern "C"
{
JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_InitializeNativeLayer( JNIEnv* Env, jobject Obj, jobject Mgr, jint width, jint height, jstring Path );
JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_Draw( JNIEnv* Env, jobject Obj );
JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_UpdatePhysics( JNIEnv* Env, jobject Obj, jfloat Elapsed );
JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_ResetGL( JNIEnv* Env, jobject Obj );
JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_Move( JNIEnv* Env, jobject Obj, jint dX, jint dY );
JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_Zoom( JNIEnv* Env, jobject Obj, jint dSpan );
JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_Fire( JNIEnv* Env, jobject Obj, jobject Mgr, jfloat SliderPos, jint x, jint y );
JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_SetWeapon( JNIEnv* Env, jobject Obj, jint index );
JNIEXPORT int JNICALL Java_com_dubiousoft_sakana_NativeSakana_GetScore( JNIEnv* Env, jobject Obj );
JNIEXPORT bool JNICALL Java_com_dubiousoft_sakana_NativeSakana_SceneFinished( JNIEnv* Env, jobject Obj );
}

std::shared_ptr<GameScreen> pGameScreen;

JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_InitializeNativeLayer( JNIEnv* Env, jobject Obj, jobject Mgr, jint width, jint height, jstring Path )
{
	AAssetManager* AssetManager = AAssetManager_fromJava( Env, Mgr );
	const char* PathStr = Env->GetStringUTFChars( Path, 0 );
	pGameScreen.reset( new GameScreen( *AssetManager, Math::Size( width, height ), PathStr ) );
	Env->ReleaseStringUTFChars( Path, PathStr );
}

JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_Draw( JNIEnv* Env, jobject Obj )
{
	pGameScreen->OnDraw();
}

JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_UpdatePhysics( JNIEnv* Env, jobject Obj, jfloat Elapsed )
{
	pGameScreen->OnPhysics( Elapsed );
}

JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_ResetGL( JNIEnv* Env, jobject Obj )
{
	pGameScreen->OnResetGL();
}

JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_Move( JNIEnv* Env, jobject Obj, jint dX, jint dY )
{
	pGameScreen->OnMoveCamera( Math::Point( dX, dY ) );
}

JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_Zoom( JNIEnv* Env, jobject Obj, jint dSpan )
{
	pGameScreen->OnZoomCamera( dSpan );
}

JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_Fire( JNIEnv* Env, jobject Obj, jobject Mgr, jfloat SliderPos, jint x, jint y )
{
	AAssetManager* AssetManager = AAssetManager_fromJava( Env, Mgr );
	pGameScreen->OnFireBtn( *AssetManager, SliderPos, Math::Point( x, y ) );
}

JNIEXPORT void JNICALL Java_com_dubiousoft_sakana_NativeSakana_SetWeapon( JNIEnv* Env, jobject Obj, jint index )
{
	pGameScreen->OnWeaponSelect( index );
}

JNIEXPORT int JNICALL Java_com_dubiousoft_sakana_NativeSakana_GetScore( JNIEnv* Env, jobject Obj )
{
	return pGameScreen->GetScore();
}

JNIEXPORT bool JNICALL Java_com_dubiousoft_sakana_NativeSakana_SceneFinished( JNIEnv* Env, jobject Obj )
{
	return pGameScreen->SceneIsFinished();
}


