package cisc181.simplemultimedia;

/*

SimpleMultimedia app -- background music, text to speech, and accelerometer sensor readings

Christopher Rasmussen
copyright 2015, University of Delaware

*/

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends ActionBarActivity {

    MediaPlayer MP;
    TextToSpeech mTTS;
    boolean readyToTalk = false;
    SensorManager SM;
    double aX, aY, aZ;
    boolean haveAccel = false;

    class SensorListener implements SensorEventListener {

        // handle sensor events

        public void onSensorChanged(SensorEvent event) {

            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                aX = event.values[0];
                aY = event.values[1];
                aZ = event.values[2];
                haveAccel = true;

                // print accel values to textviews

                TextView TV;
                DecimalFormat myFormatter = new DecimalFormat(".000");
                String output;

                TV = (TextView) findViewById(R.id.ax_id);
                output = myFormatter.format(aX);
                TV.setText("X " + output);

                TV = (TextView) findViewById(R.id.ay_id);
                output = myFormatter.format(aY);
                TV.setText("Y " + output);

                TV = (TextView) findViewById(R.id.az_id);
                output = myFormatter.format(aZ);
                TV.setText("Z " + output);
            }
        }

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

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

        // initialize music

        MP = MediaPlayer.create(getApplicationContext(), R.raw.electronic_clav);
        try {
            MP.prepare();
        } catch (Exception e) { }

        MP.setVolume(.5f, .5f);
        MP.setLooping(true);

        // initialize speech

        mTTS = new TextToSpeech(
                getApplicationContext(),
                new TextToSpeech.OnInitListener() {
                    public void onInit(int status) {
                        readyToTalk = true;
                    }
                });

       mTTS.setPitch (0.5f);
       //mTTS.setSpeechRate(2.0f);
       mTTS.addSpeech("(boom)", "cisc181.simplemultimedia", R.raw.grenade);

        // initialize sensor manager

        SM = (SensorManager)
                getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
        SM.registerListener(
                new SensorListener(),
                SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SM.SENSOR_DELAY_NORMAL);

    }

    // handle music events

    public void handlePlay(View V) {
        MP.start();
    }

    public void handlePause(View V) {
        MP.pause();
    }

    // handle speech events

    public void handleSaySomething(View V) {

        String myTextString;

        EditText ET = (EditText) findViewById(R.id.speech_text_id);

        // if the user has typed something, say it

        if (ET.getText().toString().length() > 0) {
            myTextString = ET.getText().toString();
        }

        // otherwise just use the hint string

        else {
            myTextString = ET.getHint().toString();
        }

        if (readyToTalk) {
            mTTS.speak(
                    myTextString,
                    TextToSpeech.QUEUE_ADD, // compare to QUEUE_ADD
                    null);
        }
    }

    @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, 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.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
