package com.tom_e_white.chickenalerts;

import java.text.DateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener {
	
	public static final int DEFAULT_DELAY = 45;
	
	private static final String PREF_ENABLED = "pref_enabled";
	private static final String PREF_DELAY = "pref_delay";
	private SettingsFragment fragment;

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

	@Override
	protected void onResume() {
		super.onResume();
		fragment = (SettingsFragment) getFragmentManager().findFragmentById(R.id.frag);
		fragment.getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		fragment.getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
		fragment = null;
	}
	
	public void testAlert(View view) {
		AlertScheduler scheduler = new AlertScheduler();
		scheduler.scheduleTestAlert(getApplicationContext());		
	}

	private void enableAlerts(int delay) {
		// Schedule next alert
		Calendar now = Calendar.getInstance();
		AlertScheduler scheduler = new AlertScheduler();
		Calendar nextAlert = scheduler.scheduleNextAlert(getApplicationContext(), delay, now);
		
		// Tell user when next alert was scheduled for
		DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
		boolean today = now.get(Calendar.DATE) == nextAlert.get(Calendar.DATE);
		String text = getString(today ? R.string.next_alert_today : R.string.next_alert_tomorrow,
				timeFormat.format(nextAlert.getTime()));
		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
	}
	
	private void disableAlerts() {
		// Cancel next alert
		AlertScheduler scheduler = new AlertScheduler();
		scheduler.cancelNextAlert(getApplicationContext());		
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals(PREF_ENABLED)) {
			Preference pref = fragment.findPreference(key);
			boolean on = sharedPreferences.getBoolean(PREF_ENABLED, false);
		    if (on) {
		    	pref.setSummary(getString(R.string.pref_enabled));
				int delay = getDelay(sharedPreferences);
		    	enableAlerts(delay);
		    } else {
		    	pref.setSummary(getString(R.string.pref_disabled));
		    	disableAlerts();
		    }
		} else if (key.equals(PREF_DELAY)) {
			Preference pref = fragment.findPreference(key);
			int delay = getDelay(sharedPreferences);
			pref.setSummary(getString(R.string.pref_delay_summary_param, delay));
	    	disableAlerts();
	    	enableAlerts(delay);
		}
	}
	
	private int getDelay(SharedPreferences sharedPreferences) {
		String delayString = sharedPreferences.getString(PREF_DELAY, DEFAULT_DELAY + "");
		int delay = DEFAULT_DELAY;
		try {
			delay = Integer.parseInt(delayString.trim());
		} catch (NumberFormatException e) {
			// set to default
		}
		return delay;
	}
	
}

@@ -985,16 +985,87 @@
 (this);%0A
+%09%09setSummaries(fragment.getPreferenceScreen().getSharedPreferences());%0A
 %09%7D%0A%09%0A%09@O
@@ -2977,24 +2977,572 @@
 );%0A%09%09%7D%0A%09%7D%0A%09%0A
+%09private void setSummaries(SharedPreferences sharedPreferences) %7B%0A%09%09%7B%0A%09%09%09Preference pref = fragment.findPreference(PREF_ENABLED);%0A%09%09%09boolean on = sharedPreferences.getBoolean(PREF_ENABLED, false);%0A%09%09    if (on) %7B%0A%09%09    %09pref.setSummary(getString(R.string.pref_enabled));%0A%09%09    %7D else %7B%0A%09%09    %09pref.setSummary(getString(R.string.pref_disabled));%0A%09%09    %7D%0A%09%09%7D%0A%09%09%7B%0A%09%09%09Preference pref = fragment.findPreference(PREF_DELAY);%0A%09%09%09int delay = getDelay(sharedPreferences);%0A%09%09%09pref.setSummary(getString(R.string.pref_delay_summary_param, delay));%09%09%09%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09private int

Fix to update summaries to accurately reflect current preferences.