package com.example.cmpt362_group30_maze_game

import android.os.Bundle
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import android.content.Context
import androidx.appcompat.widget.SwitchCompat
import androidx.core.view.WindowCompat

class SettingsActivity : AppCompatActivity() {

    private var mEditName: EditText? = null

    private lateinit var mControlSwitch: SwitchCompat
    private lateinit var mSoundSwitch: SwitchCompat
    private lateinit var mVibrationSwitch: SwitchCompat
    private lateinit var mLeaderboardSwitch: SwitchCompat

    companion object {
        const val PREFS_NAME = "MyPrefs"
        const val KEY_SOUND = "Sound"
        const val KEY_VIBRATION = "Vibration"
        const val KEY_LEADERBOARD = "Leaderboard"
        const val KEY_MANUAL = "Manual"
        const val KEY_NAME = "Name"
    }

    //    @SuppressLint("UseSwitchCompatOrMaterialCode")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)

        // Change colour of android status bar icons so they show on white
        WindowCompat.getInsetsController(window, window.decorView).isAppearanceLightStatusBars = true

        mEditName = findViewById(R.id.editName)
        mControlSwitch = findViewById(R.id.control_switch)
        mSoundSwitch = findViewById(R.id.sound_switch)
        mVibrationSwitch = findViewById(R.id.vibration_switch)
        mLeaderboardSwitch = findViewById(R.id.leaderboard_switch)

        loadSettings() // Load settings from SharedPreferences to populate the UI


        //  Display Toasts for on/off states
        mControlSwitch.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                Toast.makeText(applicationContext, "Manual Controls Enabled", Toast.LENGTH_SHORT)
                    .show()
            } else {
                Toast.makeText(applicationContext, "Manual Controls Disabled", Toast.LENGTH_SHORT)
                    .show()
            }
        }
        mSoundSwitch.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                Toast.makeText(applicationContext, "Sound Enabled", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(applicationContext, "Sound Disabled", Toast.LENGTH_SHORT).show()
            }
        }
        mVibrationSwitch.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                Toast.makeText(applicationContext, "Vibration Enabled", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(applicationContext, "Vibration Disabled", Toast.LENGTH_SHORT)
                    .show()
            }
        }
        mLeaderboardSwitch.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                Toast.makeText(applicationContext, "Leaderboard Enabled", Toast.LENGTH_SHORT)
                    .show()
            } else {
                Toast.makeText(applicationContext, "Leaderboard Disabled", Toast.LENGTH_SHORT)
                    .show()
            }
        }
    }

    override fun onPause() {
        super.onPause()
        saveSettings()
    }

    private fun saveSettings() {
        val sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit()
        sharedPreferences.putBoolean(KEY_MANUAL, mControlSwitch.isChecked)
        sharedPreferences.putBoolean(KEY_VIBRATION, mVibrationSwitch.isChecked)
        sharedPreferences.putBoolean(KEY_SOUND, mSoundSwitch.isChecked)
        sharedPreferences.putBoolean(KEY_LEADERBOARD, mLeaderboardSwitch.isChecked)
        sharedPreferences.putString(KEY_NAME, mEditName?.text.toString())
        sharedPreferences.apply()
    }

    private fun loadSettings() {
        val sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
        mEditName?.setText(sharedPreferences.getString(KEY_NAME, "Anonymous"))
        val sound = sharedPreferences.getBoolean(KEY_SOUND, true)
        val vibration = sharedPreferences.getBoolean(KEY_VIBRATION, true)
        val leaderboard = sharedPreferences.getBoolean(KEY_LEADERBOARD, true)
        val manual = sharedPreferences.getBoolean(KEY_MANUAL, true)

        mControlSwitch.isChecked = manual
        mSoundSwitch.isChecked = sound
        mVibrationSwitch.isChecked = vibration
        mLeaderboardSwitch.isChecked = leaderboard
    }
}