package com.studio4plus.homerplayer.ui.settings;

import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.preference.Preference;
import android.widget.Toast;

import com.studio4plus.homerplayer.BuildConfig;
import com.studio4plus.homerplayer.GlobalSettings;
import com.studio4plus.homerplayer.HomerPlayerApplication;
import com.studio4plus.homerplayer.R;
import com.studio4plus.homerplayer.model.AudioBookManager;

import java.util.Locale;

import javax.inject.Inject;

public class MainSettingsFragment extends BaseSettingsFragment {

    private static final String KEY_RESET_ALL_BOOK_PROGRESS = "reset_all_book_progress_preference";
    private static final String KEY_FAQ = "faq_preference";
    private static final String KEY_VERSION = "version_preference";

    private static final String FAQ_URL = "https://goo.gl/1RVxFW";

    @Inject public AudioBookManager audioBookManager;
    @Inject public GlobalSettings globalSettings;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        HomerPlayerApplication.getComponent(getActivity()).inject(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onCreatePreferences(Bundle bundle, String rootKey) {
        setPreferencesFromResource(R.xml.preferences_main, rootKey);

        ConfirmDialogPreference preferenceResetProgress =
                (ConfirmDialogPreference) findPreference(KEY_RESET_ALL_BOOK_PROGRESS);
        preferenceResetProgress.setOnConfirmListener(new ConfirmDialogPreference.OnConfirmListener() {
            @Override
            public void onConfirmed() {
                audioBookManager.resetAllBookProgress();
                Toast.makeText(
                        getActivity(),
                        R.string.pref_reset_all_book_progress_done,
                        Toast.LENGTH_SHORT).show();
            }
        });
        setupFaq();
        updateVersionSummary();
    }

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

        SharedPreferences sharedPreferences = getSharedPreferences();
        updateKioskModeSummary();
    }

    @Override
    protected int getTitle() {
        return R.string.settings_title;
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        switch(key) {
            case GlobalSettings.KEY_SIMPLE_KIOSK_MODE:
            case GlobalSettings.KEY_KIOSK_MODE:
                updateKioskModeSummary();
                break;
        }
    }

    private void updateVersionSummary() {
        Preference preference = findPreference(KEY_VERSION);
        String versionString = String.format(
                Locale.US, "%s (%d)", BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE);
        preference.setSummary(versionString);
    }

    private void updateKioskModeSummary() {
        Preference kioskModeScreen =
                findPreference(GlobalSettings.KEY_KIOSK_MODE_SCREEN);

        int summaryStringId = R.string.pref_kiosk_mode_screen_summary_disabled;
        if (globalSettings.isFullKioskModeEnabled())
            summaryStringId = R.string.pref_kiosk_mode_screen_summary_full;
        else if (globalSettings.isSimpleKioskModeEnabled())
            summaryStringId = R.string.pref_kiosk_mode_screen_summary_simple;
        kioskModeScreen.setSummary(summaryStringId);
    }

    private void setupFaq() {
        Preference preference = findPreference(KEY_FAQ);
        preference.setSummary(getString(R.string.pref_help_faq_summary, FAQ_URL));
        preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                openUrl(FAQ_URL);
                return true;
            }
        });
    }
}

@@ -1071,19 +1071,23 @@
 mponent(
-get
+require
 Activity
@@ -1386,38 +1386,11 @@
     
-(ConfirmDialogPreference) find
+get
 Pref
@@ -1483,123 +1483,16 @@
 ner(
-new ConfirmDialogPreference.OnConfirmListener() %7B%0A            @Override%0A            public void onConfirmed() %7B%0A   
+() -%3E %7B%0A
     
@@ -1499,17 +1499,16 @@
         
- 
 audioBoo
@@ -1536,28 +1536,24 @@
 Progress();%0A
-    
             
@@ -1588,20 +1588,16 @@
         
-    
 getActiv
@@ -1623,20 +1623,16 @@
         
- 
-   
 R.string
@@ -1683,28 +1683,24 @@
             
-    
 Toast.LENGTH
@@ -1715,30 +1715,16 @@
 show();%0A
-            %7D%0A
         
@@ -1856,79 +1856,8 @@
 t();
-%0A%0A        SharedPreferences sharedPreferences = getSharedPreferences();
 %0A   
@@ -2366,36 +2366,35 @@
 ce preference = 
-find
+get
 Preference(KEY_V
@@ -2676,29 +2676,12 @@
 en =
-%0A                find
+ get
 Pref
@@ -3200,12 +3200,11 @@
 e = 
-find
+get
 Pref
@@ -3355,149 +3355,25 @@
 ner(
-new Preference.OnPreferenceClickListener() %7B%0A            @Override%0A            public boolean onPreferenceClick(Preference preference) %7B%0A    
+preference1 -%3E %7B%0A
     
@@ -3406,28 +3406,24 @@
             
-    
 return true;
@@ -3427,22 +3427,8 @@
 ue;%0A
-            %7D%0A
     

Fix some warnings.