package com.aiosman.ravenow

import android.content.Context
import android.content.SharedPreferences
import com.google.android.gms.auth.api.signin.GoogleSignInOptions

/**
 * 持久化本地数据
 */
object AppStore {
    private const val STORE_VERSION = 1
    private const val PREFS_NAME = "app_prefs_$STORE_VERSION"
    var token: String? = null
    var rememberMe: Boolean = false
    var isGuest: Boolean = false
    private lateinit var sharedPreferences: SharedPreferences
    lateinit var googleSignInOptions: GoogleSignInOptions
    fun init(context: Context) {
        sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
        this.loadData()

        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("754277015802-uarf8br8k8gkpbj0t9g65bvkvit630q5.apps.googleusercontent.com") // Replace with your server's client ID
            .requestEmail()
            .build()
        googleSignInOptions = gso
        // apply dark mode
        if (sharedPreferences.getBoolean("darkMode", false)) {
            AppState.darkMode = true
            AppState.appTheme = DarkThemeColors()
        }
        
        // load chat background
        val savedBgUrl = sharedPreferences.getString("chatBackgroundUrl", null)
        if (savedBgUrl != null) {
            AppState.chatBackgroundUrl = savedBgUrl
        }

    }

    suspend fun saveData() {
        // shared preferences
        sharedPreferences.edit().apply {
            putString("token", token)
            putBoolean("rememberMe", rememberMe)
            putBoolean("isGuest", isGuest)
        }.apply()
    }

    fun loadData() {
        // shared preferences
        token = sharedPreferences.getString("token", null)
        rememberMe = sharedPreferences.getBoolean("rememberMe", false)
        isGuest = sharedPreferences.getBoolean("isGuest", false)
    }

    fun saveDarkMode(darkMode: Boolean) {
        sharedPreferences.edit().apply {
            putBoolean("darkMode", darkMode)
        }.apply()
    }

    fun saveChatBackgroundUrl(url: String?) {
        sharedPreferences.edit().apply {
            if (url != null) {
                putString("chatBackgroundUrl", url)
            } else {
                remove("chatBackgroundUrl")
            }
        }.apply()
        AppState.chatBackgroundUrl = url
    }

    // ===================== 用户本地扩展信息 =====================
    // 后端暂未提供 MBTI 与星座字段，使用本地持久化按用户维度进行存储
    private fun mbtiKey(userId: Int) = "mbti_user_$userId"
    private fun zodiacKey(userId: Int) = "zodiac_user_$userId"

    fun getUserMbti(userId: Int): String? {
        return sharedPreferences.getString(mbtiKey(userId), null)
    }

    fun setUserMbti(userId: Int, mbti: String?) {
        sharedPreferences.edit().apply {
            if (mbti.isNullOrEmpty()) remove(mbtiKey(userId)) else putString(mbtiKey(userId), mbti)
        }.apply()
    }

    fun getUserZodiac(userId: Int): String? {
        return sharedPreferences.getString(zodiacKey(userId), null)
    }

    fun setUserZodiac(userId: Int, zodiac: String?) {
        sharedPreferences.edit().apply {
            if (zodiac.isNullOrEmpty()) remove(zodiacKey(userId)) else putString(zodiacKey(userId), zodiac)
        }.apply()
    }

}