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.Comparator;
import net.minecraft.core.Registry;
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 java.util.Objects;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.gui.components.Button;
import net.minecraft.world.level.biome.Biome;
import java.util.function.Consumer;

public class CreateBuffetWorldScreen extends Screen {
    private final Screen parent;
    private final Consumer<Biome> applySettings;
    private BiomeList list;
    private Biome biome;
    private Button doneButton;
    
    public CreateBuffetWorldScreen(final Screen dqs, final Consumer<Biome> consumer, final Biome bre) {
        super(new TranslatableComponent("createWorld.customize.buffet.title"));
        this.parent = dqs;
        this.applySettings = consumer;
        this.biome = bre;
    }
    
    @Override
    public void onClose() {
        this.minecraft.setScreen(this.parent);
    }
    
    @Override
    protected void init() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        this.list = new BiomeList();
        this.children.add(this.list);
        this.doneButton = this.<Button>addButton(new Button(this.width / 2 - 155, this.height - 28, 150, 20, CommonComponents.GUI_DONE, dni -> {
            this.applySettings.accept(this.biome);
            this.minecraft.setScreen(this.parent);
            return;
        }));
        this.<Button>addButton(new Button(this.width / 2 + 5, this.height - 28, 150, 20, CommonComponents.GUI_CANCEL, dni -> this.minecraft.setScreen(this.parent)));
        this.list.setSelected(this.list.children().stream().filter(a -> Objects.equals(a.biome, this.biome)).findFirst().orElse(null));
    }
    
    private void updateButtonValidity() {
        this.doneButton.active = (this.list.getSelected() != null);
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderDirtBackground(0);
        this.list.render(dhl, integer2, integer3, float4);
        this.drawCenteredString(dhl, this.font, this.title, this.width / 2, 8, 16777215);
        this.drawCenteredString(dhl, this.font, I18n.get("createWorld.customize.buffet.biome"), this.width / 2, 28, 10526880);
        super.render(dhl, integer2, integer3, float4);
    }
    
    class BiomeList extends ObjectSelectionList<Entry> {
        private BiomeList() {
            super(CreateBuffetWorldScreen.this.minecraft, CreateBuffetWorldScreen.this.width, CreateBuffetWorldScreen.this.height, 40, CreateBuffetWorldScreen.this.height - 37, 16);
            Registry.BIOME.stream().sorted(Comparator.comparing(bre -> bre.getName().getString())).forEach(bre -> this.addEntry(new Entry(bre)));
        }
        
        @Override
        protected boolean isFocused() {
            return CreateBuffetWorldScreen.this.getFocused() == this;
        }
        
        @Override
        public void setSelected(@Nullable final Entry a) {
            super.setSelected(a);
            if (a != null) {
                CreateBuffetWorldScreen.this.biome = a.biome;
                NarratorChatListener.INSTANCE.sayNow(new TranslatableComponent("narrator.select", new Object[] { a.biome.getName().getString() }).getString());
            }
            CreateBuffetWorldScreen.this.updateButtonValidity();
        }
        
        class Entry extends ObjectSelectionList.Entry<Entry> {
            private final Biome biome;
            
            public Entry(final Biome bre) {
                this.biome = bre;
            }
            
            @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) {
                BiomeList.this.drawString(dhl, CreateBuffetWorldScreen.this.font, this.biome.getName().getString(), integer4 + 5, integer3 + 2, 16777215);
            }
            
            @Override
            public boolean mouseClicked(final double double1, final double double2, final int integer) {
                if (integer == 0) {
                    BiomeList.this.setSelected(this);
                    return true;
                }
                return false;
            }
        }
    }
}
