package com.example.carolina.times;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;


import java.util.List;
import java.util.Vector;

public class MainActivity3 extends ActionBarActivity implements SensorEventListener {

    //VALORES CALIBRACION
    private double g;
    private double mitX, mitY;

    //ACTUALES
    private float curX = 0, curY = 0, curZ = 0;

    //PATRONES DE MOVIMIENTO
    private String Down = "D=U=D=";
    private String Left = "L=R=L=";
    private String Rigth = "R=L=R=";
    private String Up = "U=D=U=";

    boolean D = false, U = false, L = false, RI = false;
    boolean comenzar = false;

    //REPRODUCIR SONIDO
    private MediaPlayer mediaPlayer, mp;

    private ImageView imagen;
    private Spinner spinner;
    private TextView textView11;

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

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

        mediaPlayer = MediaPlayer.create(this, R.raw.complete);
        mp = MediaPlayer.create(this, R.raw.prev);

        imagen = (ImageView) findViewById(R.id.imageView);

        textView11 = (TextView) findViewById(R.id.textView11);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //DESEMPAQUETAMOS LO QUE VIENE DEL ACTIVITY ANTERIOR
        Bundle bundle = getIntent().getExtras();
        g = Float.parseFloat(bundle.getString("G"));
        mitX = Float.parseFloat(bundle.getString("X"));
        mitY = Float.parseFloat(bundle.getString("Y")) / 2.0;

        //LLENADO DE SPINNER
        spinner = (Spinner) findViewById(R.id.spinner);

        String[] valores = {"2/4 (Binario)", "3/4 (Ternario)", "4/4 (Cuaternario)"};
        spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, valores));
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                                              @Override
                                              public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                                                  String[] partitura = ((String) adapterView.getItemAtPosition(position)).split(" ");
                                                  textView11.setText("Partitura de Compás: " + partitura[0].trim());

                                                  if (position == 0)
                                                      imagen.setImageResource(R.drawable.binary);
                                                  else if (position == 1)
                                                      imagen.setImageResource(R.drawable.ternary);
                                                  if (position == 2)
                                                      imagen.setImageResource(R.drawable.quaternary);
                                              }

                                              @Override
                                              public void onNothingSelected(AdapterView<?> parent) {
                                              }
                                          }
        );
    }

    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);
        }
    }

    @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_activity3, 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(MainActivity3.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 Comenzar(View view)
    {
        comenzar=true;
    }

    int contA = 0, contL = 0;
    boolean okD=false, okL =false, okR=false, okU=false ;

    String auxA = "", auxL = "";
    String aux2="";
    boolean MOV = false;
    int compas=0;

    int cant_bien=0;


    //MOVIMIENTO HACIA ABAJO
    public void Down() {
        //((TextView) findViewById(R.id.textView8)).setText(String.valueOf(curY));

        if (!MOV && curY < g - mitY)
        {
            if (contA >= 1)
            {
                if (auxA.charAt(auxA.length() - 2) != 'D') {
                    auxA += "D";
                    aux2 += "D";
                }
            } else {
                auxA += "D";
                aux2 += "D";
            }
            MOV = true;
        }

        if (MOV && curY > g - mitY) {

            if (auxA.charAt(auxA.length() - 1) != '=') {
                auxA += "=";
                aux2 += "=";
                contA++;
            }
            MOV = false;
        }
        if (!MOV && curY > g + mitY) {
            if (contA >= 1) {
                if (auxA.charAt(auxA.length() - 2) != 'U') {
                    auxA += "U";
                    aux2 += "U";
                }
            } else {
                auxA += "U";
                aux2 += "U";
            }
            MOV = true;
        }

        if (MOV && curY < g + mitY)
        {
            if (auxA.charAt(auxA.length() - 1) != '=') {
                auxA += "=";
                aux2 += "=";
                contA++;
            }
            MOV = false;
        }
    }
    //MOVIMIENTO HACIA Los lados
    boolean MOVL = false;
    public void Left()
    {
        if (!MOVL && curX < -1*mitX  ) {
            if ( contL>=1 ) {
                if (auxL.charAt(auxL.length() - 2) != 'L') {
                    auxL  += "L";
                    aux2 += "L";
                }
            }
            else {
                auxL  += "L";
                aux2 += "L";
            }
            MOVL = true;
        }

        if(MOVL && curX > -1*mitX)
        {
            if ( auxL.charAt(auxL.length()-1)!='=' ) {
                auxL  += "=";
                aux2 += "=";
                contL++;
            }
            MOVL = false;
        }

        if (!MOVL && curX > mitX )
        {
            if ( contL>=1 )
            {
                if (auxL.charAt(auxL.length() - 2) != 'R')
                {
                    auxL  += "R";
                    aux2 += "R";
                }
            }
            else
            {
                auxL  += "R";
                aux2 += "R";
            }
            MOVL = true;
       }
        if(MOVL && curX < mitX)
        {
            if ( auxL.charAt(auxL.length()-1)!='=' )
            {
                auxL  += "=";
                aux2 += "=";
                contL++;
            }
            MOVL = false;
        }
    }

    int cant = 0;
    boolean ok = true;
    boolean ver = false;
    boolean cam = false;
    boolean mod = false;
    @Override
    public void onSensorChanged(SensorEvent event) {
        synchronized (this) {
            curX = event.values[0];
            curY = event.values[1];
            curZ = event.values[2];


            // Para el 2/2
            if(spinner.getSelectedItemPosition() == 0 && comenzar)
            {
                Left();
                Down();
                 ((TextView) findViewById(R.id.textView4)).setText(String.valueOf(auxL));
                 ((TextView) findViewById(R.id.textView3)).setText(String.valueOf(auxA));

                if(auxL.length() == 2 && !cam) {
                    mitX /= 2;
                    cam = true;
                }


                if(compas==0)
               {
                   if(auxL.length() == 4) {
                       mp.start();
                       Intent in = new Intent(getBaseContext(), MainActivity4.class);
                       startActivity(in);

                       compas = 4;
                       ok = false;
                       return;
                   }
                   if(ok) {
                       if (auxA.equals("D=U=") || auxA.equals("U=")) {
                           mediaPlayer.start();
                           auxA = "";
                           contA = 0;
                           MOV = false;
                           auxL = "";
                           contL = 0;
                           MOVL = false;
                           if (cam) {
                               cam = false;
                               mitX *= 2;
                           }
                           compas = 1;
                       }
                   }
               }

                //movimiento hacia arriba
                if(compas==1) {

                    if(auxL.length() == 4) {
                        mp.start();
                        Intent in = new Intent(getBaseContext(), MainActivity4.class);
                        startActivity(in);
                        compas = 4;
                        ok=false;
                        return;
                    }

                    //((TextView) findViewById(R.id.textView7)).setText(String.valueOf(aux2));
                    if(ok) {
                        if (auxA.equals("D=") || auxA.equals("U=D=")) {
                            mediaPlayer.start();
                            auxA = "";
                            contA = 0;
                            MOV = false;
                            auxL = "";
                            contL = 0;
                            MOVL = false;
                            if (cam) {
                                cam = false;
                                mitX *= 2;
                            }
                            compas = 0;
                            cant++;
                        }
                        // ((TextView) findViewById(R.id.textView4)).setText(String.valueOf(auxL+"dffsg"));
                    }
                }

                 if(cant == 10)
                    {
                        Intent in = new Intent(getBaseContext(), MainActivity5.class);
                        startActivity(in);
                        cant = 0;
                        compas = 4;
                    }

                }


            // Para el 3/3 compas 0 es abajo compas 1 es izq
            if(spinner.getSelectedItemPosition() == 1 && comenzar)
            {
                Down();
                Left();

                if(compas==0)
                {((TextView) findViewById(R.id.textView4)).setText(String.valueOf(auxL));
                    ((TextView) findViewById(R.id.textView3)).setText(String.valueOf(auxA));
                    //((TextView) findViewById(R.id.textView9)).setText( String.valueOf( auxA ) );
                    if(contA == 3) {
                        if (auxA.equals(Down)) {
                            mediaPlayer.start();
                            auxA = "";
                            auxL = "";
                            compas = 1;
                            contA = 0;
                            contL = 0;
                            MOV = false;
                            MOVL = false;
                        }
                    }
                    if(contA == 4) {
                        if(auxA.equals("U=D=U=D=")) {
                            mediaPlayer.start();
                            auxA = "";
                            auxL = "";
                            compas = 1;
                            contA = 0;
                            contL = 0;
                            MOV = false;
                            MOVL = false;

                        }
                        else{
                            Intent in = new Intent(getBaseContext(), MainActivity4.class);
                            mp.start();
                            startActivity(in);
                            compas = 4;
                        }
                        ((TextView) findViewById(R.id.textView4)).setText(String.valueOf(contA));

                    }

                    if (auxL.equals(Rigth) || auxL.equals(Left) || auxL .equals("R=L=R=L=") || auxL.equals("L=R=L=R=") ) {
                        mp.start();
                        Intent in = new Intent(getBaseContext(), MainActivity4.class);
                        startActivity(in);
                        compas = 4;
                    }

                }
                //mov hacia derecha
                if(compas==1)
                {
                    //((TextView) findViewById(R.id.textView9)).setText(String.valueOf(auxL));
                    //((TextView) findViewById(R.id.textView7)).setText(String.valueOf(aux2));
                    if(auxA.equals("D=") && auxL != "" )
                    {
                        auxA="";
                        MOV = false;
                        contA = 0;
                    }

                    if (contL == 3)
                    {
                        if (auxL.equals(Rigth))
                        {
                            mediaPlayer.start();
                            compas = 2;
                            contL = 0;
                            auxL = "";
                            MOVL = false;
                            auxA = "";
                            contA = 0;
                            MOV = false;
                        }
                        else
                        {
                            compas = 4;
                            mp.start();
                            Intent in = new Intent(getBaseContext(), MainActivity4.class);
                            startActivity(in);
                            //((TextView) findViewById(R.id.textView9)).setText(String.valueOf(auxL));
                            //((TextView) findViewById(R.id.textView7)).setText(String.valueOf(aux2));
                        }
                    }

                    if (auxA.equals(Up) || auxA.equals(Down) || auxA.equals("D=U=D=U=") || auxA.equals("U=D=U=D=")) {
                        mp.start();
                        Intent in = new Intent(getBaseContext(), MainActivity4.class);
                        startActivity(in);
                        compas = 4;
                        //((TextView) findViewById(R.id.textView9)).setText(String.valueOf(auxL));
                        //((TextView) findViewById(R.id.textView7)).setText(String.valueOf(aux2));
                        // procesar por mal
                    }
                }
                if(compas==2) {
                    //       ((TextView) findViewById(R.id.textView9)).setText(String.valueOf(auxA));
                    //      ((TextView) findViewById(R.id.textView7)).setText(String.valueOf(aux2));
                    if(auxL.equals("R=") && auxA != "" )
                    {
                        auxL="";
                        MOVL = false;
                        contL = 0;
                    }

                    if (contA == 3)
                    {
                        cam = false;
                        if (auxA.equals(Up))
                        {
                            mediaPlayer.start();
                            auxA = "";
                            compas = 0;
                            cant++;
                            contA = 0;
                            auxL = "";
                            MOV = false;
                            MOVL = false;

                        }
                        else
                        {
                            mp.start();
                            Intent in = new Intent(getBaseContext(), MainActivity4.class);
                            startActivity(in);
                            compas = 4;
                        }
                    }
                    if (auxL.equals(Rigth) || auxL.equals(Left) || auxL.equals("L=R=L=R=") || auxL.equals("R=L=R=L=")) {
                        mp.start();
                        Intent in = new Intent(getBaseContext(), MainActivity4.class);
                        startActivity(in);
                        compas = 4;
                    }
                }
                if(cant == 10)
                {
                    Intent in = new Intent(getBaseContext(), MainActivity5.class);
                    startActivity(in);
                    cant = 0;
                    compas = 4;
                }
            }
            // COMPAS 4/4
            if(spinner.getSelectedItemPosition() == 2 && comenzar)
            {
                Down();
                Left();

                if(compas==0)
                {
                    ((TextView) findViewById(R.id.textView4)).setText( String.valueOf( auxL ) );
                    ((TextView) findViewById(R.id.textView3)).setText( String.valueOf( auxA ) );
                    if(contA == 3)
                        if(auxA.equals(Down))
                        {
                            mediaPlayer.start();
                            auxA = "";
                            auxL = "";
                            compas = 1;
                            contA = 0;
                            contL = 0;
                            MOV = false;
                            MOVL = false;
                        }
                    if(contA == 4) {
                        if(auxA.equals("U=D=U=D="))
                        {
                            mediaPlayer.start();
                            auxA = "";
                            auxL = "";
                            compas = 1;
                            contA = 0;
                            contL = 0;
                            MOV = false;
                            MOVL = false;
                        }
                        else
                        {
                            mp.start();
                            Intent in = new Intent(getBaseContext(), MainActivity4.class);
                            startActivity(in);
                            compas = 4;
                        }
                    }
                    if (auxL.equals(Rigth) || auxL.equals(Left) || auxL .equals("R=L=R=L=") || auxL.equals("L=R=L=R=") ) {
                        mp.start();
                        Intent in = new Intent(getBaseContext(), MainActivity4.class);
                        startActivity(in);
                        compas = 4;
                    }

                }
                if(compas==1)
                {
                    if(auxL.length() == 0 && ver)
                    {
                        mitX /=2;
                        ver = false;
                    }
                    else
                    {
                        if(!ver)
                        {
                            mitX *= 2;
                            ver = true;
                        }
                    }
                    ((TextView) findViewById(R.id.textView4)).setText( String.valueOf( auxL ) );
                    ((TextView) findViewById(R.id.textView3)).setText( String.valueOf( auxA ) );

                    if(auxA.equals("D=") && auxL != "" )
                    {
                        auxA="";
                        MOV = false;
                        contA = 0;
                    }
                    //((TextView) findViewById(R.id.textView9)).setText(String.valueOf(auxL));
                    //((TextView) findViewById(R.id.textView7)).setText(String.valueOf(aux2));
                    if (contL == 3) {
                        if (auxL.equals(Left)) {
                            mediaPlayer.start();
                            compas = 2;
                            contL = 0;
                            auxL = "";
                            MOVL = false;
                            auxA = "";
                            contA = 0;
                            MOV = false;
                        }
                    }
                    if(contL == 4)
                        if(auxL.equals("R=L=R=L=")) {
                            mediaPlayer.start();
                            compas = 2;
                            contL = 0;
                            auxL = "";
                            MOVL = false;
                            auxA = "";
                            contA = 0;
                            MOV = false;
                        }
                        else {
                            mp.start();
                            Intent in = new Intent(getBaseContext(), MainActivity4.class);
                            startActivity(in);
                            //((TextView) findViewById(R.id.textView9)).setText(String.valueOf(auxL));
                            //((TextView) findViewById(R.id.textView7)).setText(String.valueOf(aux2));
                            compas = 4;
                        }

                    if (auxA.equals(Up) || auxA.equals(Down) || auxA.equals("D=U=D=U=") || auxA.equals("U=D=U=D=")) {
                        mp.start();
                        Intent in = new Intent(getBaseContext(), MainActivity4.class);
                        startActivity(in);
                        compas = 4;
                        //((TextView) findViewById(R.id.textView9)).setText(String.valueOf(auxL));
                        //((TextView) findViewById(R.id.textView7)).setText(String.valueOf(aux2));
                        // procesar por mal
                    }
                }

                if(compas==2) {
                    ((TextView) findViewById(R.id.textView4)).setText( String.valueOf( auxL ) );
                    ((TextView) findViewById(R.id.textView3)).setText( String.valueOf( auxA ) );
                    if(auxL.length() < 3 && ver)
                    {
                        mitX /=2;
                        ver = false;
                    }
                    else
                    {
                        if(!ver)
                        {
                            mitX *= 2;
                            ver = true;
                        }
                    }

                    if (contL == 3)
                    {
                        if (auxL.equals(Rigth))
                        {
                            mediaPlayer.start();
                            compas = 3;
                            contL = 0;
                            auxL = "";
                            MOVL = false;
                            auxA = "";
                            contA = 0;
                            MOV = false;
                        }
                    }

                    if (contL == 4)
                    {
                        if (auxL.equals("L=R=L=R="))
                        {
                            mediaPlayer.start();
                            compas = 3;
                            contL = 0;
                            auxL = "";
                            MOVL = false;
                            auxA = "";
                            contA = 0;
                            MOV = false;
                        }
                        else {
                            mp.start();
                            Intent in = new Intent(getBaseContext(), MainActivity4.class);
                            startActivity(in);
                            //((TextView) findViewById(R.id.textView9)).setText(String.valueOf(auxL));
                            //((TextView) findViewById(R.id.textView7)).setText(String.valueOf(aux2));
                            compas = 4;
                        }
                    }
                    if (auxA.equals(Up) || auxA.equals(Down) || auxA.equals("D=U=D=U=") || auxA.equals("U=D=U=D=")) {
                        mp.start();
                        Intent in = new Intent(getBaseContext(), MainActivity4.class);
                        startActivity(in);
                        compas = 4;
                        //((TextView) findViewById(R.id.textView9)).setText(String.valueOf(auxL));
                        //((TextView) findViewById(R.id.textView7)).setText(String.valueOf(aux2));
                        // procesar por mal
                    }
                }
                if(compas==3) {
                    ((TextView) findViewById(R.id.textView4)).setText( String.valueOf( auxL ) );
                    ((TextView) findViewById(R.id.textView3)).setText( String.valueOf( auxA ) );
                    if(auxL.equals("R=") && auxA != "" )
                    {
                        auxL="";
                        MOVL = false;
                        contL = 0;
                    }
                    if(auxL.equals("L=") && auxA != "" )
                    {
                        auxL="";
                        MOVL = false;
                        contL = 0;
                    }

                    if (contA == 3)
                    {
                        if (auxA.equals(Up))
                        {
                            mediaPlayer.start();
                            auxA = "";
                            compas = 0;
                            contA = 0;
                            cant++;
                            auxL = "";
                            MOV = false;
                            MOVL = false;
                        }
                        else
                        {
                            mp.start();
                            Intent in = new Intent(getBaseContext(), MainActivity4.class);
                            startActivity(in);
                            compas = 4;
                        }
                    }
                    if (auxL.equals(Rigth) || auxL.equals(Left) || auxL .equals("R=L=R=L=") || auxL.equals("L=R=L=R=") ) {
                        mp.start();
                        Intent in = new Intent(getBaseContext(), MainActivity4.class);
                        startActivity(in);
                        compas = 4;
                    }

                }
                if(cant == 10)
                {
                    Intent in = new Intent(getBaseContext(), MainActivity5.class);
                    startActivity(in);

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