package net.minecraft.client.gui.screens;

import java.util.AbstractList;
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.world.level.biome.Biome;
import net.minecraft.client.gui.components.ObjectSelectionList;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import net.minecraft.world.level.chunk.ChunkGeneratorType;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.StringTag;
import net.minecraft.world.level.biome.BiomeSourceType;
import net.minecraft.core.Registry;
import net.minecraft.nbt.ListTag;
import java.util.Objects;
import net.minecraft.Util;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.gui.components.Button;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen;
import net.minecraft.resources.ResourceLocation;
import java.util.List;

public class CreateBuffetWorldScreen extends Screen {
    private static final List<ResourceLocation> GENERATORS;
    private final CreateWorldScreen parent;
    private final CompoundTag optionsTag;
    private BiomeList list;
    private int generatorIndex;
    private Button doneButton;
    
    public CreateBuffetWorldScreen(final CreateWorldScreen djh, final CompoundTag jt) {
        super(new TranslatableComponent("createWorld.customize.buffet.title", new Object[0]));
        this.parent = djh;
        this.optionsTag = jt;
    }
    
    @Override
    protected void init() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        this.<Button>addButton(new Button((this.width - 200) / 2, 40, 200, 20, I18n.get("createWorld.customize.buffet.generatortype") + " " + I18n.get(Util.makeDescriptionId("generator", CreateBuffetWorldScreen.GENERATORS.get(this.generatorIndex))), dcv -> {
            ++this.generatorIndex;
            if (this.generatorIndex >= CreateBuffetWorldScreen.GENERATORS.size()) {
                this.generatorIndex = 0;
            }
            dcv.setMessage(I18n.get("createWorld.customize.buffet.generatortype") + " " + I18n.get(Util.makeDescriptionId("generator", CreateBuffetWorldScreen.GENERATORS.get(this.generatorIndex))));
            return;
        }));
        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, I18n.get("gui.done"), dcv -> {
            this.parent.levelTypeOptions = this.saveOptions();
            this.minecraft.setScreen(this.parent);
            return;
        }));
        this.<Button>addButton(new Button(this.width / 2 + 5, this.height - 28, 150, 20, I18n.get("gui.cancel"), dcv -> this.minecraft.setScreen(this.parent)));
        this.loadOptions();
        this.updateButtonValidity();
    }
    
    private void loadOptions() {
        if (this.optionsTag.contains("chunk_generator", 10) && this.optionsTag.getCompound("chunk_generator").contains("type", 8)) {
            final ResourceLocation sm2 = new ResourceLocation(this.optionsTag.getCompound("chunk_generator").getString("type"));
            for (int integer3 = 0; integer3 < CreateBuffetWorldScreen.GENERATORS.size(); ++integer3) {
                if (CreateBuffetWorldScreen.GENERATORS.get(integer3).equals(sm2)) {
                    this.generatorIndex = integer3;
                    break;
                }
            }
        }
        if (this.optionsTag.contains("biome_source", 10) && this.optionsTag.getCompound("biome_source").contains("biomes", 9)) {
            final ListTag jz2 = this.optionsTag.getCompound("biome_source").getList("biomes", 8);
            for (int integer3 = 0; integer3 < jz2.size(); ++integer3) {
                final ResourceLocation sm3 = new ResourceLocation(jz2.getString(integer3));
                this.list.setSelected(this.list.children().stream().filter(a -> Objects.equals(a.key, sm3)).findFirst().orElse(null));
            }
        }
        this.optionsTag.remove("chunk_generator");
        this.optionsTag.remove("biome_source");
    }
    
    private CompoundTag saveOptions() {
        final CompoundTag jt2 = new CompoundTag();
        final CompoundTag jt3 = new CompoundTag();
        jt3.putString("type", Registry.BIOME_SOURCE_TYPE.getKey(BiomeSourceType.FIXED).toString());
        final CompoundTag jt4 = new CompoundTag();
        final ListTag jz5 = new ListTag();
        ((AbstractList<StringTag>)jz5).add(StringTag.valueOf(this.list.getSelected().key.toString()));
        jt4.put("biomes", jz5);
        jt3.put("options", jt4);
        final CompoundTag jt5 = new CompoundTag();
        final CompoundTag jt6 = new CompoundTag();
        jt5.putString("type", CreateBuffetWorldScreen.GENERATORS.get(this.generatorIndex).toString());
        jt6.putString("default_block", "minecraft:stone");
        jt6.putString("default_fluid", "minecraft:water");
        jt5.put("options", jt6);
        jt2.put("biome_source", jt3);
        jt2.put("chunk_generator", jt5);
        return jt2;
    }
    
    public void updateButtonValidity() {
        this.doneButton.active = (this.list.getSelected() != null);
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        this.renderDirtBackground(0);
        this.list.render(integer1, integer2, float3);
        this.drawCenteredString(this.font, this.title.getColoredString(), this.width / 2, 8, 16777215);
        this.drawCenteredString(this.font, I18n.get("createWorld.customize.buffet.generator"), this.width / 2, 30, 10526880);
        this.drawCenteredString(this.font, I18n.get("createWorld.customize.buffet.biome"), this.width / 2, 68, 10526880);
        super.render(integer1, integer2, float3);
    }
    
    static {
        GENERATORS = Registry.CHUNK_GENERATOR_TYPE.keySet().stream().filter(sm -> Registry.CHUNK_GENERATOR_TYPE.get(sm).isPublic()).collect(Collectors.toList());
    }
    
    class BiomeList extends ObjectSelectionList<Entry> {
        private BiomeList() {
            super(CreateBuffetWorldScreen.this.minecraft, CreateBuffetWorldScreen.this.width, CreateBuffetWorldScreen.this.height, 80, CreateBuffetWorldScreen.this.height - 37, 16);
            Registry.BIOME.keySet().stream().sorted(Comparator.comparing(sm -> Registry.BIOME.get(sm).getName().getString())).forEach(sm -> this.addEntry(new Entry(sm)));
        }
        
        @Override
        protected boolean isFocused() {
            return CreateBuffetWorldScreen.this.getFocused() == this;
        }
        
        @Override
        public void setSelected(@Nullable final Entry a) {
            super.setSelected(a);
            if (a != null) {
                NarratorChatListener.INSTANCE.sayNow(new TranslatableComponent("narrator.select", new Object[] { Registry.BIOME.get(a.key).getName().getString() }).getString());
            }
        }
        
        @Override
        protected void moveSelection(final int integer) {
            super.moveSelection(integer);
            CreateBuffetWorldScreen.this.updateButtonValidity();
        }
        
        class Entry extends ObjectSelectionList.Entry<Entry> {
            private final ResourceLocation key;
            
            public Entry(final ResourceLocation sm) {
                this.key = sm;
            }
            
            @Override
            public void render(final int integer1, final int integer2, final int integer3, final int integer4, final int integer5, final int integer6, final int integer7, final boolean boolean8, final float float9) {
                BiomeList.this.drawString(CreateBuffetWorldScreen.this.font, Registry.BIOME.get(this.key).getName().getString(), integer3 + 5, integer2 + 2, 16777215);
            }
            
            @Override
            public boolean mouseClicked(final double double1, final double double2, final int integer) {
                if (integer == 0) {
                    BiomeList.this.setSelected(this);
                    CreateBuffetWorldScreen.this.updateButtonValidity();
                    return true;
                }
                return false;
            }
        }
    }
}
