package net.minecraft.client.gui.screens.worldselection;

import net.minecraft.world.level.GameType;
import net.minecraft.world.level.storage.LevelData;
import com.mojang.datafixers.types.DynamicOps;
import com.mojang.datafixers.Dynamic;
import com.mojang.datafixers.types.JsonOps;
import net.minecraft.nbt.NbtOps;
import com.google.gson.JsonElement;
import net.minecraft.world.level.LevelSettings;
import org.apache.commons.lang3.StringUtils;
import java.util.Random;
import net.minecraft.FileUtil;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.screens.CreateBuffetWorldScreen;
import net.minecraft.client.gui.screens.CreateFlatWorldScreen;
import net.minecraft.world.level.LevelType;
import net.minecraft.client.gui.Font;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.client.gui.components.Button;
import javax.annotation.Nullable;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.Screen;

public class CreateWorldScreen extends Screen {
    private final Screen lastScreen;
    private EditBox nameEdit;
    private EditBox seedEdit;
    private String resultFolder;
    private SelectedGameMode gameMode;
    @Nullable
    private SelectedGameMode oldGameMode;
    private boolean features;
    private boolean commands;
    private boolean commandsChanged;
    private boolean bonusItems;
    private boolean hardCore;
    private boolean done;
    private boolean displayOptions;
    private Button createButton;
    private Button modeButton;
    private Button moreOptionsButton;
    private Button featuresButton;
    private Button bonusItemsButton;
    private Button typeButton;
    private Button commandsButton;
    private Button customizeTypeButton;
    private String gameModeHelp1;
    private String gameModeHelp2;
    private String initSeed;
    private String initName;
    private int levelTypeIndex;
    public CompoundTag levelTypeOptions;
    
    public CreateWorldScreen(final Screen dgb) {
        super(new TranslatableComponent("selectWorld.create", new Object[0]));
        this.gameMode = SelectedGameMode.SURVIVAL;
        this.features = true;
        this.levelTypeOptions = new CompoundTag();
        this.lastScreen = dgb;
        this.initSeed = "";
        this.initName = I18n.get("selectWorld.newWorld");
    }
    
    @Override
    public void tick() {
        this.nameEdit.tick();
        this.seedEdit.tick();
    }
    
    @Override
    protected void init() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        (this.nameEdit = new EditBox(this.font, this.width / 2 - 100, 60, 200, 20, I18n.get("selectWorld.enterName")) {
            @Override
            protected String getNarrationMessage() {
                return super.getNarrationMessage() + ". " + I18n.get("selectWorld.resultFolder") + " " + CreateWorldScreen.this.resultFolder;
            }
        }).setValue(this.initName);
        this.nameEdit.setResponder(string -> {
            this.initName = string;
            this.createButton.active = !this.nameEdit.getValue().isEmpty();
            this.updateResultFolder();
            return;
        });
        this.children.add(this.nameEdit);
        this.modeButton = this.addButton(new Button(this.width / 2 - 75, 115, 150, 20, I18n.get("selectWorld.gameMode"), dcv -> {
            switch (this.gameMode) {
                case SURVIVAL: {
                    this.setGameMode(SelectedGameMode.HARDCORE);
                    break;
                }
                case HARDCORE: {
                    this.setGameMode(SelectedGameMode.CREATIVE);
                    break;
                }
                case CREATIVE: {
                    this.setGameMode(SelectedGameMode.SURVIVAL);
                    break;
                }
            }
            dcv.queueNarration(250);
            return;
        }) {
            @Override
            public String getMessage() {
                return I18n.get("selectWorld.gameMode") + ": " + I18n.get("selectWorld.gameMode." + CreateWorldScreen.this.gameMode.name);
            }
            
            @Override
            protected String getNarrationMessage() {
                return super.getNarrationMessage() + ". " + CreateWorldScreen.this.gameModeHelp1 + " " + CreateWorldScreen.this.gameModeHelp2;
            }
        });
        (this.seedEdit = new EditBox(this.font, this.width / 2 - 100, 60, 200, 20, I18n.get("selectWorld.enterSeed"))).setValue(this.initSeed);
        this.seedEdit.setResponder(string -> this.initSeed = this.seedEdit.getValue());
        this.children.add(this.seedEdit);
        this.featuresButton = this.addButton(new Button(this.width / 2 - 155, 100, 150, 20, I18n.get("selectWorld.mapFeatures"), dcv -> {
            this.features = !this.features;
            dcv.queueNarration(250);
            return;
        }) {
            @Override
            public String getMessage() {
                return I18n.get("selectWorld.mapFeatures") + ' ' + I18n.get(CreateWorldScreen.this.features ? "options.on" : "options.off");
            }
            
            @Override
            protected String getNarrationMessage() {
                return super.getNarrationMessage() + ". " + I18n.get("selectWorld.mapFeatures.info");
            }
        });
        this.featuresButton.visible = false;
        this.typeButton = this.addButton(new Button(this.width / 2 + 5, 100, 150, 20, I18n.get("selectWorld.mapType"), dcv -> {
            ++this.levelTypeIndex;
            if (this.levelTypeIndex >= LevelType.LEVEL_TYPES.length) {
                this.levelTypeIndex = 0;
            }
            while (!this.isValidLevelType()) {
                ++this.levelTypeIndex;
                if (this.levelTypeIndex >= LevelType.LEVEL_TYPES.length) {
                    this.levelTypeIndex = 0;
                }
            }
            this.levelTypeOptions = new CompoundTag();
            this.setDisplayOptions(this.displayOptions);
            dcv.queueNarration(250);
            return;
        }) {
            @Override
            public String getMessage() {
                return I18n.get("selectWorld.mapType") + ' ' + I18n.get(LevelType.LEVEL_TYPES[CreateWorldScreen.this.levelTypeIndex].getDescriptionId());
            }
            
            @Override
            protected String getNarrationMessage() {
                final LevelType bkd2 = LevelType.LEVEL_TYPES[CreateWorldScreen.this.levelTypeIndex];
                if (bkd2.hasHelpText()) {
                    return super.getNarrationMessage() + ". " + I18n.get(bkd2.getHelpTextId());
                }
                return super.getNarrationMessage();
            }
        });
        this.typeButton.visible = false;
        this.customizeTypeButton = this.<Button>addButton(new Button(this.width / 2 + 5, 120, 150, 20, I18n.get("selectWorld.customizeType"), dcv -> {
            if (LevelType.LEVEL_TYPES[this.levelTypeIndex] == LevelType.FLAT) {
                this.minecraft.setScreen(new CreateFlatWorldScreen(this, this.levelTypeOptions));
            }
            if (LevelType.LEVEL_TYPES[this.levelTypeIndex] == LevelType.BUFFET) {
                this.minecraft.setScreen(new CreateBuffetWorldScreen(this, this.levelTypeOptions));
            }
            return;
        }));
        this.customizeTypeButton.visible = false;
        this.commandsButton = this.addButton(new Button(this.width / 2 - 155, 151, 150, 20, I18n.get("selectWorld.allowCommands"), dcv -> {
            this.commandsChanged = true;
            this.commands = !this.commands;
            dcv.queueNarration(250);
            return;
        }) {
            @Override
            public String getMessage() {
                return I18n.get("selectWorld.allowCommands") + ' ' + I18n.get((CreateWorldScreen.this.commands && !CreateWorldScreen.this.hardCore) ? "options.on" : "options.off");
            }
            
            @Override
            protected String getNarrationMessage() {
                return super.getNarrationMessage() + ". " + I18n.get("selectWorld.allowCommands.info");
            }
        });
        this.commandsButton.visible = false;
        this.bonusItemsButton = this.addButton(new Button(this.width / 2 + 5, 151, 150, 20, I18n.get("selectWorld.bonusItems"), dcv -> {
            this.bonusItems = !this.bonusItems;
            dcv.queueNarration(250);
            return;
        }) {
            @Override
            public String getMessage() {
                return I18n.get("selectWorld.bonusItems") + ' ' + I18n.get((CreateWorldScreen.this.bonusItems && !CreateWorldScreen.this.hardCore) ? "options.on" : "options.off");
            }
        });
        this.bonusItemsButton.visible = false;
        this.moreOptionsButton = this.<Button>addButton(new Button(this.width / 2 - 75, 187, 150, 20, I18n.get("selectWorld.moreWorldOptions"), dcv -> this.toggleDisplayOptions()));
        this.createButton = this.<Button>addButton(new Button(this.width / 2 - 155, this.height - 28, 150, 20, I18n.get("selectWorld.create"), dcv -> this.onCreate()));
        this.createButton.active = !this.initName.isEmpty();
        this.<Button>addButton(new Button(this.width / 2 + 5, this.height - 28, 150, 20, I18n.get("gui.cancel"), dcv -> this.minecraft.setScreen(this.lastScreen)));
        this.setDisplayOptions(this.displayOptions);
        this.setInitialFocus(this.nameEdit);
        this.setGameMode(this.gameMode);
        this.updateResultFolder();
    }
    
    private void updateGameModeHelp() {
        this.gameModeHelp1 = I18n.get("selectWorld.gameMode." + this.gameMode.name + ".line1");
        this.gameModeHelp2 = I18n.get("selectWorld.gameMode." + this.gameMode.name + ".line2");
    }
    
    private void updateResultFolder() {
        this.resultFolder = this.nameEdit.getValue().trim();
        if (this.resultFolder.isEmpty()) {
            this.resultFolder = "World";
        }
        try {
            this.resultFolder = FileUtil.findAvailableName(this.minecraft.getLevelSource().getBaseDir(), this.resultFolder, "");
        }
        catch (Exception exception4) {
            this.resultFolder = "World";
            try {
                this.resultFolder = FileUtil.findAvailableName(this.minecraft.getLevelSource().getBaseDir(), this.resultFolder, "");
            }
            catch (Exception exception3) {
                throw new RuntimeException("Could not create save folder", exception3);
            }
        }
    }
    
    @Override
    public void removed() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(false);
    }
    
    private void onCreate() {
        this.minecraft.setScreen(null);
        if (this.done) {
            return;
        }
        this.done = true;
        long long2 = new Random().nextLong();
        final String string4 = this.seedEdit.getValue();
        if (!StringUtils.isEmpty((CharSequence)string4)) {
            try {
                final long long3 = Long.parseLong(string4);
                if (long3 != 0L) {
                    long2 = long3;
                }
            }
            catch (NumberFormatException numberFormatException5) {
                long2 = string4.hashCode();
            }
        }
        final LevelSettings bka5 = new LevelSettings(long2, this.gameMode.gameType, this.features, this.hardCore, LevelType.LEVEL_TYPES[this.levelTypeIndex]);
        bka5.setLevelTypeOptions((JsonElement)Dynamic.convert((DynamicOps)NbtOps.INSTANCE, (DynamicOps)JsonOps.INSTANCE, this.levelTypeOptions));
        if (this.bonusItems && !this.hardCore) {
            bka5.enableStartingBonusItems();
        }
        if (this.commands && !this.hardCore) {
            bka5.enableSinglePlayerCommands();
        }
        this.minecraft.selectLevel(this.resultFolder, this.nameEdit.getValue().trim(), bka5);
    }
    
    private boolean isValidLevelType() {
        final LevelType bkd2 = LevelType.LEVEL_TYPES[this.levelTypeIndex];
        return bkd2 != null && bkd2.isSelectable() && (bkd2 != LevelType.DEBUG_ALL_BLOCK_STATES || Screen.hasShiftDown());
    }
    
    private void toggleDisplayOptions() {
        this.setDisplayOptions(!this.displayOptions);
    }
    
    private void setGameMode(final SelectedGameMode a) {
        if (!this.commandsChanged) {
            this.commands = (a == SelectedGameMode.CREATIVE);
        }
        if (a == SelectedGameMode.HARDCORE) {
            this.hardCore = true;
            this.commandsButton.active = false;
            this.bonusItemsButton.active = false;
        }
        else {
            this.hardCore = false;
            this.commandsButton.active = true;
            this.bonusItemsButton.active = true;
        }
        this.gameMode = a;
        this.updateGameModeHelp();
    }
    
    private void setDisplayOptions(final boolean boolean1) {
        this.displayOptions = boolean1;
        this.modeButton.visible = !this.displayOptions;
        this.typeButton.visible = this.displayOptions;
        if (LevelType.LEVEL_TYPES[this.levelTypeIndex] == LevelType.DEBUG_ALL_BLOCK_STATES) {
            this.modeButton.active = false;
            if (this.oldGameMode == null) {
                this.oldGameMode = this.gameMode;
            }
            this.setGameMode(SelectedGameMode.DEBUG);
            this.featuresButton.visible = false;
            this.bonusItemsButton.visible = false;
            this.commandsButton.visible = false;
            this.customizeTypeButton.visible = false;
        }
        else {
            this.modeButton.active = true;
            if (this.oldGameMode != null) {
                this.setGameMode(this.oldGameMode);
            }
            this.featuresButton.visible = (this.displayOptions && LevelType.LEVEL_TYPES[this.levelTypeIndex] != LevelType.CUSTOMIZED);
            this.bonusItemsButton.visible = this.displayOptions;
            this.commandsButton.visible = this.displayOptions;
            this.customizeTypeButton.visible = (this.displayOptions && LevelType.LEVEL_TYPES[this.levelTypeIndex].hasCustomOptions());
        }
        this.seedEdit.setVisible(this.displayOptions);
        this.nameEdit.setVisible(!this.displayOptions);
        if (this.displayOptions) {
            this.moreOptionsButton.setMessage(I18n.get("gui.done"));
        }
        else {
            this.moreOptionsButton.setMessage(I18n.get("selectWorld.moreWorldOptions"));
        }
    }
    
    @Override
    public boolean keyPressed(final int integer1, final int integer2, final int integer3) {
        if (super.keyPressed(integer1, integer2, integer3)) {
            return true;
        }
        if (integer1 == 257 || integer1 == 335) {
            this.onCreate();
            return true;
        }
        return false;
    }
    
    @Override
    public void onClose() {
        if (this.displayOptions) {
            this.setDisplayOptions(false);
        }
        else {
            this.minecraft.setScreen(this.lastScreen);
        }
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        this.renderBackground();
        this.drawCenteredString(this.font, this.title.getColoredString(), this.width / 2, 20, -1);
        if (this.displayOptions) {
            this.drawString(this.font, I18n.get("selectWorld.enterSeed"), this.width / 2 - 100, 47, -6250336);
            this.drawString(this.font, I18n.get("selectWorld.seedInfo"), this.width / 2 - 100, 85, -6250336);
            if (this.featuresButton.visible) {
                this.drawString(this.font, I18n.get("selectWorld.mapFeatures.info"), this.width / 2 - 150, 122, -6250336);
            }
            if (this.commandsButton.visible) {
                this.drawString(this.font, I18n.get("selectWorld.allowCommands.info"), this.width / 2 - 150, 172, -6250336);
            }
            this.seedEdit.render(integer1, integer2, float3);
            if (LevelType.LEVEL_TYPES[this.levelTypeIndex].hasHelpText()) {
                this.font.drawWordWrap(I18n.get(LevelType.LEVEL_TYPES[this.levelTypeIndex].getHelpTextId()), this.typeButton.x + 2, this.typeButton.y + 22, this.typeButton.getWidth(), 10526880);
            }
        }
        else {
            this.drawString(this.font, I18n.get("selectWorld.enterName"), this.width / 2 - 100, 47, -6250336);
            this.drawString(this.font, I18n.get("selectWorld.resultFolder") + " " + this.resultFolder, this.width / 2 - 100, 85, -6250336);
            this.nameEdit.render(integer1, integer2, float3);
            this.drawCenteredString(this.font, this.gameModeHelp1, this.width / 2, 137, -6250336);
            this.drawCenteredString(this.font, this.gameModeHelp2, this.width / 2, 149, -6250336);
        }
        super.render(integer1, integer2, float3);
    }
    
    public void copyFromWorld(final LevelData crm) {
        this.initName = crm.getLevelName();
        this.initSeed = Long.toString(crm.getSeed());
        final LevelType bkd3 = (crm.getGeneratorType() == LevelType.CUSTOMIZED) ? LevelType.NORMAL : crm.getGeneratorType();
        this.levelTypeIndex = bkd3.getId();
        this.levelTypeOptions = crm.getGeneratorOptions();
        this.features = crm.isGenerateMapFeatures();
        this.commands = crm.getAllowCommands();
        if (crm.isHardcore()) {
            this.gameMode = SelectedGameMode.HARDCORE;
        }
        else if (crm.getGameType().isSurvival()) {
            this.gameMode = SelectedGameMode.SURVIVAL;
        }
        else if (crm.getGameType().isCreative()) {
            this.gameMode = SelectedGameMode.CREATIVE;
        }
    }
    
    enum SelectedGameMode {
        SURVIVAL("survival", GameType.SURVIVAL), 
        HARDCORE("hardcore", GameType.SURVIVAL), 
        CREATIVE("creative", GameType.CREATIVE), 
        DEBUG("spectator", GameType.SPECTATOR);
        
        private final String name;
        private final GameType gameType;
        
        private SelectedGameMode(final String string3, final GameType bjt) {
            this.name = string3;
            this.gameType = bjt;
        }
    }
}
