package com.test.friction;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class Friction extends Activity{
    /** Called when the activity is first created. */
	private Sen gyrol;
	private Sen accell;

	private Sensor gyro;
	private Sensor accel;
	
	SensorEvent lastEvent;
	SensorManager sm;
	TextView cof_st;
	TextView cof_kt;
	TextView gx;
	TextView gy;
	TextView gz;
	Button start;
	Button done;
	public float highx = 0;
	public float highy = 0;
	private Friction g;
	@SuppressWarnings("unused")
	private float[] values;
	
    @SuppressWarnings("unused")
	private float[] accels = new float[3];
    private float[] gyros = new float[3];

    private double cof_s = 0;
    private double cof_k = 0;
	protected boolean res;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        g = this;
    	this.cof_st = ((TextView)findViewById(R.id.cofs));
    	this.cof_kt = ((TextView)findViewById(R.id.cofk));
    	this.gx = ((TextView)findViewById(R.id.gx));
    	this.gy = ((TextView)findViewById(R.id.gy));
    	
        this.sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    	this.accell = new Sen(Sen.ACC, this);
    	this.gyrol = new Sen(Sen.GYRO, this);
        this.accel = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        this.gyro = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    	this.res = false;
		
    	this.start = (Button)findViewById(R.id.start);
    	this.start.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(final View v) {
				if (!res){
					g.sm.registerListener(g.accell, g.accel, SensorManager.SENSOR_DELAY_GAME);
					g.sm.registerListener(g.gyrol, g.gyro, SensorManager.SENSOR_DELAY_GAME);
					g.cof_s = 0;
					g.cof_k = 0;
					
				}
			}
		});
        new CountDownTimer(10000000, 10) {

            public void onTick(long millisUntilFinished) {
                g.t();
            }

            public void onFinish() {
                g.finish();
            }
         }.start();
        g.t();
    }
	public void t(){
		accels = accell.get();
		gyros = gyrol.get();
		gx.setText("GX: " + -gyros[1]);
		gy.setText("GY: " + gyros[2]);

		this.cof_st.setText("S: " + (float)cof_s);
		this.cof_kt.setText("D: " + (float)cof_k);
	}
	public void slip(){

		this.sm.unregisterListener(this.gyrol);
		this.sm.unregisterListener(this.accell);
		this.res = false;
		
		float[] gg = gyrol.get();
		float[] aa = accell.get();
		
		//static
		
		double g = SensorManager.GRAVITY_EARTH*.158;
		double square = g*g;
		//square = z*z + y*y + x*x;
		double y = g*Math.sin(Math.toRadians(gg[2]));
		double x = g*Math.sin(Math.toRadians(gg[1]));
		double z = Math.sqrt(square - y*y - x*x);
		double total = Math.sqrt(y*y + x*x);
		cof_s = total/z;
		
		//kenetic
		double total_accel = Math.sqrt(aa[0]*aa[0] + aa[1]*aa[1]);
		cof_k = (total-.158*total_accel)/z;
		
	}
	
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.cal:
        	this.gyrol.cal();
        	return true;
        case R.id.reset:
        	this.finish();
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}