package com.greenyetilab.equiv.ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;

import com.greenyetilab.equiv.R;
import com.greenyetilab.equiv.core.FormatUtils;
import com.greenyetilab.equiv.core.ProteinWeightUnit;

/**
 * Fragment storing all the settings
 */
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
    private ListPreference mProteinWeightUnitPreference;
    private EditTextPreference mProteinPerDayPreference;
    private SharedPreferences mPreferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        mPreferences = getPreferenceManager().getSharedPreferences();
        mPreferences.registerOnSharedPreferenceChangeListener(this);

        mProteinWeightUnitPreference = (ListPreference) findPreference("protein_weight_unit");
        mProteinPerDayPreference = (EditTextPreference) findPreference("max_protein_per_day");
        updateSummaries();
    }

    @Override
    public void onResume() {
        super.onResume();
        mPreferences.registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        mPreferences.unregisterOnSharedPreferenceChangeListener(this);
    }

    private void updateSummaries() {
        Kernel kernel = Kernel.getExistingInstance();
        String summary;

        summary = mProteinWeightUnitPreference.getEntry().toString();
        mProteinWeightUnitPreference.setSummary(summary);

        float maxProtein = kernel.getConsumer().getMaxProteinPerDay();
        summary = String.format("%sg", FormatUtils.naturalRound(maxProtein));
        mProteinPerDayPreference.setSummary(summary);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        Kernel.getExistingInstance().updateFromPreferences(prefs);
        updateSummaries();
    }
}

@@ -269,56 +269,8 @@
 .R;%0A
-import com.greenyetilab.equiv.core.FormatUtils;%0A
 impo
@@ -319,16 +319,16 @@
 htUnit;%0A
+
 %0A/**%0A * 
@@ -643,16 +643,63 @@
 erences;
+%0A    private WeightFormatter mProteinFormatter;
 %0A%0A    @O
@@ -804,16 +804,153 @@
 State);%0A
+        mProteinFormatter = new WeightFormatter(getResources());%0A        mProteinFormatter.setProteinFormat(ProteinWeightUnit.PROTEIN);%0A%0A
         
@@ -1646,24 +1646,220 @@
 is);%0A    %7D%0A%0A
+    @Override%0A    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) %7B%0A        Kernel.getExistingInstance().updateFromPreferences(prefs);%0A        updateSummaries();%0A    %7D%0A%0A
     private 
@@ -2174,231 +2174,352 @@
     
-summary = String.format(%22%25sg%22, FormatUtils.naturalRound(maxProtein));%0A        mProteinPerDayPreference.setSummary(summary
+if (kernel.getProteinUnit() == ProteinWeightUnit.POTATO) %7B%0A            String proteinSummary = mProteinFormatter.format(maxProtein, WeightFormatter.UnitFormat.SHORT);%0A            String potatoSummary = kernel.getWeightFormater().format(maxProtein
 );%0A    
-%7D%0A%0A
     
-@Override%0A    public void onSharedPreferenceChanged(SharedPreferences prefs, String key)
+    summary = String.format(%22%25s (= %25s)%22, proteinSummary, potatoSummary);%0A        %7D else
  %7B%0A 
@@ -2529,91 +2529,164 @@
     
+  
-Kernel.getExistingInstance().updateFromPreferences(prefs);%0A        updateSummaries(
+  summary = kernel.getWeightFormater().format(maxProtein, WeightFormatter.UnitFormat.SHORT);%0A        %7D%0A        mProteinPerDayPreference.setSummary(summary
 );%0A 

Use a different summary when in potato mode