package net.minecraft.client.gui.screens;

import net.minecraft.client.gui.components.AbstractSelectionList;
import net.minecraft.client.gui.chat.NarratorChatListener;
import javax.annotation.Nullable;
import java.util.Iterator;
import net.minecraft.client.resources.language.LanguageInfo;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.FormattedText;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.client.Option;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.Options;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.OptionButton;
import net.minecraft.client.resources.language.LanguageManager;

public class LanguageSelectScreen extends OptionsSubScreen {
    private LanguageSelectionList packSelectionList;
    private final LanguageManager languageManager;
    private OptionButton forceUnicodeButton;
    private Button doneButton;
    
    public LanguageSelectScreen(final Screen dqs, final Options dmb, final LanguageManager ems) {
        super(dqs, dmb, new TranslatableComponent("options.language"));
        this.languageManager = ems;
    }
    
    @Override
    protected void init() {
        this.packSelectionList = new LanguageSelectionList(this.minecraft);
        this.children.add(this.packSelectionList);
        this.forceUnicodeButton = this.<OptionButton>addButton(new OptionButton(this.width / 2 - 155, this.height - 38, 150, 20, Option.FORCE_UNICODE_FONT, Option.FORCE_UNICODE_FONT.getMessage(this.options), dni -> {
            Option.FORCE_UNICODE_FONT.toggle(this.options);
            this.options.save();
            dni.setMessage(Option.FORCE_UNICODE_FONT.getMessage(this.options));
            this.minecraft.resizeDisplay();
            return;
        }));
        final LanguageSelectionList.Entry a3;
        this.doneButton = this.<Button>addButton(new Button(this.width / 2 - 155 + 160, this.height - 38, 150, 20, CommonComponents.GUI_DONE, dni -> {
            a3 = this.packSelectionList.getSelected();
            if (a3 != null && !a3.language.getCode().equals(this.languageManager.getSelected().getCode())) {
                this.languageManager.setSelected(a3.language);
                this.options.languageCode = a3.language.getCode();
                this.minecraft.reloadResourcePacks();
                this.doneButton.setMessage(CommonComponents.GUI_DONE);
                this.forceUnicodeButton.setMessage(Option.FORCE_UNICODE_FONT.getMessage(this.options));
                this.options.save();
            }
            this.minecraft.setScreen(this.lastScreen);
            return;
        }));
        super.init();
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.packSelectionList.render(dhl, integer2, integer3, float4);
        this.drawCenteredString(dhl, this.font, this.title, this.width / 2, 16, 16777215);
        this.drawCenteredString(dhl, this.font, "(" + I18n.get("options.languageWarning") + ")", this.width / 2, this.height - 56, 8421504);
        super.render(dhl, integer2, integer3, float4);
    }
    
    class LanguageSelectionList extends ObjectSelectionList<Entry> {
        public LanguageSelectionList(final Minecraft dlx) {
            super(dlx, LanguageSelectScreen.this.width, LanguageSelectScreen.this.height, 32, LanguageSelectScreen.this.height - 65 + 4, 18);
            for (final LanguageInfo emr5 : LanguageSelectScreen.this.languageManager.getLanguages()) {
                final Entry a6 = new Entry(emr5);
                this.addEntry(a6);
                if (LanguageSelectScreen.this.languageManager.getSelected().getCode().equals(emr5.getCode())) {
                    this.setSelected(a6);
                }
            }
            if (this.getSelected() != null) {
                this.centerScrollOn(this.getSelected());
            }
        }
        
        @Override
        protected int getScrollbarPosition() {
            return super.getScrollbarPosition() + 20;
        }
        
        @Override
        public int getRowWidth() {
            return super.getRowWidth() + 50;
        }
        
        @Override
        public void setSelected(@Nullable final Entry a) {
            super.setSelected(a);
            if (a != null) {
                NarratorChatListener.INSTANCE.sayNow(new TranslatableComponent("narrator.select", new Object[] { a.language }).getString());
            }
        }
        
        @Override
        protected void renderBackground(final PoseStack dhl) {
            LanguageSelectScreen.this.renderBackground(dhl);
        }
        
        @Override
        protected boolean isFocused() {
            return LanguageSelectScreen.this.getFocused() == this;
        }
        
        public class Entry extends ObjectSelectionList.Entry<Entry> {
            private final LanguageInfo language;
            
            public Entry(final LanguageInfo emr) {
                this.language = emr;
            }
            
            @Override
            public void render(final PoseStack dhl, final int integer2, final int integer3, final int integer4, final int integer5, final int integer6, final int integer7, final int integer8, final boolean boolean9, final float float10) {
                final String string12 = this.language.toString();
                LanguageSelectScreen.this.font.drawShadow(dhl, string12, (float)(LanguageSelectionList.this.width / 2 - LanguageSelectScreen.this.font.width(string12) / 2), (float)(integer3 + 1), 16777215, true);
            }
            
            @Override
            public boolean mouseClicked(final double double1, final double double2, final int integer) {
                if (integer == 0) {
                    this.select();
                    return true;
                }
                return false;
            }
            
            private void select() {
                LanguageSelectionList.this.setSelected(this);
            }
        }
    }
}
