package com.tomhempel.rbarometer.ui

import android.content.Context
import com.tomhempel.rbarometer.questionnaire.AppLanguages

/**
 * Persists the coach's chosen UI language across app restarts and screens.
 */
object AppLanguageStore {
    private const val PREF = "app_settings"
    private const val KEY_UI_LANGUAGE = "ui_language_id"

    fun get(context: Context): String {
        val raw = context.getSharedPreferences(PREF, Context.MODE_PRIVATE)
            .getString(KEY_UI_LANGUAGE, AppLanguages.DEFAULT_ID)
            ?: AppLanguages.DEFAULT_ID
        return raw.takeIf { AppLanguages.ids.contains(it) } ?: AppLanguages.DEFAULT_ID
    }

    fun set(context: Context, languageId: String) {
        if (!AppLanguages.ids.contains(languageId)) return
        context.getSharedPreferences(PREF, Context.MODE_PRIVATE).edit()
            .putString(KEY_UI_LANGUAGE, languageId)
            .apply()
    }
}
