package com.example.carolina.times;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;

import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.*;
import android.view.MenuItem;
import android.content.pm.ActivityInfo;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import java.util.List;

public class MainActivity2 extends ActionBarActivity implements SensorEventListener {

    private float curX = 0, curY = 0, curZ = 0;
    private boolean pro = false;
    private double X, Y, g;
    private Button button;

    //PROGRESS BAR
    private ProgressDialog progressBar;
    private int progressStatus = 0;
    private Handler progressBarbHandler = new Handler();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setIcon(R.mipmap.ic_launcher);

        button = (Button) findViewById(R.id.button);
        button.setEnabled(false);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


    }

    protected void onResume() {
        super.onResume();
        SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
        if (sensors.size() > 0)
        {
            sm.registerListener(this, sensors.get(0), SensorManager.SENSOR_DELAY_GAME);
        }
        button.setEnabled(false);
    }

    @Override
    protected void onStop() {
        SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        sm.unregisterListener(this);
        super.onStop();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.autor) {
            String msn = "Nombre:\nCarolina Souto Venturini\nE-Mail:\ncarsouto@yahoo.com";
            MensajeEmergente("Autor.",msn, "OK");
            return true;
        }

        if (id == R.id.atras) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void MensajeEmergente(String titulo, String mensaje, String boton)
    {
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity2.this).create();
        alertDialog.setTitle(titulo);
        alertDialog.setMessage(mensaje);
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, boton,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }

    public void Launch2(View view)
    {
        Intent in = new Intent(this, MainActivity3.class);
        in.putExtra("G", String.valueOf(g));
        in.putExtra("X", String.valueOf(X));
        in.putExtra("Y", String.valueOf(Y));
        startActivity(in);
    }


    public void Calibracion(View view)
    {
        g = Math.sqrt( curX*curX + curY*curY + curZ*curZ ); //VALOR DE LA GRAVEDAD EN REPOSO
        if(pro == false)
        {
            pro = true;
        }
        IniciarProgressBar(view);
    }

    public void IniciarProgressBar(View view)
    {
        progressBar = new ProgressDialog(view.getContext());
        progressBar.setCancelable(false);
        progressBar.setMessage("Calibrando ...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();
        progressStatus = 0;

        new Thread(new Runnable() {
            public void run() {
                while (progressStatus < 100)
                {
                    progressStatus += 4;
                    progressBarbHandler.post(new Runnable()
                    {
                        public void run()
                        {
                            progressBar.setProgress(progressStatus);
                            if (progressStatus>=100)
                            {
                                pro = false;
                       //         ((TextView) findViewById(R.id.textView5)).setText("Y " + String.valueOf(Y)+ "   X" + String.valueOf(X) );
                                button.setEnabled(true);
                            }
                        }
                    });
                    try
                    {
                        Thread.sleep(200);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                progressBar.dismiss();
            }
        }).start();
    }

    public void Ins(View view)
    {
        Intent in = new Intent(this, Instrucciones.class);
        startActivity(in);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        synchronized (this)
        {
            curX = event.values[0];
            curY = event.values[1];
            curZ = event.values[2];

            if( pro == true )
            {
                Y = Math.max(Y, Math.abs(curY - g));
                X = Math.max(X, Math.abs(curX));
            }

           // ((TextView) findViewById(R.id.textView2)).setText("X: " + curX);
          //  ((TextView) findViewById(R.id.textView3)).setText("Y: " + curY);
          //  ((TextView) findViewById(R.id.textView4)).setText("Z: " + curZ);
       }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
