package hh.dejsem;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.preference.Preference;

import hh.lib.PrefsFragment;

public class Prefs extends PrefsFragment {

	static String SSL_DEBUG_KEY;
	static String SSL_DEBUG_DEFAULT;
	static String SSL_DEBUG_TITLE;
	static String SSL_DEBUG_DIALOG_TITLE;
	static String HOST_KEY;
	static String HOST_DEFAULT;
	static String HOST_CACHE_KEY;
	static String HOST_TITLE;
	static String HOST_DIALOG_TITLE;
	static String CHAN_KEY;
	static String CHAN_DEFAULT;
	static String HOME_KEY;
	static String HOME_DEFAULT;
	static String HOME_CACHE_KEY;
	static String HOME_TITLE;
	static String HOME_DIALOG_TITLE;
	static String SSL_KEY, SSL_DEFAULT;

	public static boolean sslDebug;
	public static String homeDir;
	public static String host;
	public static boolean ssl;

	public void getR(Context c) {
		super.getR(c);
		HOST_KEY = rs.getString(R.string.host_key);
		HOST_DEFAULT = rs.getString(R.string.host_default);
		HOST_CACHE_KEY = rs.getString(R.string.host_cache_key);
		HOST_TITLE = rs.getString(R.string.host_title);
		HOST_DIALOG_TITLE = rs.getString(R.string.host_dialog_title);
		CHAN_KEY = rs.getString(R.string.chan_key);
		CHAN_DEFAULT = rs.getString(R.string.chan_default);
		HOME_KEY = rs.getString(R.string.home_key);
		HOME_DEFAULT = c.getFilesDir().getAbsolutePath();
		HOME_CACHE_KEY = rs.getString(R.string.home_cache_key);
		HOME_TITLE = rs.getString(R.string.home_title);
		HOME_DIALOG_TITLE = rs.getString(R.string.home_dialog_title);
		SSL_KEY = rs.getString(R.string.ssl_key);
		SSL_DEFAULT = rs.getString(R.string.ssl_default);
		SSL_DEBUG_KEY = rs.getString(R.string.ssl_debug_key);
		SSL_DEBUG_DEFAULT = rs.getString(R.string.ssl_debug_default);
		SSL_DEBUG_TITLE = rs.getString(R.string.ssl_debug_title);
		SSL_DEBUG_DIALOG_TITLE = rs.getString(R.string.ssl_debug_dialog_title);
	}

	@Override
	public void refreshPrefs(Context c) {
		getR(c);
		super.refreshPrefs(c);
		sslDebug = sp.getBoolean(SSL_DEBUG_KEY, Boolean.valueOf(SSL_DEBUG_DEFAULT));
		host = sp.getString(HOST_KEY, HOST_DEFAULT);
		homeDir = sp.getString(HOME_KEY, HOME_DEFAULT);
		ssl = sp.getBoolean(SSL_KEY, Boolean.valueOf(SSL_DEFAULT));
	}

	public void setHome(Context c, String dir) {
		sp.edit().putString(HOME_KEY, dir).apply();
		refreshPrefs(c);
	}

	@Override
	public void onCreatePreferences(Bundle bundle, String s) {
		super.onCreatePreferences(bundle, s);
		addPreferencesFromResource(R.xml.prefs);
		if(rs == null) getR(getActivity());
		refreshPrefs(getActivity());
		setSumms();
	}

	@Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		super.onSharedPreferenceChanged(sharedPreferences, key);
		if(key.equals(HOST_KEY)) {
			K.title.setChannelNum();
			K.cmdConn = null;
//			K.histlist = null;
		}
	}

	void setSumms() {
		findPreference(SSL_DEBUG_KEY).setSummary(String.valueOf(sslDebug));
		findPreference(HOST_KEY).setSummary(host);
		findPreference(HOME_KEY).setSummary(homeDir);
		findPreference(SSL_KEY).setSummary(String.valueOf(ssl));
	}

	@Override
	public boolean onPreferenceTreeClick(Preference preference)	{
		String key = preference.getKey();
		if(key.equals(HOME_KEY)) {
			new AutoTextComplDialogFragment().enterText(d, preference, HOME_DIALOG_TITLE, HOME_CACHE_KEY);
			return true;
		}
		else if(key.equals(HOST_KEY)) {
			new AutoTextComplDialogFragment().enterText(d, preference, HOST_DIALOG_TITLE, HOST_CACHE_KEY);
			return true;
		}
		else return super.onPreferenceTreeClick(preference);
	}

	void resetPrefs(Context c) {
		getR(c);
		super.refreshPrefs(c);
		sp.edit().putBoolean(SSL_DEBUG_KEY, Boolean.valueOf(SSL_DEBUG_DEFAULT)).apply();
		sp.edit().putString(HOST_KEY, HOST_DEFAULT);
		sp.edit().putString(HOME_KEY, HOME_DEFAULT);
		sp.edit().putBoolean(SSL_KEY, Boolean.valueOf(SSL_DEFAULT));
		refreshPrefs(c);
	}
}
