package net.cs76.lectures.Storage02;

import net.cs76.lectures.Storage02.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/*
 * Code2
 * Dan Armendariz
 * Computer Science E-76
 * 
 * Demonstrates saving and restoring data between
 * multiple Activities in the same application via
 * the SharedPreferences object.
 */

public class Code2 extends Activity implements OnClickListener {

	// define a "file name", of sorts, in which to store the preferences
	private final String PREFS_NAME = "Code2Prefs";

	// declare the textview that will show our text
	TextView display;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // connect to the UI object
        display = (TextView)findViewById(R.id.textView);

        // make sure to start the Settings activity when a user
        // clicks on the "Settings" button
        Button settings = (Button)findViewById(R.id.settings);
        settings.setOnClickListener(this);
    }


	/** Restore our preferences.
	  * Why might we want to restore onResume(), 
	  * rather than onCreate()?
	  */
	public void onResume() {
		// as with all overridden activity methods, we should call the 
		// same method on the parent or risk a crash.
		super.onResume();
		
		// get our SharedPreferences object
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

        // load data from it or use some defaults if values don't (yet?) exist
        boolean boldedText = prefs.getBoolean("boldedText", false);
        String welcomeText = prefs.getString("welcomeText", getString(R.string.hello));

        // we'll set the TextView to bold, but only if the boldedText setting is true
        if(boldedText)
        	display.setTypeface(Typeface.DEFAULT_BOLD);
        else
        	display.setTypeface(Typeface.DEFAULT);

        // change the text of the TextView to the saved value
        display.setText(welcomeText);
	}

	/** Open the "Settings" activity on request.
	 */
	public void onClick(View v) {
    	Intent i = new Intent(this, Settings.class);
		startActivity(i);
	}
	
	
}