package net.minecraft.client.resources.language;

import org.apache.logging.log4j.LogManager;
import com.google.common.collect.Sets;
import java.util.SortedSet;
import java.util.List;
import net.minecraft.locale.Language;
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 com.google.common.collect.Maps;
import net.minecraft.server.packs.PackResources;
import java.util.stream.Stream;
import com.google.common.collect.ImmutableMap;
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;
    private static final LanguageInfo DEFAULT_LANGUAGE;
    private Map<String, LanguageInfo> languages;
    private String currentCode;
    private LanguageInfo currentLanguage;
    
    public LanguageManager(final String string) {
        this.languages = ImmutableMap.<String, LanguageInfo>of("en_us", LanguageManager.DEFAULT_LANGUAGE);
        this.currentLanguage = LanguageManager.DEFAULT_LANGUAGE;
        this.currentCode = string;
    }
    
    private static Map<String, LanguageInfo> extractLanguages(final Stream<PackResources> stream) {
        final Map<String, LanguageInfo> map2 = Maps.newHashMap();
        LanguageMetadataSection ena3;
        final Iterator<LanguageInfo> iterator;
        LanguageInfo emr5;
        final Map<String, LanguageInfo> map3;
        final Exception ex;
        Exception exception3;
        stream.forEach(aae -> {
            try {
                ena3 = aae.<LanguageMetadataSection>getMetadataSection((MetadataSectionSerializer<LanguageMetadataSection>)LanguageMetadataSection.SERIALIZER);
                if (ena3 != null) {
                    ena3.getLanguages().iterator();
                    while (iterator.hasNext()) {
                        emr5 = iterator.next();
                        map3.putIfAbsent(emr5.getCode(), emr5);
                    }
                }
            }
            catch (RuntimeException | IOException ex2) {
                exception3 = ex;
                LanguageManager.LOGGER.warn("Unable to parse language metadata section of resourcepack: {}", aae.getName(), exception3);
            }
            return;
        });
        return ImmutableMap.copyOf(map2);
    }
    
    @Override
    public void onResourceManagerReload(final ResourceManager abc) {
        this.languages = extractLanguages(abc.listPacks());
        final LanguageInfo emr3 = this.languages.getOrDefault("en_us", LanguageManager.DEFAULT_LANGUAGE);
        this.currentLanguage = this.languages.getOrDefault(this.currentCode, emr3);
        final List<LanguageInfo> list4 = Lists.<LanguageInfo>newArrayList(emr3);
        if (this.currentLanguage != emr3) {
            list4.add(this.currentLanguage);
        }
        final ClientLanguage emp5 = ClientLanguage.loadFrom(abc, list4);
        I18n.setLanguage(emp5);
        Language.inject(emp5);
    }
    
    public void setSelected(final LanguageInfo emr) {
        this.currentCode = emr.getCode();
        this.currentLanguage = emr;
    }
    
    public LanguageInfo getSelected() {
        return this.currentLanguage;
    }
    
    public SortedSet<LanguageInfo> getLanguages() {
        return Sets.newTreeSet(this.languages.values());
    }
    
    public LanguageInfo getLanguage(final String string) {
        return this.languages.get(string);
    }
    
    static {
        LOGGER = LogManager.getLogger();
        DEFAULT_LANGUAGE = new LanguageInfo("en_us", "US", "English", false);
    }
}
