package net.minecraft.client.resources.language;

import org.apache.logging.log4j.LogManager;
import com.google.common.collect.Sets;
import java.util.SortedSet;
import com.google.common.collect.Lists;
import net.minecraft.server.packs.resources.ResourceManager;
import java.util.Iterator;
import java.io.IOException;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import net.minecraft.client.resources.metadata.language.LanguageMetadataSection;
import net.minecraft.server.packs.Pack;
import java.util.List;
import com.google.common.collect.Maps;
import java.util.Map;
import org.apache.logging.log4j.Logger;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;

public class LanguageManager implements ResourceManagerReloadListener {
    private static final Logger LOGGER;
    protected static final Locale LOCALE;
    private String currentCode;
    private final Map<String, Language> languages;
    
    public LanguageManager(final String string) {
        this.languages = Maps.newHashMap();
        this.currentCode = string;
        I18n.setLocale(LanguageManager.LOCALE);
    }
    
    public void reload(final List<Pack> list) {
        this.languages.clear();
        for (final Pack ye4 : list) {
            try {
                final LanguageMetadataSection ebo5 = ye4.<LanguageMetadataSection>getMetadataSection((MetadataSectionSerializer<LanguageMetadataSection>)LanguageMetadataSection.SERIALIZER);
                if (ebo5 == null) {
                    continue;
                }
                for (final Language ebe7 : ebo5.getLanguages()) {
                    if (!this.languages.containsKey(ebe7.getCode())) {
                        this.languages.put(ebe7.getCode(), ebe7);
                    }
                }
            }
            catch (RuntimeException | IOException ex2) {
                final Exception ex;
                final Exception exception5 = ex;
                LanguageManager.LOGGER.warn("Unable to parse language metadata section of resourcepack: {}", ye4.getName(), exception5);
            }
        }
    }
    
    @Override
    public void onResourceManagerReload(final ResourceManager zb) {
        final List<String> list3 = Lists.<String>newArrayList("en_us");
        if (!"en_us".equals(this.currentCode)) {
            list3.add(this.currentCode);
        }
        LanguageManager.LOCALE.loadFrom(zb, list3);
        net.minecraft.locale.Language.forceData(LanguageManager.LOCALE.storage);
    }
    
    public boolean isBidirectional() {
        return this.getSelected() != null && this.getSelected().isBidirectional();
    }
    
    public void setSelected(final Language ebe) {
        this.currentCode = ebe.getCode();
    }
    
    public Language getSelected() {
        final String string2 = this.languages.containsKey(this.currentCode) ? this.currentCode : "en_us";
        return this.languages.get(string2);
    }
    
    public SortedSet<Language> getLanguages() {
        return Sets.newTreeSet(this.languages.values());
    }
    
    public Language getLanguage(final String string) {
        return this.languages.get(string);
    }
    
    static {
        LOGGER = LogManager.getLogger();
        LOCALE = new Locale();
    }
}
