package net.cs76.lectures.intents05;

import net.cs76.lectures.intents05.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/*
 * Intents05
 * Dan Armendariz
 * Computer Science E-76
 * 
 * Demonstrates creating an Options menu (accessible 
 * by pushing the MENU key on the device). 
 */

public class Code5 extends Activity implements OnClickListener, OnCompletionListener {

	MediaPlayer player;
	ImageView image;
	Button vroom;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // find the image and button Views, and set the listener for the button
        image = (ImageView)findViewById(R.id.image);
        
        vroom = (Button)findViewById(R.id.button);
        vroom.setOnClickListener(this);

    }

    // cause a media player instantiation when the activity comes to the front
    public void onResume() {
    	super.onResume();

    	// by default, we'll show and play the sound for the Hare
        setCar(R.id.hare);
    }
    
    // release the media player's resources when paused
    public void onPause() {
    	super.onPause();

    	player.release();
    	player = null;
    }
    
    // initialize the media player with a sound and give the
    // ImageView an image, depending on the requested option
    public void setCar(int car) {
    	// set different MediaPlayer or Image options based on the input

    	// if the player has already been initialized, we should reset it.
    	// this will stop playing a sound (if one is playing)
    	if(player != null) {  
    		player.reset();
    		vroom.setText(R.string.play);
    	}
    	
    	switch(car) {
    		case R.id.tortoise: 
    			player = MediaPlayer.create(this, R.raw.earlycarengine);
    			image.setImageResource(R.drawable.bmw3);
    			break;
    		case R.id.hare:
    		default:
    			player = MediaPlayer.create(this, R.raw.ferrari250);
    			image.setImageResource(R.drawable.citation);
    	}

    	// set some options for the player no matter which we picked
    	player.setLooping(false);
    	player.setOnCompletionListener(this);
    	
    }

    // override the onCreateOptionsMenu that gets
    // called when the user requests a menu
    public boolean onCreateOptionsMenu(Menu menu) {

    	// can also use the add() method to programmatically create a menu
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.options_menu, menu);
    	return true;
    }

    // when an item is selected in the menu, onOptionsItemSelected
    // is called and passes the selected 'item'
	public boolean onOptionsItemSelected(MenuItem item) {
		// change our car based on the selection
    	setCar(item.getItemId());

    	return true;
	}


    // FROM Intents04
    // when the button is clicked we want to hear a sound
	public void onClick(View v) {
		
		if(player.isPlaying()) {
			player.pause();
			vroom.setText(R.string.play);
		} else {
			vroom.setText(R.string.pause);
			player.start();
		}

	}

	// FROM Intents04
	// reset button text when the sound is done playing
	public void onCompletion(MediaPlayer mp) {
		vroom.setText(R.string.play);
	}

}