package me.teridax.jcash.lang;

import me.teridax.jcash.Logging;

import java.util.Locale;

/**
 * Class for storing static information about the locale used by the application instance at runtime.
 */
@SuppressWarnings("unused")
public class Locales {

    private static Locale defaultLocale = new Locale("en", "EN");

    /**
     * Sets the default locale to use for the application instance.
     * This will instruct the translator to use the default locale as well as
     * Java swing components.
     *
     * @param language the locale to use for language
     * @param country  the locale to use for country
     */
    public static void setDefaultLocale(String language, String country) {
        var locale = language + "_" + country;

        if (Translator.setTranslationLocale(locale)) {
            defaultLocale = new Locale(language, country);
            // apply locale to JVM
            Locale.setDefault(defaultLocale);
        }
        Logging.LOGGER.info("Using locale: " + locale);
    }

    public static Locale getDefaultLocale() {
        return defaultLocale;
    }

    /**
     * Tries to automatically detect the default locale.
     * This will prefer the users locale over the systems locale.
     * If both fail, the JVMs default locale will be used.
     */
    public static void autodetectDefaultLocale() {
        var country = System.getProperty("user.country");
        var language = System.getProperty("user.language");

        var jvmLocale = Locale.getDefault();

        if (null == country)
            country = jvmLocale.getCountry();

        if (null == language)
            language = jvmLocale.getLanguage();

        setDefaultLocale(language, country);
    }
}
