package com.shing.aputimetable;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.shing.aputimetable.model.ApuClassContract;
import com.shing.aputimetable.utils.QueryUtils;

import org.greenrobot.eventbus.EventBus;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        boolean useDarkTheme = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("dark_theme", false);

        if (useDarkTheme) {
            setTheme(R.style.AppTheme_Dark);
        }
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_settings);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new SettingsFragment()).commit();
        }

    }

    public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

        private SharedPreferences prefs;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

            EditTextPreference p = (EditTextPreference) findPreference("intake_code");
            String intake = prefs.getString("intake_code", "");
            if (intake.equals("")) {
                p.setSummary("Please provide your Intake Code");
            } else {
                p.setSummary(intake);
            }

//            findPreference("intake_code").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
//                public boolean onPreferenceClick(Preference preference) {
//                    Toast.makeText(getActivity(),"clicked",Toast.LENGTH_SHORT).show();
//                    return true;
//                }
//            });

            findPreference("intake_code").setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object o) {
                    Boolean save = true;
                    if (false) {
                        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setTitle("Invalid Input");
                        builder.setMessage("Something's gone wrong...");
                        builder.setPositiveButton(android.R.string.ok, null);
                        builder.show();
                        save = false;
                    }
                    return save;
                }
            });

        }

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

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

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            switch (key) {
                case "dark_theme":
                    getActivity().recreate();
                    EventBus.getDefault().post(new ReloadEvent());
                    break;
                case "intake_code":
                    EditTextPreference p = (EditTextPreference) findPreference(key);
                    p.setText(p.getText().toUpperCase());
                    p.setSummary(p.getText());
                    Thread t = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                getActivity().getContentResolver().delete(ApuClassContract.ApuClassEntry.CONTENT_URI, null, null);
                                QueryUtils.getAllClass(prefs.getString("intake_code", ""), getActivity());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    t.start();
                    try {
                        t.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    EventBus.getDefault().post(new DataChangeEvent());
                    break;
            }
        }

    }

}

@@ -630,24 +630,81 @@
 Activity %7B%0A%0A
+    private final String DARK_THEME_KEY = %22dark_theme%22;%0A%0A
     @Overrid
@@ -857,28 +857,30 @@
 Boolean(
-%22dark_theme%22
+DARK_THEME_KEY
 , false)
@@ -1703,24 +1703,146 @@
 Listener %7B%0A%0A
+        private final String INTAKE_CODE_KEY = %22intake_code%22;%0A        private final String DARK_THEME_KEY = %22dark_theme%22;%0A
         priv
@@ -2208,29 +2208,31 @@
 ference(
-%22intake_code%22
+INTAKE_CODE_KEY
 );%0A     
@@ -2266,37 +2266,39 @@
 s.getString(
-%22intake_code%22
+INTAKE_CODE_KEY
 , %22%22);%0A     
@@ -2362,41 +2362,36 @@
 ary(
-%22Please provide your I
+R.string.i
 ntake
- Code%22
+_code_summary
 );%0A 
@@ -2463,18 +2463,16 @@
      %7D%0A%0A
-//
         
@@ -2486,37 +2486,39 @@
 dPreference(
-%22intake_code%22
+INTAKE_CODE_KEY
 ).setOnPrefe
@@ -2515,36 +2515,37 @@
 setOnPreferenceC
-lick
+hange
 Listener(new Pre
@@ -2565,20 +2565,21 @@
 ferenceC
-lick
+hange
 Listener
@@ -2579,26 +2579,50 @@
 istener() %7B%0A
-//
+                @Override%0A
             
@@ -2653,20 +2653,21 @@
 ferenceC
-lick
+hange
 (Prefere
@@ -2684,14 +2684,22 @@
 ence
+, Object o
 ) %7B%0A
-//
     
@@ -2718,114 +2718,29 @@
     
-Toast.makeText(getActivity(),%22clicked%22,Toast.LENGTH_SHORT).show();%0A//                    return
+Boolean save =
  true;%0A
-//  
     
@@ -2753,328 +2753,168 @@
     
-%7D%0A//
       
-      %7D);%0A%0A            findPreference(%22intake_code%22).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() %7B%0A                @Override%0A                public boolean onPreferenceChange(Preference preference, Object o) %7B%0A                    Boolean save = true;%0A                    if (false
+EditTextPreference p = (EditTextPreference) findPreference(INTAKE_CODE_KEY);%0A                    if (p.getEditText().getText().toString().trim().isEmpty()
 ) %7B%0A
@@ -3120,33 +3120,39 @@
 ge(%22
-Something's gone wrong...
+Empty Intake Code Is Not Allow!
 %22);%0A
@@ -3887,28 +3887,30 @@
    case 
-%22dark_theme%22
+DARK_THEME_KEY
 :%0A      
@@ -4064,29 +4064,31 @@
    case 
-%22intake_code%22
+INTAKE_CODE_KEY
 :%0A      
@@ -4644,29 +4644,31 @@
 tString(
-%22intake_code%22
+INTAKE_CODE_KEY
 , %22%22), g
@@ -5143,32 +5143,33 @@
 ChangeEvent());%0A
+%0A
                 

add empty validation to intake code preference & code refactor