package ch.redacted.ui.settings;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;

import ch.redacted.REDApplication;
import ch.redacted.app.BuildConfig;
import ch.redacted.app.R;
import ch.redacted.data.local.PreferencesHelper;
import net.rdrei.android.dirchooser.DirectoryChooserActivity;
import net.rdrei.android.dirchooser.DirectoryChooserConfig;

public class SettingsFragment extends PreferenceFragmentCompat {

    private PreferencesHelper mPreferenceHelper;

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.preferences);
        mPreferenceHelper = ((REDApplication) getActivity().getApplication()).getComponent().preferencesHelper();
        Preference versionPreference = findPreference("pref_verion_name");
        versionPreference.setTitle(BuildConfig.VERSION_NAME);

        Preference downloadLocation = findPreference(PreferencesHelper.PREF_DEFAULT_DOWNLOAD_LOCATION);

        downloadLocation.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override public boolean onPreferenceClick(Preference preference) {
                final Intent chooserIntent = new Intent(getActivity(), DirectoryChooserActivity.class);

                final DirectoryChooserConfig config = DirectoryChooserConfig.builder()
                    .allowReadOnlyDirectory(true)
                    .allowNewDirectoryNameModification(false)
                    .build();

                chooserIntent.putExtra(DirectoryChooserActivity.EXTRA_CONFIG, config);
                getActivity().startActivityForResult(chooserIntent, 0);
                return false;
            }
        });

    }

    @Override
    public void onPause() {
        super.onPause();
        EditTextPreference preference = (EditTextPreference) findPreference(PreferencesHelper.PREF_WM_HOST_KEY);
        mPreferenceHelper.setWmHost(preference.getText());

        EditTextPreference preference2 = (EditTextPreference) findPreference(PreferencesHelper.PREF_WM_PASSWORD_KEY);
        mPreferenceHelper.setWmPassword(preference2.getText());

        EditTextPreference preference3 = (EditTextPreference) findPreference(PreferencesHelper.PREF_WM_PORT_KEY);
        mPreferenceHelper.setWmPort(preference3.getText());

        //todo rename these to something useful, ugh
        //todo error checking on url input

        //todo return an error if host or url is null
        EditTextPreference preference4 = (EditTextPreference) findPreference(PreferencesHelper.PREF_WM_USER_KEY);
        mPreferenceHelper.setWmUser(preference4.getText());

        ListPreference preference5 = (ListPreference) findPreference(PreferencesHelper.PREF_DEFAULT_DOWNLOAD_KEY);
        mPreferenceHelper.setDownloadMethod(preference5.getValue());

        CheckBoxPreference preference6 = (CheckBoxPreference) findPreference(PreferencesHelper.PREF_LOAD_IMAGES);
        mPreferenceHelper.setLoadImages(preference6.isChecked());

        ListPreference preference7 = (ListPreference) findPreference(PreferencesHelper.PREF_TOP_TORRENT_LIMIT);
        mPreferenceHelper.setTopTorrentLimit(Integer.parseInt(preference7.getValue()));

        EditTextPreference preference8 = (EditTextPreference) findPreference(PreferencesHelper.PREF_PYWA_HOST_KEY);
        mPreferenceHelper.setPywaHost(preference8.getText());

        EditTextPreference preference9 = (EditTextPreference) findPreference(
            PreferencesHelper.PREF_PYWA_PASSWORD_KEY);
        mPreferenceHelper.setPywaPassword(preference9.getText());

        EditTextPreference preference10 = (EditTextPreference) findPreference(
            PreferencesHelper.PREF_PYWA_PORT_KEY);
        mPreferenceHelper.setPywaPort(preference10.getText());

    }
}

@@ -27,16 +27,41 @@
 tings;%0A%0A
+import android.Manifest;%0A
 import a
@@ -535,16 +535,107 @@
 Helper;%0A
+import com.tbruyelle.rxpermissions2.RxPermissions;%0Aimport io.reactivex.functions.Consumer;%0A
 import n
@@ -1529,24 +1529,419 @@
 eference) %7B%0A
+%0A                new RxPermissions(getActivity())%0A                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)%0A                    .subscribe(new Consumer%3CBoolean%3E() %7B%0A                        @Override%0A                        public void accept(Boolean granted) throws Exception %7B%0A                            if (granted) %7B%0A                
             
@@ -2045,24 +2045,40 @@
             
+   
+             
 final Direct
@@ -2152,24 +2152,40 @@
             
+                
 .allowReadOn
@@ -2202,16 +2202,96 @@
 y(true)%0A
+                                    .newDirectoryName(%22NewDir%22)%0A                
         
@@ -2364,16 +2364,32 @@
         
+          
+      
 .build()
@@ -2387,24 +2387,40 @@
  .build();%0A%0A
+                
             
@@ -2494,16 +2494,32 @@
 onfig);%0A
+                
         
@@ -2582,16 +2582,96 @@
 nt, 0);%0A
+                            %7D%0A                        %7D%0A                    %7D);%0A
         

Add missing required parameter to directory picker, request permission on click