package net.minecraft.client.gui.screens;

import net.minecraft.client.gui.GuiComponent;
import net.minecraft.util.Mth;
import net.minecraft.network.chat.Component;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.Font;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.client.gui.chat.NarratorChatListener;
import net.minecraft.client.gui.components.CommandSuggestions;
import net.minecraft.client.gui.components.EditBox;

public class ChatScreen extends Screen {
    private String historyBuffer;
    private int historyPos;
    protected EditBox input;
    private String initial;
    private CommandSuggestions commandSuggestions;
    
    public ChatScreen(final String string) {
        super(NarratorChatListener.NO_TITLE);
        this.historyBuffer = "";
        this.historyPos = -1;
        this.initial = "";
        this.initial = string;
    }
    
    @Override
    protected void init() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        this.historyPos = this.minecraft.gui.getChat().getRecentChat().size();
        (this.input = new EditBox(this.font, 4, this.height - 12, this.width - 4, 12, I18n.get("chat.editBox")) {
            @Override
            protected String getNarrationMessage() {
                return super.getNarrationMessage() + ChatScreen.this.commandSuggestions.getNarrationMessage();
            }
        }).setMaxLength(256);
        this.input.setBordered(false);
        this.input.setValue(this.initial);
        this.input.setResponder(this::onEdited);
        this.children.add(this.input);
        (this.commandSuggestions = new CommandSuggestions(this.minecraft, this, this.input, this.font, false, false, 1, 10, true, -805306368)).updateCommandInfo();
        this.setInitialFocus(this.input);
    }
    
    @Override
    public void resize(final Minecraft dbn, final int integer2, final int integer3) {
        final String string5 = this.input.getValue();
        this.init(dbn, integer2, integer3);
        this.setChatLine(string5);
        this.commandSuggestions.updateCommandInfo();
    }
    
    @Override
    public void removed() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(false);
        this.minecraft.gui.getChat().resetChatScroll();
    }
    
    @Override
    public void tick() {
        this.input.tick();
    }
    
    private void onEdited(final String string) {
        final String string2 = this.input.getValue();
        this.commandSuggestions.setAllowSuggestions(!string2.equals(this.initial));
        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 == 256) {
            this.minecraft.setScreen(null);
            return true;
        }
        if (integer1 == 257 || integer1 == 335) {
            final String string5 = this.input.getValue().trim();
            if (!string5.isEmpty()) {
                this.sendMessage(string5);
            }
            this.minecraft.setScreen(null);
            return true;
        }
        if (integer1 == 265) {
            this.moveInHistory(-1);
            return true;
        }
        if (integer1 == 264) {
            this.moveInHistory(1);
            return true;
        }
        if (integer1 == 266) {
            this.minecraft.gui.getChat().scrollChat(this.minecraft.gui.getChat().getLinesPerPage() - 1);
            return true;
        }
        if (integer1 == 267) {
            this.minecraft.gui.getChat().scrollChat(-this.minecraft.gui.getChat().getLinesPerPage() + 1);
            return true;
        }
        return false;
    }
    
    @Override
    public boolean mouseScrolled(final double double1, final double double2, double double3) {
        if (double3 > 1.0) {
            double3 = 1.0;
        }
        if (double3 < -1.0) {
            double3 = -1.0;
        }
        if (this.commandSuggestions.mouseScrolled(double3)) {
            return true;
        }
        if (!Screen.hasShiftDown()) {
            double3 *= 7.0;
        }
        this.minecraft.gui.getChat().scrollChat(double3);
        return true;
    }
    
    @Override
    public boolean mouseClicked(final double double1, final double double2, final int integer) {
        if (this.commandSuggestions.mouseClicked((int)double1, (int)double2, integer)) {
            return true;
        }
        if (integer == 0) {
            final Component lf7 = this.minecraft.gui.getChat().getClickedComponentAt(double1, double2);
            if (lf7 != null && this.handleComponentClicked(lf7)) {
                return true;
            }
        }
        return this.input.mouseClicked(double1, double2, integer) || super.mouseClicked(double1, double2, integer);
    }
    
    @Override
    protected void insertText(final String string, final boolean boolean2) {
        if (boolean2) {
            this.input.setValue(string);
        }
        else {
            this.input.insertText(string);
        }
    }
    
    public void moveInHistory(final int integer) {
        int integer2 = this.historyPos + integer;
        final int integer3 = this.minecraft.gui.getChat().getRecentChat().size();
        integer2 = Mth.clamp(integer2, 0, integer3);
        if (integer2 == this.historyPos) {
            return;
        }
        if (integer2 == integer3) {
            this.historyPos = integer3;
            this.input.setValue(this.historyBuffer);
            return;
        }
        if (this.historyPos == integer3) {
            this.historyBuffer = this.input.getValue();
        }
        this.input.setValue(this.minecraft.gui.getChat().getRecentChat().get(integer2));
        this.commandSuggestions.setAllowSuggestions(false);
        this.historyPos = integer2;
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        this.setFocused(this.input);
        this.input.setFocus(true);
        GuiComponent.fill(2, this.height - 14, this.width - 2, this.height - 2, this.minecraft.options.getBackgroundColor(Integer.MIN_VALUE));
        this.input.render(integer1, integer2, float3);
        this.commandSuggestions.render(integer1, integer2);
        final Component lf5 = this.minecraft.gui.getChat().getClickedComponentAt(integer1, integer2);
        if (lf5 != null && lf5.getStyle().getHoverEvent() != null) {
            this.renderComponentHoverEffect(lf5, integer1, integer2);
        }
        super.render(integer1, integer2, float3);
    }
    
    @Override
    public boolean isPauseScreen() {
        return false;
    }
    
    private void setChatLine(final String string) {
        this.input.setValue(string);
    }
}
