package de.apps4ics.mountainnavigation;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;

import de.apps4ics.mountainnavigation.handlers.WeatherHandler;

/**
 * This file is part of MountainNavigation.
 *
 * MountainNavigation is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MountainNavigation is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MountainNavigation.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @copyright Copyright (c) 2016 Vinzenz Rosenkanz <vinzenz.rosenkranz@gmail.com>
 *
 * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
 */
public class SettingsActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FragmentManager mFragmentManager = getFragmentManager();
        FragmentTransaction mFragmentTransaction = mFragmentManager
                .beginTransaction();
        PrefsFragment mPrefsFragment = new PrefsFragment();
        mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
        mFragmentTransaction.commit();

    }

    public static int getMaxColdFromPrefs() {
        return getMaxColdFromPrefs(WeatherHandler.MAX_COLD_TEMP_DEFAULT);
    }

    public static int getMaxColdFromPrefs(int defaultValue) {
        return getIntWithMinFromPrefs(R.string.preferences_maximal_cold_temp_min, R.string.settings_hint_max_cold_temp_key, defaultValue);
    }

    public static int getMinHotFromPrefs() {
        return getMinHotFromPrefs(WeatherHandler.MIN_HOT_TEMP_DEFAULT);
    }

    public static int getMinHotFromPrefs(int defaultValue) {
        return getIntWithMinFromPrefs(R.string.preferences_minimal_hot_temp_min, R.string.settings_hint_min_hot_temp_key, defaultValue);
    }

    public static int getDaysForecastFromPrefs() {
        return getDaysForecastFromPrefs(WeatherHandler.MAX_DAYS_FORECAST_DEFAULT);
    }

    public static int getDaysForecastFromPrefs(int defaultValue) {
        return getIntWithMinFromPrefs(R.string.preferences_days_of_forecast_min, R.string.settings_weather_days_forecast_key, defaultValue);
    }

    private static int getIntWithMinFromPrefs(int minId, int resId, int defaultValue) {
        Resources res = MainActivity.getRes();
        SharedPreferences sharedPrefs = MainActivity.getSharedPrefs();
        int min = Integer.parseInt(res.getString(minId));
        return sharedPrefs.getInt(res.getString(resId), (defaultValue-min)) + min;
    }

    public static class PrefsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            if(key.equals(getString(R.string.settings_hint_enable_key))) {
                boolean val = sharedPreferences.getBoolean(key, true);
                PreferenceCategory hintCategory = (PreferenceCategory) findPreference(getString(R.string.settings_hint_key));
                for(int i=0; i<hintCategory.getPreferenceCount(); i++) {
                    Preference p = hintCategory.getPreference(i);
                    if(!p.getKey().equals(key)) p.setEnabled(val); //enable/disable hint options if hints are enabled/disabled
                }
            }
        }

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

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