package de.tudarmstadt.informatik.hostage.ui;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;
import de.tudarmstadt.informatik.hostage.R;

/**
 * SettingsActivity creates the settings defined in /res/xml/preferences.xml.
 * 
 * @author Lars Pandikow.
 */
public class SettingsActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		// Set the value of the preference as the summary for the preference
		Preference pref = findPreference("pref_external_location");
		EditTextPreference etp = (EditTextPreference) pref;
		pref.setSummary(etp.getText());

		// Set the value of the preference as the summary for the preference
		pref = findPreference("pref_upload_server");
		etp = (EditTextPreference) pref;
		pref.setSummary(etp.getText());

		SharedPreferences defaultPref = PreferenceManager
				.getDefaultSharedPreferences(this);
		// Set the value of the preference as the summary for the preference
		pref = findPreference("pref_max_connections");
		etp = (EditTextPreference) pref;
		defaultPref
				.edit()
				.putInt("max_connections",
						Integer.valueOf(etp.getText()).intValue()).commit();
		pref.setSummary(etp.getText());

		// Set the value of the preference as the summary for the preference
		pref = findPreference("pref_timeout");
		etp = (EditTextPreference) pref;
		defaultPref.edit()
				.putInt("timeout", Integer.valueOf(etp.getText()).intValue())
				.commit();
		pref.setSummary(etp.getText());

		// Set the value of the preference as the summary for the preference
		pref = findPreference("pref_sleeptime");
		etp = (EditTextPreference) pref;
		defaultPref.edit()
				.putInt("sleeptime", Integer.valueOf(etp.getText()).intValue())
				.commit();
		pref.setSummary(etp.getText());

		// Set the value of the preference as the summary for the preference
		pref = findPreference("pref_location_time");
		etp = (EditTextPreference) pref;
		defaultPref
				.edit()
				.putInt("location_time",
						Integer.valueOf(etp.getText()).intValue()).commit();
		pref.setSummary(etp.getText());

		// Set the value of the preference as the summary for the preference
		pref = findPreference("pref_location_retries");
		etp = (EditTextPreference) pref;
		defaultPref
				.edit()
				.putInt("location_retries",
						Integer.valueOf(etp.getText()).intValue()).commit();
		pref.setSummary(etp.getText());
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		// Check which preference has been changed
		if (key.equals("pref_external_location")) {
			Preference pref = findPreference(key);
			EditTextPreference etp = (EditTextPreference) pref;
			String path = etp.getText();
			// Check if the Path is valid
			if (!path.startsWith("/"))
				path = new String("/").concat(path);
			if (!path.endsWith("/"))
				path = path.concat(new String("/"));
			if (!path.matches("/(([a-zA-Z_0-9])+/)*")) {
				Toast.makeText(this,
						"Path not valid. Must only contain a-zA-Z_0-9",
						Toast.LENGTH_SHORT).show();
				path = "/";
				sharedPreferences.edit().putString(key, path).commit();
			}
			pref.setSummary(path);
		} else if (key.equals("pref_upload_server")) {
			Preference pref = findPreference(key);
			EditTextPreference etp = (EditTextPreference) pref;
			pref.setSummary(etp.getText());
		} else if (key.equals("pref_max_connections")) {
			Preference pref = findPreference(key);
			EditTextPreference etp = (EditTextPreference) pref;
			String value = etp.getText();
			if (!value.matches("([0-9])+")) {
				Toast.makeText(getApplicationContext(),
						"Enter a valid number.", Toast.LENGTH_SHORT).show();
				value = getResources().getString(
						R.string.pref_max_connections_default);
			}
			sharedPreferences
					.edit()
					.putInt("max_connections",
							Integer.valueOf(value).intValue()).commit();
			pref.setSummary(value);
		} else if (key.equals("pref_timeout")) {
			Preference pref = findPreference(key);
			EditTextPreference etp = (EditTextPreference) pref;
			String value = etp.getText();
			if (!value.matches("([0-9])+")) {
				Toast.makeText(getApplicationContext(),
						"Enter a valid number.", Toast.LENGTH_SHORT).show();
				value = getResources().getString(R.string.pref_timeout_default);
			}
			sharedPreferences.edit()
					.putInt("timeout", Integer.valueOf(value).intValue())
					.commit();
			pref.setSummary(value);
		} else if (key.equals("pref_sleeptime")) {
			Preference pref = findPreference(key);
			EditTextPreference etp = (EditTextPreference) pref;
			String value = etp.getText();
			if (!value.matches("([0-9])+")) {
				Toast.makeText(getApplicationContext(),
						"Enter a valid number.", Toast.LENGTH_SHORT).show();
				value = getResources().getString(
						R.string.pref_sleeptime_default);
			}
			sharedPreferences.edit()
					.putInt("sleeptime", Integer.valueOf(value).intValue())
					.commit();
			pref.setSummary(value);
		} else if (key.equals("pref_location_time")) {
			Preference pref = findPreference(key);
			EditTextPreference etp = (EditTextPreference) pref;
			String value = etp.getText();
			if (!value.matches("([0-9])+")) {
				Toast.makeText(getApplicationContext(),
						"Enter a valid number.", Toast.LENGTH_SHORT).show();
				value = getResources().getString(
						R.string.pref_location_time_default);
			}
			sharedPreferences.edit()
					.putInt("location_time", Integer.valueOf(value).intValue())
					.commit();
			pref.setSummary(value);
		} else if (key.equals("pref_location_retries")) {
			Preference pref = findPreference(key);
			EditTextPreference etp = (EditTextPreference) pref;
			String value = etp.getText();
			if (!value.matches("([0-9])+")) {
				Toast.makeText(getApplicationContext(),
						"Enter a valid number.", Toast.LENGTH_SHORT).show();
				value = getResources().getString(
						R.string.pref_location_retries_default);
			}
			sharedPreferences
					.edit()
					.putInt("location_retries",
							Integer.valueOf(value).intValue()).commit();
			pref.setSummary(value);
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onResume() {
		// register a listener to catch preference changes
		super.onResume();
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}
}
