package net.minecraft.client.gui.screens;

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.world.level.GameType;
import net.minecraft.util.HttpUtil;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.gui.components.Button;

public class ShareToLanScreen extends Screen {
    private final Screen lastScreen;
    private Button commandsButton;
    private Button modeButton;
    private String gameModeName;
    private boolean commands;
    
    public ShareToLanScreen(final Screen dqs) {
        super(new TranslatableComponent("lanServer.title"));
        this.gameModeName = "survival";
        this.lastScreen = dqs;
    }
    
    @Override
    protected void init() {
        final int integer3;
        final TranslatableComponent translatableComponent;
        Component mr4;
        this.<Button>addButton(new Button(this.width / 2 - 155, this.height - 28, 150, 20, new TranslatableComponent("lanServer.start"), dni -> {
            this.minecraft.setScreen(null);
            integer3 = HttpUtil.getAvailablePort();
            if (this.minecraft.getSingleplayerServer().publishServer(GameType.byName(this.gameModeName), this.commands, integer3)) {
                new TranslatableComponent("commands.publish.started", new Object[] { integer3 });
                mr4 = translatableComponent;
            }
            else {
                mr4 = new TranslatableComponent("commands.publish.failed");
            }
            this.minecraft.gui.getChat().addMessage(mr4);
            this.minecraft.updateTitle();
            return;
        }));
        this.<Button>addButton(new Button(this.width / 2 + 5, this.height - 28, 150, 20, CommonComponents.GUI_CANCEL, dni -> this.minecraft.setScreen(this.lastScreen)));
        this.modeButton = this.<Button>addButton(new Button(this.width / 2 - 155, 100, 150, 20, new TranslatableComponent("selectWorld.gameMode"), dni -> {
            if ("spectator".equals(this.gameModeName)) {
                this.gameModeName = "creative";
            }
            else if ("creative".equals(this.gameModeName)) {
                this.gameModeName = "adventure";
            }
            else if ("adventure".equals(this.gameModeName)) {
                this.gameModeName = "survival";
            }
            else {
                this.gameModeName = "spectator";
            }
            this.updateSelectionStrings();
            return;
        }));
        this.commandsButton = this.<Button>addButton(new Button(this.width / 2 + 5, 100, 150, 20, new TranslatableComponent("selectWorld.allowCommands"), dni -> {
            this.commands = !this.commands;
            this.updateSelectionStrings();
            return;
        }));
        this.updateSelectionStrings();
    }
    
    private void updateSelectionStrings() {
        this.modeButton.setMessage(new TranslatableComponent("selectWorld.gameMode").append(": ").append(new TranslatableComponent("selectWorld.gameMode." + this.gameModeName)));
        this.commandsButton.setMessage(new TranslatableComponent("selectWorld.allowCommands").append(" ").append(CommonComponents.optionStatus(this.commands)));
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBackground(dhl);
        this.drawCenteredString(dhl, this.font, this.title, this.width / 2, 50, 16777215);
        this.drawCenteredString(dhl, this.font, I18n.get("lanServer.otherPlayers"), this.width / 2, 82, 16777215);
        super.render(dhl, integer2, integer3, float4);
    }
}
