package net.cs76.lectures.storage01;

import net.cs76.lectures.storage01.R;
import android.app.Activity;
import android.content.SharedPreferences;
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;

/*
 * Code1
 * Dan Armendariz
 * Computer Science E-76
 * 
 * Based on Intents05.
 * Demonstrates saving/restoring simple data via
 * the SharedPreferences object.
 */

public class Code1 extends Activity implements OnClickListener, OnCompletionListener {

	MediaPlayer player;
	ImageView image;
	Button vroom;

	// fields related to saving preferences
	int selection;
	
    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);
    }

    /** called when the Activity is resuming
     */
    public void onResume() {
    	super.onResume();

        // Restore preferences - get the saved preferences
        SharedPreferences prefs = getPreferences(MODE_PRIVATE);

        // recall the saved car value, or use R.id.hare as the default if
        // the preference doesn't (yet?) exist.
        int savedCar = prefs.getInt("defaultCar", R.id.hare);

        // display the saved car
        setCar(savedCar);    	
    }

    /** called when the Activity is pausing (and might be killed, so save data)
      */
    public void onPause() {
    	// call the parent's onPause() method
    	super.onPause();

    	// free up the media player's resources
    	player.release();
    	player = null;
    	
    	// build our preferences object with our data to save
    	SharedPreferences prefs = getPreferences(MODE_PRIVATE);
    	SharedPreferences.Editor editor = prefs.edit();
    	editor.putInt("defaultCar", selection);
    	
    	// we must commit the preferences or they won't be saved!
    	editor.commit();
    }


    /** Initialize the media player with a sound and give the
      * ImageView an image, depending on the requested option.
      */
    public void setCar(int car) {
    	
    	// remember our current car selection
    	selection = 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(getApplicationContext(), R.raw.earlycarengine);
    			image.setImageResource(R.drawable.bmw3);
    			break;
    		case R.id.hare:
    		default:
    			player = MediaPlayer.create(getApplicationContext(), 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;
	}


    /** 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();
		}

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

}