package de.reinhardt_karlheinz.pcc.android;

import de.reinhardt_karlheinz.pcc.android.plugins.TiltCalc;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

public class Minecraft extends Activity {

	private static final String TAG = "Minecraft-Pl";
	// private SensorManager sm;
	/* sensor data */
	// @http://pastebin.com/c1MNT5x7
	SensorManager m_sensorManager;
	float[] m_lastMagFields;
	float[] m_lastAccels;
	private float[] m_rotationMatrix = new float[16];
	private float[] m_remappedR = new float[16];
	private float[] m_orientation = new float[4];
	/* fix random noise by averaging tilt values */
	final static int AVERAGE_BUFFER = 30;
	float[] m_prevPitch = new float[AVERAGE_BUFFER];
	float m_lastPitch = 0.f;
	float m_lastYaw = 0.f;
	/* current index int m_prevEasts */
	int m_pitchIndex = 0;

	float[] m_prevRoll = new float[AVERAGE_BUFFER];
	float m_lastRoll = 0.f;
	/* current index into m_prevTilts */
	int m_rollIndex = 0;

	/* center of the rotation */
	private float m_tiltCentreX = 0.f;
	private float m_tiltCentreY = 0.f;
	private float m_tiltCentreZ = 0.f;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.minecraft);
		Log.d(TAG, "act started");
		// sm = (SensorManager) getSystemService(SENSOR_SERVICE);
		// TiltCalc tc = new TiltCalc(getBaseContext());
	}

	private void computeOrientation() {
		if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
				m_lastMagFields, m_lastAccels)) {
			SensorManager.getOrientation(m_rotationMatrix, m_orientation);

			/* 1 radian = 57.2957795 degrees */
			/*
			 * [0] : yaw, rotation around z axis [1] : pitch, rotation around x
			 * axis [2] : roll, rotation around y axis
			 */
			float yaw = m_orientation[0] * 57.2957795f;
			float pitch = m_orientation[1] * 57.2957795f;
			float roll = m_orientation[2] * 57.2957795f;

			/* append returns an average of the last 10 values */
			m_lastYaw = m_filters[0].append(yaw);
			m_lastPitch = m_filters[1].append(pitch);
			m_lastRoll = m_filters[2].append(roll);
			TextView rt = (TextView) findViewById(R.id.roll);
			TextView pt = (TextView) findViewById(R.id.pitch);
			TextView yt = (TextView) findViewById(R.id.yaw);
			yt.setText("azi z: " + m_lastYaw);
			pt.setText("pitch x: " + m_lastPitch);
			rt.setText("roll y: " + m_lastRoll);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		// Sensor acc_sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

		// register this class as a listener for the orientation and
		// accelerometer sensors
		// sm.registerListener(this, acc_sensor, SensorManager.SENSOR_DELAY_UI);
	}

	private final TouchVector referenceVector = new TouchVector(0, 0, 0, 1);
	float lastXPos = 0;
	float lastYPos = 0;

	@Override
	public boolean onTouchEvent(MotionEvent me) {
		// TODO set intervall

		int action = me.getAction();
		float currentXPosition = me.getX();
		float currentYPosition = me.getY();

		// Log.v(TAG, "Action = " + action);
		// Log.v(TAG, "X = " + currentXPosition + "Y = " + currentYPosition);
		if (action == MotionEvent.ACTION_DOWN) {
			// lastXPos = 0;
			// lastYPos = 0;
			lastXPos = me.getX();
			lastYPos = me.getY();
		}
		if (action == MotionEvent.ACTION_MOVE) {
			// calculate vector
			TouchVector vector = new TouchVector(lastXPos, currentXPosition,
					lastYPos, currentYPosition);

			// McGlobals.svr.sendMsg("angl("+ angle+")lnght("+length+")");
			McGlobals.svr.sendMsg("X(" + vector.getX() + ")Y(" + vector.getY()
					+ ")");

			// Toast.makeText(getBaseContext(), "" + angle, 10).show();

			// do something
			lastXPos = currentXPosition;
			lastYPos = currentYPosition;

		}

		if (action == MotionEvent.ACTION_UP) {
			// do something
			// lastXPos = 0;
			// lastYPos = 0;
			Log.v(TAG, "finger released");
		}

		return true;
	}

	// @Override
	// public void onAccuracyChanged(Sensor sensor, int accuracy) {
	// // TODO Auto-generated method stub

	// }

	// float[] values;
	//
	// @Override
	// public void onSensorChanged(SensorEvent event) {
	// values = event.values;
	// float[] R = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
	// float[] orientation = sm.getOrientation(R, values);
	// Log.d("SENSOR LISTENER", "X: " + orientation[1] + "Y: "
	// + orientation[2] + "Z: " + orientation[0]);
	// }
	//
	// @Override
	// protected void onStop() {
	// // unregister listener
	// sm.unregisterListener(this);
	// super.onStop();
	// }
}
