// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package org.yuzu.yuzu_emu.features.settings.ui

import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.text.TextUtils
import android.widget.Toast
import androidx.preference.PreferenceManager
import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.YuzuApplication
import org.yuzu.yuzu_emu.features.settings.model.AbstractBooleanSetting
import org.yuzu.yuzu_emu.features.settings.model.AbstractIntSetting
import org.yuzu.yuzu_emu.features.settings.model.BooleanSetting
import org.yuzu.yuzu_emu.features.settings.model.ByteSetting
import org.yuzu.yuzu_emu.features.settings.model.IntSetting
import org.yuzu.yuzu_emu.features.settings.model.LongSetting
import org.yuzu.yuzu_emu.features.settings.model.Settings
import org.yuzu.yuzu_emu.features.settings.model.ShortSetting
import org.yuzu.yuzu_emu.features.settings.model.view.*
import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile
import org.yuzu.yuzu_emu.model.SettingsViewModel
import org.yuzu.yuzu_emu.utils.NativeConfig

class SettingsFragmentPresenter(
    private val settingsViewModel: SettingsViewModel,
    private val adapter: SettingsAdapter,
    private var menuTag: String,
    private var gameId: String
) {
    private var settingsList = ArrayList<SettingsItem>()

    private val preferences: SharedPreferences
        get() = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext)

    private val context: Context get() = YuzuApplication.appContext

    // Extension for populating settings list based on paired settings
    fun ArrayList<SettingsItem>.add(key: String) {
        val item = SettingsItem.settingsItems[key]!!
        val pairedSettingKey = item.setting.pairedSettingKey
        if (pairedSettingKey.isNotEmpty()) {
            val pairedSettingValue = NativeConfig.getBoolean(pairedSettingKey, false)
            if (!pairedSettingValue) return
        }
        add(item)
    }

    fun onViewCreated() {
        loadSettingsList()
    }

    fun loadSettingsList() {
        if (!TextUtils.isEmpty(gameId)) {
            settingsViewModel.setToolbarTitle(
                context.getString(
                    R.string.advanced_settings_game,
                    gameId
                )
            )
        }

        val sl = ArrayList<SettingsItem>()
        when (menuTag) {
            SettingsFile.FILE_NAME_CONFIG -> addConfigSettings(sl)
            Settings.SECTION_GENERAL -> addGeneralSettings(sl)
            Settings.SECTION_SYSTEM -> addSystemSettings(sl)
            Settings.SECTION_RENDERER -> addGraphicsSettings(sl)
            Settings.SECTION_AUDIO -> addAudioSettings(sl)
            Settings.SECTION_THEME -> addThemeSettings(sl)
            Settings.SECTION_DEBUG -> addDebugSettings(sl)
            else -> {
                val context = YuzuApplication.appContext
                Toast.makeText(
                    context,
                    context.getString(R.string.unimplemented_menu),
                    Toast.LENGTH_SHORT
                ).show()
                return
            }
        }
        settingsList = sl
        adapter.submitList(settingsList)
    }

    private fun addConfigSettings(sl: ArrayList<SettingsItem>) {
        settingsViewModel.setToolbarTitle(context.getString(R.string.advanced_settings))
        sl.apply {
            add(SubmenuSetting(R.string.preferences_general, 0, Settings.SECTION_GENERAL))
            add(SubmenuSetting(R.string.preferences_system, 0, Settings.SECTION_SYSTEM))
            add(SubmenuSetting(R.string.preferences_graphics, 0, Settings.SECTION_RENDERER))
            add(SubmenuSetting(R.string.preferences_audio, 0, Settings.SECTION_AUDIO))
            add(SubmenuSetting(R.string.preferences_debug, 0, Settings.SECTION_DEBUG))
            add(
                RunnableSetting(R.string.reset_to_default, 0, false) {
                    settingsViewModel.setShouldShowResetSettingsDialog(true)
                }
            )
        }
    }

    private fun addGeneralSettings(sl: ArrayList<SettingsItem>) {
        settingsViewModel.setToolbarTitle(context.getString(R.string.preferences_general))
        sl.apply {
            add(BooleanSetting.RENDERER_USE_SPEED_LIMIT.key)
            add(ShortSetting.RENDERER_SPEED_LIMIT.key)
            add(IntSetting.CPU_ACCURACY.key)
            add(BooleanSetting.PICTURE_IN_PICTURE.key)
        }
    }

    private fun addSystemSettings(sl: ArrayList<SettingsItem>) {
        settingsViewModel.setToolbarTitle(context.getString(R.string.preferences_system))
        sl.apply {
            add(BooleanSetting.USE_DOCKED_MODE.key)
            add(IntSetting.REGION_INDEX.key)
            add(IntSetting.LANGUAGE_INDEX.key)
            add(BooleanSetting.USE_CUSTOM_RTC.key)
            add(LongSetting.CUSTOM_RTC.key)
        }
    }

    private fun addGraphicsSettings(sl: ArrayList<SettingsItem>) {
        settingsViewModel.setToolbarTitle(context.getString(R.string.preferences_graphics))
        sl.apply {
            add(IntSetting.RENDERER_ACCURACY.key)
            add(IntSetting.RENDERER_RESOLUTION.key)
            add(IntSetting.RENDERER_VSYNC.key)
            add(IntSetting.RENDERER_SCALING_FILTER.key)
            add(IntSetting.RENDERER_ANTI_ALIASING.key)
            add(IntSetting.RENDERER_SCREEN_LAYOUT.key)
            add(IntSetting.RENDERER_ASPECT_RATIO.key)
            add(BooleanSetting.RENDERER_USE_DISK_SHADER_CACHE.key)
            add(BooleanSetting.RENDERER_FORCE_MAX_CLOCK.key)
            add(BooleanSetting.RENDERER_ASYNCHRONOUS_SHADERS.key)
            add(BooleanSetting.RENDERER_REACTIVE_FLUSHING.key)
        }
    }

    private fun addAudioSettings(sl: ArrayList<SettingsItem>) {
        settingsViewModel.setToolbarTitle(context.getString(R.string.preferences_audio))
        sl.apply {
            add(IntSetting.AUDIO_OUTPUT_ENGINE.key)
            add(ByteSetting.AUDIO_VOLUME.key)
        }
    }

    private fun addThemeSettings(sl: ArrayList<SettingsItem>) {
        settingsViewModel.setToolbarTitle(context.getString(R.string.preferences_theme))
        sl.apply {
            val theme: AbstractIntSetting = object : AbstractIntSetting {
                override val int: Int
                    get() = preferences.getInt(Settings.PREF_THEME, 0)

                override fun setInt(value: Int) {
                    preferences.edit()
                        .putInt(Settings.PREF_THEME, value)
                        .apply()
                    settingsViewModel.setShouldRecreate(true)
                }

                override val key: String = Settings.PREF_THEME
                override val category = Settings.Category.UiGeneral
                override val isRuntimeModifiable: Boolean = false
                override val defaultValue: Int = 0
                override fun reset() {
                    preferences.edit()
                        .putInt(Settings.PREF_THEME, defaultValue)
                        .apply()
                }
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                add(
                    SingleChoiceSetting(
                        theme,
                        R.string.change_app_theme,
                        0,
                        R.array.themeEntriesA12,
                        R.array.themeValuesA12
                    )
                )
            } else {
                add(
                    SingleChoiceSetting(
                        theme,
                        R.string.change_app_theme,
                        0,
                        R.array.themeEntries,
                        R.array.themeValues
                    )
                )
            }

            val themeMode: AbstractIntSetting = object : AbstractIntSetting {
                override val int: Int
                    get() = preferences.getInt(Settings.PREF_THEME_MODE, -1)

                override fun setInt(value: Int) {
                    preferences.edit()
                        .putInt(Settings.PREF_THEME_MODE, value)
                        .apply()
                    settingsViewModel.setShouldRecreate(true)
                }

                override val key: String = Settings.PREF_THEME_MODE
                override val category = Settings.Category.UiGeneral
                override val isRuntimeModifiable: Boolean = false
                override val defaultValue: Int = -1
                override fun reset() {
                    preferences.edit()
                        .putInt(Settings.PREF_BLACK_BACKGROUNDS, defaultValue)
                        .apply()
                    settingsViewModel.setShouldRecreate(true)
                }
            }

            add(
                SingleChoiceSetting(
                    themeMode,
                    R.string.change_theme_mode,
                    0,
                    R.array.themeModeEntries,
                    R.array.themeModeValues
                )
            )

            val blackBackgrounds: AbstractBooleanSetting = object : AbstractBooleanSetting {
                override val boolean: Boolean
                    get() = preferences.getBoolean(Settings.PREF_BLACK_BACKGROUNDS, false)

                override fun setBoolean(value: Boolean) {
                    preferences.edit()
                        .putBoolean(Settings.PREF_BLACK_BACKGROUNDS, value)
                        .apply()
                    settingsViewModel.setShouldRecreate(true)
                }

                override val key: String = Settings.PREF_BLACK_BACKGROUNDS
                override val category = Settings.Category.UiGeneral
                override val isRuntimeModifiable: Boolean = false
                override val defaultValue: Boolean = false
                override fun reset() {
                    preferences.edit()
                        .putBoolean(Settings.PREF_BLACK_BACKGROUNDS, defaultValue)
                        .apply()
                    settingsViewModel.setShouldRecreate(true)
                }
            }

            add(
                SwitchSetting(
                    blackBackgrounds,
                    R.string.use_black_backgrounds,
                    R.string.use_black_backgrounds_description
                )
            )
        }
    }

    private fun addDebugSettings(sl: ArrayList<SettingsItem>) {
        settingsViewModel.setToolbarTitle(context.getString(R.string.preferences_debug))
        sl.apply {
            add(HeaderSetting(R.string.gpu))
            add(IntSetting.RENDERER_BACKEND.key)
            add(BooleanSetting.RENDERER_DEBUG.key)

            add(HeaderSetting(R.string.cpu))
            add(BooleanSetting.CPU_DEBUG_MODE.key)
            add(SettingsItem.FASTMEM_COMBINED)
        }
    }
}
