package com.quyunshuo.androidbaseframemvvm.base.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Configuration
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import java.util.Locale

object LanguageUtilsObject {
    private const val PREFS_NAME = "language_prefs"
    private const val KEY_LANGUAGE = "selected_language"
    private const val KEY_LAST_LANGUAGE = "last_language"

    // 设置应用语言
    fun setAppLocale(context: Context, languageTag: String) {
        // 保存当前语言作为最后使用的语言
        getPreferences(context).edit().apply {
            putString(KEY_LAST_LANGUAGE, getCurrentLanguage(context))
            putString(KEY_LANGUAGE, languageTag)
            apply()
        }

        applyLanguage(context, languageTag)
    }

    // 重置为系统语言
    fun resetToSystemLanguage(context: Context) {
        // 保存当前语言作为最后使用的语言
        getPreferences(context).edit().apply {
            putString(KEY_LAST_LANGUAGE, getCurrentLanguage(context))
            remove(KEY_LANGUAGE)
            apply()
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList())
        } else {
            applyLanguage(context, Locale.getDefault().language)
        }
    }

    // 应用语言设置
    private fun applyLanguage(context: Context, languageTag: String) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            // Android 13+ 使用官方方法
            val localeList = LocaleListCompat.forLanguageTags(languageTag)
            AppCompatDelegate.setApplicationLocales(localeList)
        } else {
            // Android 10-12 手动更新配置
            updateResourcesLegacy(context, languageTag)
        }
    }

    // 获取当前语言设置
    fun getCurrentLanguage(context: Context): String {
        return getPreferences(context).getString(KEY_LANGUAGE, "") ?: ""
    }

    // 检查语言是否变更
    fun hasLanguageChanged(context: Context): Boolean {
        val prefs = getPreferences(context)
        val current = prefs.getString(KEY_LANGUAGE, "")
        val last = prefs.getString(KEY_LAST_LANGUAGE, "")
        return current != last
    }

    // 应用启动时应用保存的语言
    fun applyOnCreate(context: Context) {
        val language = getCurrentLanguage(context)
        if (language.isNotEmpty()) {
            applyLanguage(context, language)
        }
    }

    // 更新资源配置 (Android 10兼容)
    private fun updateResourcesLegacy(context: Context, languageTag: String): Context {
        val locale = if (languageTag.isEmpty()) {
            Locale.getDefault()
        } else {
            Locale.forLanguageTag(languageTag)
        }

        Locale.setDefault(locale)

        val resources = context.resources
        val config = Configuration(resources.configuration)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(locale)
            config.setLayoutDirection(locale)
            return context.createConfigurationContext(config)
        } else {
            config.locale = locale
            resources.updateConfiguration(config, resources.displayMetrics)
            return context
        }
    }

    private fun getPreferences(context: Context): SharedPreferences {
        return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
    }

    // 包装上下文以应用语言设置 (重要!)
    fun wrapContext(context: Context, languageTag: String = getCurrentLanguage(context)): Context {
        return if (languageTag.isEmpty()) {
            context
        } else {
            updateResourcesLegacy(context, languageTag)
        }
    }

}