package moe.minori.pgpclipper.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;

import moe.minori.pgpclipper.PGPClipperService;
import moe.minori.pgpclipper.R;

public class PGPClipperSettingsActivity extends AppCompatActivity {

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingFragment()).commit();
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

    public static class SettingFragment extends PreferenceFragment {

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

            addPreferencesFromResource(R.xml.pref_general);

            findPreference("enabledCheckBox").setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {

                    if ((boolean) newValue)
                        // enabled
                        getActivity().startService(new Intent(getActivity(), PGPClipperService.class));
                    else
                        // disabled
                        getActivity().stopService(new Intent(getActivity(), PGPClipperService.class));
                    return true;
                }
            });

            findPreference("issueTrackerPreferenceItem").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {

                    Intent webBrowserLaunchIntent = new Intent(Intent.ACTION_VIEW);
                    webBrowserLaunchIntent.setData(Uri.parse("https://github.com/Mnkai/PGPClipper"));
                    startActivity(webBrowserLaunchIntent);

                    return true;
                }
            });

            findPreference("licensePreferenceItem").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {

                    Intent webBrowserLaunchIntent = new Intent(Intent.ACTION_VIEW);
                    webBrowserLaunchIntent.setData(Uri.parse("https://github.com/Mnkai/PGPClipper/blob/master/LICENSE"));
                    startActivity(webBrowserLaunchIntent);

                    return true;
                }
            });

            findPreference("thirdPartyLicensePreferenceItem").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {

                    Intent webBrowserLaunchIntent = new Intent(Intent.ACTION_VIEW);
                    webBrowserLaunchIntent.setData(Uri.parse("https://github.com/Mnkai/PGPClipper/blob/master/LICENSE"));
                    startActivity(webBrowserLaunchIntent);

                    return true;
                }
            });

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());

            final ListPreference themePref = (ListPreference) findPreference("themeSelection");

            themePref.setEntryValues(R.array.themes_values);
            themePref.setEntries(R.array.themes);

            String currentVal = sharedPreferences.getString("themeSelection", "dark");
            switch (currentVal)
            {
                case "dark":
                    themePref.setSummary(getResources().getString(R.string.darkText));
                    break;
                case "light":
                    themePref.setSummary(getResources().getString(R.string.lightText));
                    break;
            }

            themePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {

                    switch ((String) newValue) {
                        case "dark":
                            themePref.setSummary(getResources().getString(R.string.darkText));
                            break;
                        case "light":
                            themePref.setSummary(getResources().getString(R.string.lightText));
                            break;
                    }

                    return true;
                }
            });

            final CheckBoxPreference enabledPref = (CheckBoxPreference)findPreference("enabledCheckBox");

            findPreference("pgpServiceProviderApp").setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    String providerApp = (String) newValue;
                    if (providerApp == null || "".equals(providerApp)) {
                        enabledPref.setEnabled(false);
                        enabledPref.setChecked(false);
                        getActivity().stopService(new Intent(getActivity(), PGPClipperService.class));
                    } else {
                        enabledPref.setEnabled(true);
                    }
                    return true;
                }
            });
            String providerApp = sharedPreferences.getString("pgpServiceProviderApp",null);
            if(providerApp == null || "".equals(providerApp)){
                enabledPref.setEnabled(false);
                enabledPref.setChecked(false);
                getActivity().stopService(new Intent(getActivity(), PGPClipperService.class));
            }else {
                if (enabledPref.isChecked()) {
                    getActivity().startService(new Intent(getActivity(), PGPClipperService.class));
                }
                enabledPref.setEnabled(true);
            }
        }
    }
}

@@ -3514,16 +3514,22 @@
         
+final 
 SharedPr
@@ -3643,16 +3643,94 @@
 text());
+%0A            final SharedPreferences.Editor editor = sharedPreferences.edit();
 %0A%0A      
@@ -6512,32 +6512,1547 @@
 );%0A            %7D
+%0A%0A            // for NFC authentication%0A%0A            findPreference(%22enableNFCAuth%22).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() %7B%0A                @Override%0A                public boolean onPreferenceChange(Preference preference, Object newValue) %7B%0A                    if ( (boolean) newValue == false )%0A                    %7B%0A                        // delete current hash and encrypted data%0A%0A                        editor.remove(%22deviceSalt%22);%0A                        editor.remove(%22encryptedKeyPass%22);%0A%0A                        editor.commit();%0A                    %7D%0A                    else%0A                    %7B%0A                        // start NFCAuthSetupActivity%0A                        Intent intent = new Intent(getActivity(), NFCAuthenticationSetupActivity.class);%0A%0A                        startActivityForResult(intent, 7272);%0A                    %7D%0A                    return true;%0A                %7D%0A            %7D);%0A        %7D%0A%0A        @Override%0A        public void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A            super.onActivityResult(requestCode, resultCode, data);%0A%0A            if ( requestCode == 7272 ) // NFCAuthSetupResult%0A            %7B%0A                if ( resultCode != 0 ) // error or user canceled auth operation%0A                %7B%0A                    CheckBoxPreference checkBoxPreference = (CheckBoxPreference) findPreference(%22enableNFCAuth%22);%0A                    checkBoxPreference.setChecked(false);%0A                %7D%0A            %7D
 %0A        %7D%0A    %7D

add NFCAuth start routine when pref is checked