package com.seven.ikar.kotlin.core.settings

import android.content.Context
import android.content.res.Configuration
import android.os.LocaleList
import java.util.Locale

object AppLocaleManager {
    fun apply(context: Context, mode: AppLanguageMode) {
        val locale = localeFor(mode) ?: Locale.getDefault()
        Locale.setDefault(locale)

        val configuration = Configuration(context.resources.configuration)
        if (mode == AppLanguageMode.System) {
            configuration.setLocales(LocaleList.getDefault())
        } else {
            configuration.setLocale(locale)
            configuration.setLocales(LocaleList(locale))
        }
        context.resources.updateConfiguration(configuration, context.resources.displayMetrics)
    }

    private fun localeFor(mode: AppLanguageMode): Locale? = when (mode) {
        AppLanguageMode.System -> null
        AppLanguageMode.Russian -> Locale("ru", "RU")
        AppLanguageMode.English -> Locale.ENGLISH
    }
}
