package xyz.myachin.saveto.ui.settings

import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import xyz.myachin.saveto.R
import xyz.myachin.saveto.settings.Settings
import xyz.myachin.saveto.ui.sharing.direct.DirectShareActivity
import xyz.myachin.saveto.ui.sharing.select.ChooserShareActivity

class SettingsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)
        if (savedInstanceState == null) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings, SettingsFragment())
                .commit()
        }
        supportActionBar?.setDisplayShowHomeEnabled(true)
    }

    class SettingsFragment : PreferenceFragmentCompat() {
        private var directSaving: SwitchPreferenceCompat? = null
        private var manualSaving: SwitchPreferenceCompat? = null

        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey)

            setDirectSavingFeature()
            setManualSavingFeature()
        }

        private fun setManualSavingFeature() {
            manualSaving = findPreference(getString(R.string.allow_manual_saving))
            manualSaving?.onPreferenceChangeListener =
                Preference.OnPreferenceChangeListener { _, newValue ->
                    if (context?.applicationContext != null) {
                        val state = when (newValue as Boolean) {
                            true -> PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
                            false -> PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                        }
                        Settings.setComponentState(
                            context?.applicationContext!!,
                            ChooserShareActivity::class.java.name, state
                        )
                        true
                    } else false
                }
        }

        private fun setDirectSavingFeature() {
            directSaving = findPreference(getString(R.string.allow_direct_saving))
            directSaving?.onPreferenceChangeListener =
                Preference.OnPreferenceChangeListener { _, newValue ->
                    if (context?.applicationContext != null) {
                        val state = when (newValue as Boolean) {
                            true -> PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
                            false -> {
                                Settings.documentUri = null
                                Settings.archiveUri = null
                                Settings.imageUri = null
                                Settings.videoUri = null
                                PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                            }
                        }
                        Settings.setComponentState(
                            context?.applicationContext!!,
                            DirectShareActivity::class.java.name,
                            state
                        )
                        true
                    } else false
                }
        }
    }
}