/*
 * Copyright (c) 2026 Meshtastic LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package org.meshtastic.feature.settings.util

import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.core.os.LocaleListCompat
import org.jetbrains.compose.resources.stringResource
import org.meshtastic.core.resources.Res
import org.meshtastic.core.resources.fr_HT
import org.meshtastic.core.resources.preferences_system_default
import org.meshtastic.core.resources.pt_BR
import org.meshtastic.core.resources.zh_CN
import org.meshtastic.core.resources.zh_TW
import java.util.Locale

object LanguageUtils {

    const val SYSTEM_DEFAULT = "zz"

    /**
     * Sets the application locale using AppCompatDelegate. Note: This is the modern standard for per-app language
     * support, providing a backport for API levels below 33.
     */
    fun setAppLocale(languageTag: String) {
        AppCompatDelegate.setApplicationLocales(
            if (languageTag == SYSTEM_DEFAULT) {
                LocaleListCompat.getEmptyLocaleList()
            } else {
                LocaleListCompat.forLanguageTags(languageTag)
            },
        )
    }

    /** Using a hardcoded list, maps language tags to their localized language names (e.g.: "en" -> "English") */
    @Suppress("CyclomaticComplexMethod", "LongMethod")
    @Composable
    fun languageMap(): Map<String, String> {
        val languageTags = remember {
            listOf(
                SYSTEM_DEFAULT,
                "en",
                "ar",
                "bg",
                "ca",
                "cs",
                "de",
                "el",
                "es",
                "et",
                "fi",
                "fr",
                "ga",
                "gl",
                "hr",
                "ht",
                "hu",
                "is",
                "it",
                "iw",
                "ja",
                "ko",
                "lt",
                "nl",
                "nb",
                "pl",
                "pt",
                "pt-BR",
                "ro",
                "ru",
                "sk",
                "sl",
                "sq",
                "sr",
                "srp",
                "sv",
                "tr",
                "uk",
                "zh-CN",
                "zh-TW",
            )
        }

        return languageTags.associateWith { languageTag ->
            when (languageTag) {
                SYSTEM_DEFAULT -> stringResource(Res.string.preferences_system_default)

                "ht" -> stringResource(Res.string.fr_HT)

                "pt-BR" -> stringResource(Res.string.pt_BR)

                "zh-CN" -> stringResource(Res.string.zh_CN)

                "zh-TW" -> stringResource(Res.string.zh_TW)

                else -> {
                    Locale.forLanguageTag(languageTag).let { locale ->
                        locale.getDisplayLanguage(locale).replaceFirstChar { char ->
                            if (char.isLowerCase()) char.titlecase(locale) else char.toString()
                        }
                    }
                }
            }
        }
    }
}
