package xyz.myachin.saveto.ui.settings

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import xyz.myachin.saveto.R
import xyz.myachin.saveto.logic.intent.*
import xyz.myachin.saveto.settings.Settings
import xyz.myachin.saveto.ui.sharing.RequestAccessToFolderActivity

class MimeTypesSettingsActivity : 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 mDocumentPref: Preference? = null
        private var mForgetDocumentPref: Preference? = null

        private var mArchivePref: Preference? = null
        private var mForgetArchivePref: Preference? = null

        private var mImagePref: Preference? = null
        private var mForgetImagePref: Preference? = null

        private var mVideoPref: Preference? = null
        private var mForgetVideoPref: Preference? = null

        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.mime_type_preferences, rootKey)
            findMainPrefs()
            findDisablingPrefs()
        }

        private fun findMainPrefs() {
            val requestIntent =
                Intent(context?.applicationContext!!, RequestAccessToFolderActivity::class.java)
            mDocumentPref = findPreference(getString(R.string.select_doc_dir_key))
            mDocumentPref?.run {
                setPrefText(this, Settings.documentUri)
                onPreferenceClickListener = Preference.OnPreferenceClickListener {
                    requestIntent.action = ACTION_GET_ACCESS_FOR_DOCUMENTS
                    startActivityForResult(requestIntent, ACCESS_DOCUMENTS_CODE)
                    true
                }
            }
            mArchivePref = findPreference(getString(R.string.select_archive_dir_key))
            mArchivePref?.run {
                setPrefText(this, Settings.archiveUri)
                onPreferenceClickListener = Preference.OnPreferenceClickListener {
                    requestIntent.action = ACTION_GET_ACCESS_FOR_ARCHIVES
                    startActivityForResult(requestIntent, ACCESS_ARCHIVES_CODE)
                    true
                }
            }
            mImagePref = findPreference(getString(R.string.select_image_dir_key))
            mImagePref?.run {
                setPrefText(this, Settings.imageUri)
                onPreferenceClickListener = Preference.OnPreferenceClickListener {
                    requestIntent.action = ACTION_GET_ACCESS_FOR_IMAGES
                    startActivityForResult(requestIntent, ACCESS_IMAGES_CODE)
                    true
                }
            }
            mVideoPref = findPreference(getString(R.string.select_video_dir_key))
            mVideoPref?.run {
                setPrefText(this, Settings.videoUri)
                onPreferenceClickListener = Preference.OnPreferenceClickListener {
                    requestIntent.action = ACTION_GET_ACCESS_FOR_VIDEOS
                    startActivityForResult(requestIntent, ACCESS_VIDEOS_CODE)
                    true
                }
            }
        }

        private fun findDisablingPrefs() {
            mForgetDocumentPref = findPreference(getString(R.string.forget_docs))
            mForgetDocumentPref?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
                it.isVisible = false
                Settings.documentUri = null
                setPrefText(mDocumentPref, null)
                true
            }
            if (Settings.documentUri != null) mForgetDocumentPref?.isVisible = true

            mForgetArchivePref = findPreference(getString(R.string.forget_archives))
            mForgetArchivePref?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
                it.isVisible = false
                Settings.archiveUri = null
                setPrefText(mArchivePref, null)
                true
            }
            if (Settings.archiveUri != null) mForgetArchivePref?.isVisible = true

            mForgetImagePref = findPreference(getString(R.string.forget_images))
            mForgetImagePref?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
                it.isVisible = false
                Settings.imageUri = null
                setPrefText(mImagePref, null)
                true
            }
            if (Settings.imageUri != null) mForgetImagePref?.isVisible = true

            mForgetVideoPref = findPreference(getString(R.string.forget_videos))
            mForgetVideoPref?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
                it.isVisible = false
                Settings.videoUri = null
                setPrefText(mVideoPref, null)
                true
            }
            if (Settings.videoUri != null) mForgetVideoPref?.isVisible = true
        }

        private fun setPrefText(pref: Preference?, uri: Uri?) {
            if (uri != null) {
                pref?.title = getString(R.string.change_change_folder)
                return
            }
            pref?.title = getString(R.string.select_directory_title)
        }

        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            when (requestCode) {
                ACCESS_DOCUMENTS_CODE -> {
                    if (RESULT_OK == resultCode) {
                        setPrefText(mDocumentPref!!, Settings.documentUri)
                        mForgetDocumentPref?.isVisible = true
                    }
                }
                ACCESS_ARCHIVES_CODE -> {
                    if (RESULT_OK == resultCode) {
                        setPrefText(mArchivePref!!, Settings.archiveUri)
                        mForgetArchivePref?.isVisible = true
                    }
                }
                ACCESS_IMAGES_CODE -> {
                    if (RESULT_OK == resultCode) {
                        setPrefText(mImagePref!!, Settings.imageUri)
                        mForgetImagePref?.isVisible = true
                    }
                }
                ACCESS_VIDEOS_CODE -> {
                    if (RESULT_OK == resultCode) {
                        setPrefText(mVideoPref!!, Settings.videoUri)
                        mForgetVideoPref?.isVisible = true
                    }
                }
            }
        }
    }
}