package com.android.Stimuli;


import com.android.main.R;
import com.android.main.TimeDomainActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @author impaler
 * This is the main surface that handles the ontouch events and draws
 * the image to the screen.
 */
public class MainPanel extends SurfaceView implements SurfaceHolder.Callback {

	private boolean DEBUG = true;
	private final int MIN_SLIDE_DISTANCE = 200;
	private float oriXAxis,oriYAxis;//for one finger, swipe screen
	private static final String TAG = MainPanel.class.getSimpleName();
	private MainThread thread;
	private GarbageThread gt;
	//private Droid droid;
	private Stimuli stimuli_9HZ,
					stimuli_925HZ,
					stimuli_95HZ,
					stimuli_975HZ,
					stimuli_10HZ,
					stimuli_1025HZ,
					stimuli_105HZ,
					stimuli_1075HZ,
					stimuli_11HZ,
					stimuli_1125HZ,
					stimuli_115HZ,
					stimuli_1175HZ;
	private Paint paint;
	private final int stimuli_width = 300;//galaxyS 200, 450(single);//xoom 280, 300(single)
	private final int stimuli_height = 210;//galaxyS 90, 315(single);//xooom 150, 210(single)
	private final int stimuli_X_gap = 0;//galaxyS 50;//xoom 220
	private final int stiluli_Y_gap = 0;//galaxyS 30;//xoom 40
	public MainPanel(Context context) {
		super(context);
		// adding the callback (this) to the surface holder to intercept events
		getHolder().addCallback(this);

		//gt = new GarbageThread(10);
		setFocusable(true);
		stimuli_9HZ = new Stimuli	("9HZ",		0,								0,							     stimuli_width,stimuli_height);
		stimuli_925HZ = new Stimuli	("9.25HZ",	stimuli_width+stimuli_X_gap, 	0,							     stimuli_width,stimuli_height);
		stimuli_95HZ = new Stimuli	("9.5HZ",	stimuli_width*2+stimuli_X_gap*2,0,							     stimuli_width,stimuli_height);
		stimuli_975HZ = new Stimuli	("9.75HZ",	0,								stimuli_height+stiluli_Y_gap,    stimuli_width,stimuli_height);
		stimuli_10HZ = new Stimuli	("10HZ", 	stimuli_width+stimuli_X_gap,	stimuli_height+stiluli_Y_gap,	 stimuli_width,stimuli_height);
		stimuli_1025HZ = new Stimuli("10.25HZ", stimuli_width*2+stimuli_X_gap*2,stimuli_height+stiluli_Y_gap,	 stimuli_width,stimuli_height);
		stimuli_105HZ = new Stimuli	("10.5HZ", 	0,								stimuli_height*2+stiluli_Y_gap*2,stimuli_width,stimuli_height);
		stimuli_1075HZ = new Stimuli("10.75HZ", stimuli_width+stimuli_X_gap,	stimuli_height*2+stiluli_Y_gap*2,stimuli_width,stimuli_height);
		stimuli_11HZ = new Stimuli("11HZ", 0,0,stimuli_width,stimuli_height);//stimuli_11HZ = new Stimuli	("11HZ", 	stimuli_width*2+stimuli_X_gap*2,stimuli_height*2+stiluli_Y_gap*2,stimuli_width,stimuli_height);
		stimuli_1125HZ = new Stimuli("11.25HZ", 0, 								stimuli_height*3+stiluli_Y_gap*3,stimuli_width,stimuli_height);
		stimuli_115HZ = new Stimuli	("11.5HZ", 	stimuli_width+stimuli_X_gap, 	stimuli_height*3+stiluli_Y_gap*3,stimuli_width,stimuli_height);
		stimuli_1175HZ = new Stimuli("11.75HZ", stimuli_width*2+stimuli_X_gap*2,stimuli_height*3+stiluli_Y_gap*3,stimuli_width,stimuli_height);
		// create the main thread
		thread = new MainThread(getHolder(), this);;//new MainThread_test(getHolder(), this);
		paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setStyle(Style.FILL);
		oriXAxis = 0;
		oriYAxis = 0;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// at this point the surface is created and
		// we can safely start the game loop
		if(true){//thread.rtt.CONNECTED){
			thread.setRunning(true);
			thread.start();
		}
		else{
			Log.e(TAG, "BT connection hasn't been established");
			((Activity)getContext()).finish();
		}
		//gt.setRunning(true);
		//gt.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "Surface is being destroyed");
		// tell the thread to shut down and wait for it to finish
		// this is a clean shutdown
		boolean retry = true;
		while (retry) {
			try {
				//thread.rtt.closeAllSocket();
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}
		Log.d(TAG, "Thread was shut down cleanly");
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		//handleOneFingerSwipe(event);
		if(event.getX()<(getWidth()/4) && event.getY()<(getHeight()/4)){
			Log.e(TAG,"EXIT,"+event.getX()+"/"+event.getY());
			/*thread.rtt.RUN = false;
			thread.rtt.closeAllSocket();
			try {
				thread.sleep(10);
			} catch (InterruptedException e) {
				Log.e(TAG, "Can't make thread sleep!, "+e.getMessage());
			}*/
			thread.setRunning(false);
			((Activity)getContext()).finish();
		}
		else{
			Log.e(TAG,"TOUCHED, "+event.getX()+"/"+event.getY());
		}
		/*if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// delegating event handling to the droid
			droid.handleActionDown((int)event.getX(), (int)event.getY());
			
			// check if in the lower part of the screen we exit
			if (event.getY() > getHeight() - 50) {
				thread.setRunning(false);
				((Activity)getContext()).finish();
			} else {
				Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
			}
		} if (event.getAction() == MotionEvent.ACTION_MOVE) {
			// the gestures
			if (droid.isTouched()) {
				// the droid was picked up and is being dragged
				droid.setX((int)event.getX());
				droid.setY((int)event.getY());
			}
		} if (event.getAction() == MotionEvent.ACTION_UP) {
			// touch was released
			if (droid.isTouched()) {
				droid.setTouched(false);
			}
		}*/
		return true;
	}
	private void handleOneFingerSwipe(MotionEvent event){
		int action = event.getAction();
		int actionCode = action & MotionEvent.ACTION_MASK;
		if(actionCode == MotionEvent.ACTION_DOWN && event.getPointerCount()==1){
			oriXAxis = event.getX();
			oriYAxis = event.getY();
			if(DEBUG)Log.e(TAG, "oriXAxis= "+oriXAxis+"\n");
		}
		if(actionCode == MotionEvent.ACTION_MOVE &&event.getPointerCount()==1){
			if(event.getX()>oriXAxis && (event.getX()-oriXAxis >MIN_SLIDE_DISTANCE)){//swipe to right
				/*setContentView(R.layout.slideclockmain);
				myflipper.setInAnimation(inFromRightAnimation());	       
				myflipper.setOutAnimation(outToLeftAnimation());	        
				myflipper.showNext();*/ 
				if(DEBUG)Log.e(TAG, "SWIPE TO RIGHT! /oriXAxis= "+oriXAxis+" / "+event.getX()+"\n");
			}
			else if(event.getX()<oriXAxis && (oriXAxis-event.getX() >MIN_SLIDE_DISTANCE)){//swipe to left. to FFT
				/*setContentView(R.layout.slideclockmain);
				myflipper.setInAnimation(inFromLeftAnimation());     
				myflipper.setOutAnimation(outToRightAnimation());      
				myflipper.showPrevious(); */
				//setContentView(R.layout.canvas);
				thread.setRunning(false);
				((Activity)getContext()).finish();
				//finish();
				getContext().startActivity(new Intent( (Activity)getContext(),TimeDomainActivity.class));

				if(DEBUG)Log.e(TAG, "SWIPE TO LEFT! /oriXAxis= "+oriXAxis+" / "+event.getX()+"\n");
			}			
		}
	}
	public void render(Canvas canvas) {
		//canvas.drawColor(Color.BLACK);
		//canvas.drawRect(100, 100, 200, 200, paint);
		//canvas.drawColor(Color.BLACK);
		//droid.draw(canvas);
		/*stimuli_9HZ.draw(canvas);
		stimuli_925HZ.draw(canvas);
		stimuli_95HZ.draw(canvas);
		stimuli_975HZ.draw(canvas);
		stimuli_10HZ.draw(canvas);
		stimuli_1025HZ.draw(canvas);
		stimuli_105HZ.draw(canvas);
		stimuli_1075HZ.draw(canvas);*/
		stimuli_11HZ.draw(canvas);
		/*stimuli_1125HZ.draw(canvas);
		stimuli_115HZ.draw(canvas);
		stimuli_1175HZ.draw(canvas);*/
	}

	/**
	 * This is the game update method. It iterates through all the objects
	 * and calls their update method if they have one or calls specific
	 * engine's update method.
	 */
	public void update() {
		/*stimuli_9HZ.update();
		stimuli_925HZ.update();
		stimuli_95HZ.update();
		stimuli_975HZ.update();
		stimuli_10HZ.update();
		stimuli_1025HZ.update();
		stimuli_105HZ.update();
		stimuli_1075HZ.update();*/
		stimuli_11HZ.update();
		/*stimuli_1125HZ.update();
		stimuli_115HZ.update();
		stimuli_1175HZ.update();*/
		//droid.update();
		/*// check collision with right wall if heading right
		if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT
				&& droid.getX() + droid.getBitmap().getWidth() / 2 >= getWidth()) {
			droid.getSpeed().toggleXDirection();
		}
		// check collision with left wall if heading left
		if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT
				&& droid.getX() - droid.getBitmap().getWidth() / 2 <= 0) {
			droid.getSpeed().toggleXDirection();
		}
		// check collision with bottom wall if heading down
		if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN
				&& droid.getY() + droid.getBitmap().getHeight() / 2 >= getHeight()) {
			droid.getSpeed().toggleYDirection();
		}
		// check collision with top wall if heading up
		if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP
				&& droid.getY() - droid.getBitmap().getHeight() / 2 <= 0) {
			droid.getSpeed().toggleYDirection();
		}
		// Update the lone droid*/
		
	}

}
