/*
 * Copyright (c) 2015 Oleksandr Tyshkovets <olexandr.tyshkovets@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gnucash.android.ui.settings;

import android.annotation.TargetApi;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockPreferenceActivity;

import org.gnucash.android.R;
import org.gnucash.android.db.AccountsDbAdapter;
import org.gnucash.android.model.Money;

import java.util.Currency;
import java.util.List;

/**
 * Fragment for charts configuring
 * @author Oleksandr Tyshkovets <olexandr.tyshkovets@gmail.com>
 */
@TargetApi(11)
public class ChartPreferenceFragment extends PreferenceFragment {

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

        addPreferencesFromResource(R.xml.fragment_chart_preferences);

        ActionBar actionBar = ((SherlockPreferenceActivity) getActivity()).getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Chart Prefs");

        List<Currency> currencyList = AccountsDbAdapter.getInstance().getCurrencies();
        int size = currencyList.size();
        String[] currencyCodes = new String[size];
        for (Currency currency : currencyList) {
            currencyCodes[--size] = currency.getCurrencyCode();
        }

        ListPreference pref = (ListPreference) findPreference(getString(R.string.key_chart_currency));
        pref.setEntries(currencyCodes);
        pref.setEntryValues(currencyCodes);
//        android:entries="@array/currency_names"
//        android:entryValues="@array/key_currency_codes"
    }

    @Override
    public void onResume() {
        super.onResume();

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String keyChartCurrency = getString(R.string.key_chart_currency);
        Preference pref = findPreference(keyChartCurrency);
        String chartCurrency = sharedPreferences.getString(keyChartCurrency, null);
        if (chartCurrency != null && !chartCurrency.trim().isEmpty()) {
            pref.setSummary(chartCurrency);
        } else {
            pref.setSummary(sharedPreferences.getString(getString(R.string.key_default_currency), Money.DEFAULT_CURRENCY_CODE));
        }

    }

}

@@ -853,32 +853,97 @@
 nce.Preference;%0A
+import android.preference.Preference.OnPreferenceChangeListener;%0A
 import android.p
@@ -972,16 +972,16 @@
 agment;%0A
-
 import a
@@ -1240,24 +1240,77 @@
 del.Money;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0A
 import java.
@@ -1534,16 +1534,54 @@
 ragment 
+implements OnPreferenceChangeListener 
 %7B%0A%0A    @
@@ -2407,48 +2407,8 @@
 ));%0A
-        pref.setEntries(currencyCodes);%0A
     
@@ -2447,26 +2447,24 @@
 Codes);%0A
-//
         
 android:
@@ -2459,32 +2459,182 @@
     
-android:entries=%22@
+pref.setOnPreferenceChangeListener(this);%0A%0A%0A        List%3CString%3E currencyNames = new ArrayList%3C%3E();%0A        String%5B%5D allCurrencyNames = getResources().getStringArray(R.
 array
-/
+.
 curr
@@ -2647,20 +2647,19 @@
 ames
-%22%0A//
+);%0A
         
 andr
@@ -2658,36 +2658,92 @@
     
-android:entryValues=%22@
+List%3CString%3E allCurrencyCodes = Arrays.asList(getResources().getStringArray(R.
 array
-/
+.
 key_
@@ -2756,17 +2756,237 @@
 cy_codes
-%22
+));%0A        for (String code : currencyCodes) %7B%0A            currencyNames.add(allCurrencyNames%5BallCurrencyCodes.indexOf(code)%5D);%0A        %7D%0A%0A        pref.setEntries(currencyNames.toArray(new String%5BcurrencyNames.size()%5D));
 %0A    %7D%0A%0A
@@ -3661,10 +3661,393 @@
 %0A    %7D%0A%0A
+    @Override%0A    public boolean onPreferenceChange(Preference preference, Object newValue) %7B%0A        preference.setSummary(newValue.toString());%0A        PreferenceManager.getDefaultSharedPreferences(getActivity())%0A                .edit()%0A                .putString(getString(R.string.key_chart_currency), newValue.toString())%0A                .commit();%0A%0A        return true;%0A    %7D%0A%0A
 %7D%0A

Save user preferences. List currency names instead of codes