/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.vostan.sokoban.play;

import android.content.Context;
import android.graphics.Point;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import org.vostan.sokoban.Puzzle;

//import android.support.v4.view.GestureDetectorCompat;

/**
 *
 * @author vahagnk
 */
public class PuzzleControl extends PuzzleView
						implements GestureDetector.OnGestureListener
								, GestureDetector.OnDoubleTapListener
								, ScaleGestureDetector.OnScaleGestureListener
								, Animator.AnimationLister
{
	protected GestureDetector simpled;
	protected ScaleGestureDetector scaled;
	protected PuzzleLogic logic;
	protected Animator animator;
	protected PuzzleControlLister lister;

	protected float lastSpan;
	protected Point singleTapTile = new Point();
	
	public interface PuzzleControlLister
	{
		public void onSolved();
		public void onLongPress();
		public void onTouch();
	}
	
	public PuzzleControl(Context c)
	{
		super(c);
		logic = new PuzzleLogic();
		animator = new Animator( this );
		animator.setAnimationLister(this);
		
		simpled = new GestureDetector( c, this );
		simpled.setOnDoubleTapListener(this);
		scaled = new ScaleGestureDetector( c, this );
	}
	
	public void setPuzzle( Puzzle p )
	{
		super.setPuzzle(p);
		logic.setPuzzle(p);
	}

	public void setPuzzleControlLister( PuzzleControlLister l )
	{
		lister = l;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		//
		//	Stop any pending animations.
		//
		animator.stop();
		animator.clear();
		//
		//	Pass this event to scale and gesture detectors.
		//
		boolean r = scaled.onTouchEvent(event);
		r = simpled.onTouchEvent(event) || r;
		//r = super.onTouchEvent(event) || r;
		return r;
	}
	
	public boolean onDown(MotionEvent e)
	{
		return true;
	}
	public void onLongPress(MotionEvent e)
	{
		if ( lister != null )
			lister.onLongPress();
	}
	public void onShowPress(MotionEvent e)
	{
	}
	public boolean onSingleTapUp(MotionEvent e)
	{
		return true;
	}
	public boolean onDoubleTap(MotionEvent e)
	{
		return true;
	}
	public boolean onDoubleTapEvent(MotionEvent e)
	{
		return true;
	}

	public boolean onSingleTapConfirmed(MotionEvent e)
	{
		//
		//	Translate the tap point into tile column and row.
		//
		getTile( e.getX(), e.getY(), singleTapTile );
		//
		//	If the tap is not on a valid tile then there is not point to
		//	continue.
		//
		if ( !puzzle.isValid(singleTapTile.x, singleTapTile.y) )
			return true;
		//
		//	Create sequence of steps and then animate it.
		//
		if ( logic.createSteps(animator, singleTapTile.x, singleTapTile.y) )
		{
			puzzle.save();
			animator.play();
		}
		//
		//	Notify that we were touched.
		//
		if ( lister != null )
			lister.onTouch();
		//
		//	This event is processed.
		//
		return true;
	}
		
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
	{
		return true;
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
	{
		scrollViewport( distanceX, distanceY );
		return true;
	}
	
	public boolean onScaleBegin(ScaleGestureDetector detector)
	{
		lastSpan = scaled.getCurrentSpan();
		return true;
	}
	
	public boolean onScale(ScaleGestureDetector detector)
	{
		float span = scaled.getCurrentSpan();
		float focusX = scaled.getFocusX();
		float focusY = scaled.getFocusY();
		scaleViewport(focusX,focusY,lastSpan/span);
		lastSpan = span;
		return true;
	}
	
	public void onScaleEnd(ScaleGestureDetector detector)
	{
		scaleViewportDone();
	}
	
	public void onAnimationEnd()
	{
		if ( puzzle.isDone() )
			if ( lister != null )
				lister.onSolved();
	}
}
