package com.example.defenseapplication.utils

import android.content.Context
import androidx.core.content.edit

enum class AppLanguage(val code: String) {
    ENGLISH("en_US"),
    SIMPLIFIED_CHINESE("zh_CN"),
    TRADITIONAL_CHINESE("zh_TW")
}

object LanguageHeaderManager {
    private const val PREF_NAME = "app_lang_pref"
    private const val KEY_LANGUAGE = "selected_language"

    private var currentLanguage: AppLanguage = AppLanguage.ENGLISH

    fun init(context: Context) {
        val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        val langCode = prefs.getString(KEY_LANGUAGE, AppLanguage.ENGLISH.code)
        currentLanguage = AppLanguage.values().find { it.code == langCode } ?: AppLanguage.ENGLISH
    }

    fun getCurrentLanguage(): AppLanguage = currentLanguage

    fun switchLanguage(context: Context, language: AppLanguage) {
        currentLanguage = language
        // 持久化
        context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit {
            putString(KEY_LANGUAGE, language.code)
            apply()
        }
        // 可选：更新应用 Locale、重启 Activity 等
    }

    fun getLanguageHeaderValue(): String = currentLanguage.code
}