/* bindings.cpp - Glue code for the Java native methods.
* Copyright (C) 2014 Paulo Pinto
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/

#include <jni.h>
#include <time.h>
#include <android/log.h>
#include <android/bitmap.h>
#include <string>

#include "Logger.h"
#include "AndroidRender.h"
#include "MapWidget.h"

// Real code
static MapWidget mapWidget;

/*
 * Class:     org_progtools_astarsearch_MapView
 * Method:    renderMap
 * Signature: (Landroid/graphics/Bitmap;)V
 */
extern "C"
JNIEXPORT void JNICALL Java_org_progtools_astarsearch_MapView_renderMap
  (JNIEnv *env, jclass, jobject bitmap)
{
	AndroidRender render(env, bitmap);
	mapWidget.paint(render);
}

/*
 * Class:     org_progtools_astarsearch_MapView
 * Method:    startSearch
 * Signature: (IIII)V
 */
extern "C"
JNIEXPORT void JNICALL Java_org_progtools_astarsearch_MapView_startSearch
  (JNIEnv *, jclass)
{
	mapWidget.startSearch();
}

/*
 * Class:     org_progtools_astarsearch_MapView
 * Method:    stopSearch
 * Signature: ()V
 */
extern "C"
JNIEXPORT void JNICALL Java_org_progtools_astarsearch_MapView_stopSearch
  (JNIEnv *, jclass)
{

}

/*
 * Class:     org_progtools_astarsearch_MapView
 * Method:    clear
 * Signature: ()V
 */
extern "C"
JNIEXPORT void JNICALL Java_org_progtools_astarsearch_MapView_clear
  (JNIEnv *, jclass)
{
	mapWidget.clearMap();
}

/*
 * Class:     org_progtools_astarsearch_MapView
 * Method:    showGrid
 * Signature: ()V
 */
extern "C"
JNIEXPORT void JNICALL Java_org_progtools_astarsearch_MapView_showGrid
  (JNIEnv *, jclass)
{
	mapWidget.enable_gridlines();
}

/*
 * Class:     org_progtools_astarsearch_MapView
 * Method:    hideGrid
 * Signature: ()V
 */
extern "C"
JNIEXPORT void JNICALL Java_org_progtools_astarsearch_MapView_hideGrid
  (JNIEnv *, jclass)
{
	mapWidget.disable_gridlines();
}

/*
 * Class:     org_progtools_astarsearch_MapView
 * Method:    loadMap
 * Signature: (Ljava/lang/String;)Z
 */
extern "C"
JNIEXPORT jboolean JNICALL Java_org_progtools_astarsearch_MapView_loadMap
  (JNIEnv *env, jclass , jstring pathname)
{
	   const char *nativeString = env->GetStringUTFChars(pathname, nullptr);

	   LogInfo(std::string("The pathnam %s ") +  nativeString);
	   bool result = mapWidget.loadMap(std::string(nativeString));

	   env->ReleaseStringUTFChars(pathname, nativeString);

	   return result;
}


/*
 * Class:     org_progtools_astarsearch_MapView
 * Method:    touchDownAt
 * Signature: (II)V
 */
extern "C"
JNIEXPORT void JNICALL Java_org_progtools_astarsearch_MapView_touchDownAt
  (JNIEnv *, jclass, jfloat x, jfloat y)
{
	mapWidget.mouseDoubleClickEvent(x, y);
}
