package com.github.openwebnet.view.settings;

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;
import android.text.TextUtils;

import com.github.openwebnet.R;
import com.github.openwebnet.view.MainActivity;

import org.greenrobot.eventbus.EventBus;

import de.cketti.library.changelog.ChangeLog;

import static com.github.openwebnet.view.settings.GatewayListPreference.PREF_DEFAULT_GATEWAY_VALUE;

public class SettingsFragment extends PreferenceFragment {

    public static final String PREF_KEY_ABOUT_CHANGELOG = "com.github.openwebnet_preferences.PREF_KEY_ABOUT_CHANGELOG";
    public static final String PREF_KEY_DEBUG_DEVICE = "com.github.openwebnet_preferences.PREF_KEY_DEBUG_DEVICE";
    public static final String PREF_KEY_TEMPERATURE = "com.github.openwebnet_preferences.PREF_KEY_TEMPERATURE";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
        updatePreferenceSummary(getPreferenceScreen());
        initDebug();
        initAbout();
    }

    private void updatePreferenceSummary(Preference preference) {
        if (preference instanceof PreferenceGroup) {
            PreferenceGroup preferenceGroup = (PreferenceGroup) preference;
            for (int i=0; i<preferenceGroup.getPreferenceCount(); i++) {
                // recursive
                updatePreferenceSummary(preferenceGroup.getPreference(i));
            }
        } else if (preference instanceof GatewayListPreference) {
            updateGatewayListPreferenceSummary((GatewayListPreference) preference);
        } else if (preference instanceof ListPreference) {
            ListPreference listPreference = (ListPreference) preference;
            preference.setSummary(listPreference.getEntry());
        }
    }

    private void updateGatewayListPreferenceSummary(GatewayListPreference preference) {
        String value = preference.getSharedPreferences().getString(PREF_DEFAULT_GATEWAY_VALUE, "");
        if (!TextUtils.isEmpty(value)) {
            preference.setSummary(value);
        }
    }

    private void initDebug() {
        getPreferenceScreen().findPreference(PREF_KEY_DEBUG_DEVICE)
            .setOnPreferenceChangeListener((preference, newValue) -> {
                EventBus.getDefault().post(new MainActivity
                    .OnChangePreferenceDeviceDebugEvent((Boolean) newValue));
                return true;
            });
    }

    private void initAbout() {
        getPreferenceScreen().findPreference(PREF_KEY_ABOUT_CHANGELOG)
            .setOnPreferenceClickListener(preference -> {
                new ChangeLog(preference.getContext()).getLogDialog().show();
                return true;
            });
    }
}

@@ -262,24 +262,94 @@
 TextUtils;%0A%0A
+import com.annimon.stream.Optional;%0Aimport com.annimon.stream.Stream;%0A
 import com.g
@@ -368,16 +368,16 @@
 bnet.R;%0A
-
 import c
@@ -455,24 +455,73 @@
 .EventBus;%0A%0A
+import java.util.Arrays;%0Aimport java.util.List;%0A%0A
 import de.ck
@@ -1279,24 +1279,57 @@
 eScreen());%0A
+        initTemperatureChange();%0A
         init
@@ -2379,24 +2379,24 @@
 ary(value);%0A
-
         %7D%0A  
@@ -2392,32 +2392,716 @@
        %7D%0A    %7D%0A%0A
+    private void initTemperatureChange() %7B%0A        getPreferenceScreen().findPreference(PREF_KEY_TEMPERATURE)%0A            .setOnPreferenceChangeListener((preference, newValue) -%3E %7B%0A%0A                List%3CString%3E keys = Arrays.asList(getResources().getStringArray(R.array.temperature_scale_keys));%0A                Optional%3CString%3E label = Stream.of(keys)%0A                    .filter(newValue::equals)%0A                    .map(keys::indexOf)%0A                    .map(index -%3E getResources().getStringArray(R.array.temperature_scale_values)%5Bindex%5D)%0A                    .findFirst();%0A%0A                preference.setSummary(label.get());%0A                return true;%0A            %7D);%0A    %7D%0A%0A
     private void

refresh temperature scale on preference change