package com.tomhempel.rbarometer.questionnaire

import com.tomhempel.rbarometer.LanguageManager

/**
 * Single source for coach language selection across opening screen and questionnaires.
 */
object AppLanguages {
    const val DEFAULT_ID = "GERMAN"

    val ids: List<String> = listOf(
        "GERMAN", "ENGLISH", "FRENCH", "ROMANIAN", "ARABIC",
        "POLISH", "TURKISH", "UKRAINIAN", "RUSSIAN", "SPANISH",
    )

    /** Short labels shown on in-questionnaire language spinners. */
    val shortLabels: List<String> = listOf(
        "DE", "EN", "FR", "RO", "AR",
        "PL", "TR", "UA", "RU", "ES",
    )

    /** Human-readable names for language spinners (opening screen, upload). */
    fun spinnerLabels(uiLanguageId: String): List<String> =
        ids.map { displayName(uiLanguageId, it) }

    fun displayName(uiLanguageId: String, languageId: String): String {
        val apiKey = "language_${languageId.lowercase()}"
        val fromApi = LanguageManager.getText(uiLanguageId, apiKey)
        if (fromApi != apiKey) return fromApi
        return nativeNamesFor(uiLanguageId)[languageId]
            ?: nativeNamesFor("ENGLISH")[languageId]
            ?: shortLabels.getOrElse(indexOf(languageId)) { languageId }
    }

    fun indexOf(languageId: String): Int =
        ids.indexOf(languageId).coerceAtLeast(0)

    private fun nativeNamesFor(uiLanguageId: String): Map<String, String> = when (uiLanguageId) {
        "GERMAN" -> NAMES_DE
        "ENGLISH" -> NAMES_EN
        else -> NAMES_EN
    }

    private val NAMES_DE = mapOf(
        "GERMAN" to "Deutsch",
        "ENGLISH" to "Englisch",
        "FRENCH" to "Französisch",
        "ROMANIAN" to "Rumänisch",
        "ARABIC" to "Arabisch",
        "POLISH" to "Polnisch",
        "TURKISH" to "Türkisch",
        "UKRAINIAN" to "Ukrainisch",
        "RUSSIAN" to "Russisch",
        "SPANISH" to "Spanisch",
    )

    private val NAMES_EN = mapOf(
        "GERMAN" to "German",
        "ENGLISH" to "English",
        "FRENCH" to "French",
        "ROMANIAN" to "Romanian",
        "ARABIC" to "Arabic",
        "POLISH" to "Polish",
        "TURKISH" to "Turkish",
        "UKRAINIAN" to "Ukrainian",
        "RUSSIAN" to "Russian",
        "SPANISH" to "Spanish",
    )
}
