#include "android_api.h"

#include "main_code.h"


//#include "boost/shared_ptr.h"

const float CONST_MAXRIX_WIDTH = 800.f;
const float CONST_MAXRIX_HEIGHT = 480.f;

boost::mutex RenderMutex;

JNIEXPORT void JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_Init(JNIEnv * env, jobject obj,  jint width, jint height)
{
	try
	{
	if (App->Inited)
	{
		App->OuterDeinit(); //Clean up what is left at previous launch (if applicable)
	}
	
	App->OuterInit(width, height, CONST_MAXRIX_WIDTH, CONST_MAXRIX_HEIGHT);
	
	App->Inited = true;
	
	}
	catch (ErrorCommon e)
	{
		throw;
	}
}	

JNIEXPORT void JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_StopSounds(JNIEnv * env, jobject obj)
{

}


JNIEXPORT void JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_Update(JNIEnv * env, jobject obj, long dt)
{
	RenderMutex.lock();
	try
	{
	if (App->Inited)
	{
		App->OuterDraw();
		App->OuterUpdate(dt);
	}
	
	}
	catch (...)
	{
		throw;
	}

	RenderMutex.unlock();
}

JNIEXPORT int JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_IsInited(JNIEnv * env, jobject obj)
{   

    if (App->Inited)
    {
        return 1;
    }
    else
    {
        return 0;
    }

}

JNIEXPORT void JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_Destroy(JNIEnv * env, jobject obj)
{
	RenderMutex.lock();
	try
	{
		if (App->Inited)
		{
			App->Inited = false;
			App->OuterDeinit();
		}
	}
	catch (ErrorCommon e)
	{
		throw;
	}
	RenderMutex.unlock();
}


JNIEXPORT void JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_OnTapDown(JNIEnv * env, jobject obj, float x, float y, long time)
{
	
	try
	{
		App->OuterOnTapDown(vec2(x*CONST_MAXRIX_WIDTH/HalibutRender->GetScreenWidth(), y*CONST_MAXRIX_HEIGHT/HalibutRender->GetScreenHeight()));
	}
	catch (ErrorCommon e)
	{
		throw;
	}
}


JNIEXPORT void JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_OnTapUp(JNIEnv * env, jobject obj, float x, float y, long time)
{
	try
	{
		App->OuterOnTapUp(vec2(x*CONST_MAXRIX_WIDTH/HalibutRender->GetScreenWidth(), y*CONST_MAXRIX_HEIGHT/HalibutRender->GetScreenHeight()));
	}
	catch (ErrorCommon e)
	{
		throw;
	}
}

JNIEXPORT void JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_OnTapMove(JNIEnv * env, jobject obj, float x, float y, long time)
{
}

JNIEXPORT void JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_OnFling(JNIEnv * env, jobject obj, jfloat velocityX, jfloat velocityY, long time)
{
}

JNIEXPORT void JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_OnScroll(JNIEnv * env, jobject obj, jfloat distanceX, jfloat distanceY, long time)
{
	try
	{
		App->OuterOnMove(vec2(distanceX*CONST_MAXRIX_WIDTH/HalibutRender->GetScreenWidth(), distanceY*CONST_MAXRIX_HEIGHT/HalibutRender->GetScreenHeight()));
	}
	catch (ErrorCommon e)
	{
		throw;
	}
}

JNIEXPORT void JNICALL Java_fishrungames_crystalofrhylil_JniWrapper_OnKeyPress(JNIEnv * env, jobject obj, jint keyCode)
{
	
	try
	{
		App->OnKeyPress(keyCode);
	}
	catch (ErrorCommon e)
	{
		throw;
	}
}

