package net.taehui.twilight.system

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import net.taehui.twilight.Logger

object LanguageSystem : Logger {
    private val languageStore = mutableMapOf<String, MutableMap<String, String>>()

    init {
        LanguageSystem::class.java.classLoader.getResourceAsStream("Language.json")?.use {
            languageStore.putAll(
                ObjectMapper().readValue(
                    it,
                    object : TypeReference<HashMap<String, MutableMap<String, String>>>() {})
            )
            logInfo("Loaded Language")
        }
    }

    fun getLanguage(language: String, target: String): String {
        return languageStore[target]?.get(language) ?: languageStore[target]?.get("en-US") ?: throw RuntimeException(
            target
        )
    }
}