/* MapView.java - Widget for the rendering the map
* 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.
*/
package org.progtools.astarsearch;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Implements the view responsible for drawing and handle touch events for the
 * map.
 */
public class MapView extends View {
	private Bitmap bitmap;

	private GestureDetectorCompat  gestures;

	public MapView(Context context, int width, int height) {
		super(context);

		bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        gestures = new GestureDetectorCompat (getContext(), new GestureListner());
	}

	/**
	 * Draws the widget contents.
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		renderMap(bitmap);
		canvas.drawBitmap(bitmap, 0, 0, null);
	}

    @Override
    public boolean onTouchEvent(MotionEvent event){
        gestures.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

	class GestureListner extends  GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        /**
         * Processes the Android events by dispatching them
         * to the relevant C++ code.
         */
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            float x = e.getX();
            float y = e.getY();

            touchDownAt(x, y);
            return true;
        }
	}
	
	// methods implemented in C++
	
	public native boolean loadMap(String pathname);
    public native void startSearch();
	public native void touchDownAt(float x, float y);
	public native void clear();
	private native void renderMap(Bitmap bitmap);
}
