package com.jimandreas.android.designlibdemo

import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import android.os.Bundle
import android.preference.PreferenceManager
import android.support.v7.app.AppCompatDelegate
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
import timber.log.Timber

class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {

    override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
        Timber.v("onSharedPrefChanged: key is %s", key)
    }

    internal interface RestartCallback {
        fun doRestart()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.pref_sandbox)
        // Preferences.sync(getPreferenceManager());

        // gist from:  https://gist.github.com/yujikosuga/1234287
        var pref = findPreference("fmi_key")
        pref.onPreferenceClickListener = Preference.OnPreferenceClickListener { preference ->
            val key = preference.key

            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse(
                    "https://github.com/jimandreas/BottomNavigationView-plus-LeakCanary/blob/master/README.md")
            startActivity(intent)
            true
        }

        pref = findPreference("nightmode")
        pref.onPreferenceClickListener = Preference.OnPreferenceClickListener { preference ->
            val key = preference.key
            val nightModeSetting = PreferenceManager
                    .getDefaultSharedPreferences(activity)
                    .getBoolean("nightmode", false)
            Timber.v("click listener: turn night mode: %s", if (nightModeSetting) "on" else "off")
            AppCompatDelegate
                    .setDefaultNightMode(if (nightModeSetting) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO)
            (activity as RestartCallback).doRestart()
            true
        }
    }

    override fun onCreatePreferences(bundle: Bundle?, s: String?) {}

    override fun onPreferenceTreeClick(preference: Preference?): Boolean {
        val key = preference?.key
        return super.onPreferenceTreeClick(preference)
    }
}