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

import net.minecraft.client.resources.language.I18n;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.client.gui.Font;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.world.level.BaseCommandBlock;
import net.minecraft.client.gui.chat.NarratorChatListener;
import net.minecraft.client.gui.components.CommandSuggestions;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.Screen;

public abstract class AbstractCommandBlockEditScreen extends Screen {
    protected EditBox commandEdit;
    protected EditBox previousEdit;
    protected Button doneButton;
    protected Button cancelButton;
    protected Button outputButton;
    protected boolean trackOutput;
    private CommandSuggestions commandSuggestions;
    
    public AbstractCommandBlockEditScreen() {
        super(NarratorChatListener.NO_TITLE);
    }
    
    @Override
    public void tick() {
        this.commandEdit.tick();
    }
    
    abstract BaseCommandBlock getCommandBlock();
    
    abstract int getPreviousY();
    
    @Override
    protected void init() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        this.doneButton = this.<Button>addButton(new Button(this.width / 2 - 4 - 150, this.height / 4 + 120 + 12, 150, 20, CommonComponents.GUI_DONE, dni -> this.onDone()));
        this.cancelButton = this.<Button>addButton(new Button(this.width / 2 + 4, this.height / 4 + 120 + 12, 150, 20, CommonComponents.GUI_CANCEL, dni -> this.onClose()));
        final BaseCommandBlock bpc3;
        this.outputButton = this.<Button>addButton(new Button(this.width / 2 + 150 - 20, this.getPreviousY(), 20, 20, new TextComponent("O"), dni -> {
            bpc3 = this.getCommandBlock();
            bpc3.setTrackOutput(!bpc3.isTrackOutput());
            this.updateCommandOutput();
            return;
        }));
        (this.commandEdit = new EditBox(this.font, this.width / 2 - 150, 50, 300, 20, new TranslatableComponent("advMode.command")) {
            @Override
            protected MutableComponent createNarrationMessage() {
                return super.createNarrationMessage().append(AbstractCommandBlockEditScreen.this.commandSuggestions.getNarrationMessage());
            }
        }).setMaxLength(32500);
        this.commandEdit.setResponder(this::onEdited);
        this.children.add(this.commandEdit);
        (this.previousEdit = new EditBox(this.font, this.width / 2 - 150, this.getPreviousY(), 276, 20, new TranslatableComponent("advMode.previousOutput"))).setMaxLength(32500);
        this.previousEdit.setEditable(false);
        this.previousEdit.setValue("-");
        this.children.add(this.previousEdit);
        this.setInitialFocus(this.commandEdit);
        this.commandEdit.setFocus(true);
        (this.commandSuggestions = new CommandSuggestions(this.minecraft, this, this.commandEdit, this.font, true, true, 0, 7, false, Integer.MIN_VALUE)).setAllowSuggestions(true);
        this.commandSuggestions.updateCommandInfo();
    }
    
    @Override
    public void resize(final Minecraft dlx, final int integer2, final int integer3) {
        final String string5 = this.commandEdit.getValue();
        this.init(dlx, integer2, integer3);
        this.commandEdit.setValue(string5);
        this.commandSuggestions.updateCommandInfo();
    }
    
    protected void updateCommandOutput() {
        if (this.getCommandBlock().isTrackOutput()) {
            this.outputButton.setMessage(new TextComponent("O"));
            this.previousEdit.setValue(this.getCommandBlock().getLastOutput().getString());
        }
        else {
            this.outputButton.setMessage(new TextComponent("X"));
            this.previousEdit.setValue("-");
        }
    }
    
    protected void onDone() {
        final BaseCommandBlock bpc2 = this.getCommandBlock();
        this.populateAndSendPacket(bpc2);
        if (!bpc2.isTrackOutput()) {
            bpc2.setLastOutput(null);
        }
        this.minecraft.setScreen(null);
    }
    
    @Override
    public void removed() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(false);
    }
    
    protected abstract void populateAndSendPacket(final BaseCommandBlock bpc);
    
    @Override
    public void onClose() {
        this.getCommandBlock().setTrackOutput(this.trackOutput);
        this.minecraft.setScreen(null);
    }
    
    private void onEdited(final String string) {
        this.commandSuggestions.updateCommandInfo();
    }
    
    @Override
    public boolean keyPressed(final int integer1, final int integer2, final int integer3) {
        if (this.commandSuggestions.keyPressed(integer1, integer2, integer3)) {
            return true;
        }
        if (super.keyPressed(integer1, integer2, integer3)) {
            return true;
        }
        if (integer1 == 257 || integer1 == 335) {
            this.onDone();
            return true;
        }
        return false;
    }
    
    @Override
    public boolean mouseScrolled(final double double1, final double double2, final double double3) {
        return this.commandSuggestions.mouseScrolled(double3) || super.mouseScrolled(double1, double2, double3);
    }
    
    @Override
    public boolean mouseClicked(final double double1, final double double2, final int integer) {
        return this.commandSuggestions.mouseClicked(double1, double2, integer) || super.mouseClicked(double1, double2, integer);
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBackground(dhl);
        this.drawCenteredString(dhl, this.font, I18n.get("advMode.setCommand"), this.width / 2, 20, 16777215);
        this.drawString(dhl, this.font, I18n.get("advMode.command"), this.width / 2 - 150, 40, 10526880);
        this.commandEdit.render(dhl, integer2, integer3, float4);
        int integer4 = 75;
        if (!this.previousEdit.getValue().isEmpty()) {
            final int n = integer4;
            final int n2 = 5;
            this.font.getClass();
            integer4 = n + (n2 * 9 + 1 + this.getPreviousY() - 135);
            this.drawString(dhl, this.font, I18n.get("advMode.previousOutput"), this.width / 2 - 150, integer4 + 4, 10526880);
            this.previousEdit.render(dhl, integer2, integer3, float4);
        }
        super.render(dhl, integer2, integer3, float4);
        this.commandSuggestions.render(dhl, integer2, integer3);
    }
}
