/**
 * @author Twenty Codes
 * @author ricky barrette
 */
package com.TwentyCodes.android.SweetDreamsLib;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;

/**
 * this is a settings activity for WhiteNoise. it handles user changeable settings and saves them
 * @author ricky barrette
 */
public class Settings extends PreferenceActivity implements	OnPreferenceChangeListener {

	//shared_prefs file name
	protected static final String SETTINGS = "settings";
	
	//the following strings are for sound track selection
	protected static final String SOUNDS = "sounds";
	protected static final String SOUND_RIVER = "Rainy River";
	protected static final String SOUND_BEACH = "Beach";
	protected static final String SOUND_CRICKETS = "Crickets";
	protected static final String SOUND_FALLS = "Water Falls";
	protected static final String SOUND_AC = "Air Conditioner";
	
	//the following strings are for saving volume level from the audiomanager.STREAM_MUSIC so we can restore them back when the app quits
	protected static final String MUSIC_VOLUME = "music_volume";
	
	//the following strings are for timer preference
	protected static final String TIMER_ENABLED = "timer_enabled";
	protected static final String TIMER_LENGTH = "timer_length";
	protected static final String TIMER_EXIT_ON_FINISH = "timer_exit_on_finish";

	/** 
	 * Called when the activity is first created. 
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    	//set shared_prefs name
    	getPreferenceManager().setSharedPreferencesName(SETTINGS);
    	
    	//load preferences xml
    	this.addPreferencesFromResource( SweetDreamsFull.isFUll ? R.xml.settings : R.xml.settings_lite);
    	
    	//get shared_prefs
    	SharedPreferences settings = getPreferenceManager().getSharedPreferences();
    	
		/*
		 * Initialize preference sound
		 * set OnPreferenceChangeListener
		 * and set summary to current settings
		 */
    	Preference pSounds = findPreference(SOUNDS);
		pSounds.setOnPreferenceChangeListener(this);
		pSounds.setSummary(settings.getString(SOUNDS, SOUND_RIVER));
		
		/*
		 * Initialize preference timerLength
		 * set OnPreferenceChangeListener
		 * and set summary to current settings
		 */
		Preference timerLength = findPreference(TIMER_LENGTH);
		timerLength.setOnPreferenceChangeListener(this);
		try {
			if(SweetDreamsFull.isFUll)
				timerLength.setSummary(settings.getInt(TIMER_LENGTH, 5));
			else
				timerLength.setSummary(30);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

	/**
	 * called when a preference is changed
	 * @param preference
	 * @param newValue
	 * @return
	 * @author ricky barrette
	 */
	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
			preference.setSummary(newValue.toString());
			return true;
	}
}