package com.foxx.androidcast;

/*********************************************************************
 * CastLocaleHelper.java
 * Created at: Sun 17 May 2026 22:49:49 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class CastLocaleHelper
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 86 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	6945cf0b617565a368eaf89b22484f82f07cdd9422fbccc6dabbe169f3b1bd6e
**********************************************************************/
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.os.LocaleListCompat;

import java.util.Locale;

/** Applies user-selected app language (per-app locales). */
public final class CastLocaleHelper {
    private CastLocaleHelper() {}

    public enum AppLocaleMode {
        /** Follow system locale. */
        SYSTEM,
        /** Force English resources. */
        ENGLISH,
        /** Force Russian resources. */
        RUSSIAN
    }

    public static void applyStoredLocale(Context context) {
        AppCompatDelegate.setApplicationLocales(toLocaleList(AppPreferences.getLocaleMode(context)));
    }

    /** Wraps base context so resources resolve with the saved locale. */
    public static Context attach(Context context) {
        AppLocaleMode mode = AppPreferences.getLocaleMode(context);
        if (mode == AppLocaleMode.SYSTEM) {
            return context;
        }
        Locale locale = toLocale(mode);
        if (locale == null) {
            return context;
        }
        return localizedContext(context, locale);
    }

    public static LocaleListCompat toLocaleList(AppLocaleMode mode) {
        if (mode == null) {
            mode = AppLocaleMode.SYSTEM;
        }
        switch (mode) {
            case ENGLISH:
                return LocaleListCompat.forLanguageTags("en");
            case RUSSIAN:
                return LocaleListCompat.forLanguageTags("ru");
            case SYSTEM:
            default:
                return LocaleListCompat.getEmptyLocaleList();
        }
    }

    private static Locale toLocale(AppLocaleMode mode) {
        switch (mode) {
            case ENGLISH:
                return Locale.ENGLISH;
            case RUSSIAN:
                return new Locale("ru");
            case SYSTEM:
            default:
                return null;
        }
    }

    private static Context localizedContext(Context context, Locale locale) {
        Locale.setDefault(locale);
        Configuration config = new Configuration(context.getResources().getConfiguration());
        config.setLocale(locale);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return context.createConfigurationContext(config);
        }
        config.setLocales(new android.os.LocaleList(locale));
        return context.createConfigurationContext(config);
    }
}
