package edu.rpi.cs.yacs.core;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;

import edu.rpi.cs.yacs.R;

/**
 * Created by Mark Robinson on 9/25/16.
 */

public class SettingsActivity extends PreferenceActivity {

    public static class SettingsFragment extends PreferenceFragment {

        @Override
        public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, final Preference preference) {
            if (preference.getKey().equals(getString(R.string.houseOfYACS))) {
                new MaterialDialog.Builder(getActivity())
                        .title(R.string.houseOfYACS)
                        .items(R.array.home_YACS_values)
                        .itemsCallbackSingleChoice(0, new MaterialDialog.ListCallbackSingleChoice() {
                            @Override
                            public boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                preference.setSummary(text);
                                preference.setDefaultValue(text);
                                return true;
                            }
                        })
                        .positiveText(R.string.Select)
                        .show();
            }

            return super.onPreferenceTreeClick(preferenceScreen, preference);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.prefs);

            SharedPreferences sp = getPreferenceScreen().getSharedPreferences();

            Preference houseYACS = findPreference("House of YACS");
            houseYACS.setSummary(sp.getString(getString(R.string.houseOfYACS), "Rensselaer Polytechnic Institute"));
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(android.R.id.content, new SettingsFragment())
                    .commit();
        }
    }

    @Override
    protected boolean isValidFragment(String fragmentName) {
        return SettingsFragment.class.getName().equals(fragmentName);
    }
}
@@ -551,24 +551,62 @@
 Fragment %7B%0A%0A
+        SharedPreferences sp = null;%0A%0A
         @Ove
@@ -656,16 +656,22 @@
 eeClick(
+final 
 Preferen
@@ -1362,24 +1362,141 @@
 Value(text);
+%0A%0A                                sp.edit().putString(getString(R.string.houseOfYACS), String.valueOf(text)).apply();
 %0A           
@@ -1969,34 +1969,16 @@
         
-SharedPreferences 
 sp = get
@@ -2173,40 +2173,8 @@
 ), %22
-Rensselaer Polytechnic Institute
 %22));

Write preference changes to disk