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

import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.math.Matrix4f;
import java.util.List;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.resources.model.Material;
import net.minecraft.world.level.block.state.BlockState;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.Tesselator;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.components.ComponentRenderUtils;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.world.level.block.StandingSignBlock;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.platform.Lighting;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerboundSignUpdatePacket;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.gui.font.TextFieldHelper;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.client.renderer.blockentity.SignRenderer;
import net.minecraft.client.gui.screens.Screen;

public class SignEditScreen extends Screen {
    private final SignRenderer.SignModel signModel;
    private final SignBlockEntity sign;
    private int frame;
    private int line;
    private TextFieldHelper signField;
    
    public SignEditScreen(final SignBlockEntity bxh) {
        super(new TranslatableComponent("sign.edit", new Object[0]));
        this.signModel = new SignRenderer.SignModel();
        this.sign = bxh;
    }
    
    @Override
    protected void init() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        this.<Button>addButton(new Button(this.width / 2 - 100, this.height / 4 + 120, 200, 20, I18n.get("gui.done"), dcv -> this.onDone()));
        this.sign.setEditable(false);
        this.signField = new TextFieldHelper(this.minecraft, () -> this.sign.getMessage(this.line).getString(), string -> this.sign.setMessage(this.line, new TextComponent(string)), 90);
    }
    
    @Override
    public void removed() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(false);
        final ClientPacketListener dnp2 = this.minecraft.getConnection();
        if (dnp2 != null) {
            dnp2.send(new ServerboundSignUpdatePacket(this.sign.getBlockPos(), this.sign.getMessage(0), this.sign.getMessage(1), this.sign.getMessage(2), this.sign.getMessage(3)));
        }
        this.sign.setEditable(true);
    }
    
    @Override
    public void tick() {
        ++this.frame;
        if (!this.sign.getType().isValid(this.sign.getBlockState().getBlock())) {
            this.onDone();
        }
    }
    
    private void onDone() {
        this.sign.setChanged();
        this.minecraft.setScreen(null);
    }
    
    @Override
    public boolean charTyped(final char character, final int integer) {
        this.signField.charTyped(character);
        return true;
    }
    
    @Override
    public void onClose() {
        this.onDone();
    }
    
    @Override
    public boolean keyPressed(final int integer1, final int integer2, final int integer3) {
        if (integer1 == 265) {
            this.line = (this.line - 1 & 0x3);
            this.signField.setEnd();
            return true;
        }
        if (integer1 == 264 || integer1 == 257 || integer1 == 335) {
            this.line = (this.line + 1 & 0x3);
            this.signField.setEnd();
            return true;
        }
        return this.signField.keyPressed(integer1) || super.keyPressed(integer1, integer2, integer3);
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        Lighting.setupForFlatItems();
        this.renderBackground();
        this.drawCenteredString(this.font, this.title.getColoredString(), this.width / 2, 40, 16777215);
        final PoseStack cyl5 = new PoseStack();
        cyl5.pushPose();
        cyl5.translate(this.width / 2, 0.0, 50.0);
        final float float4 = 93.75f;
        cyl5.scale(93.75f, -93.75f, 93.75f);
        cyl5.translate(0.0, -1.3125, 0.0);
        final BlockState byj7 = this.sign.getBlockState();
        final boolean boolean8 = byj7.getBlock() instanceof StandingSignBlock;
        if (!boolean8) {
            cyl5.translate(0.0, -0.3125, 0.0);
        }
        final boolean boolean9 = this.frame / 6 % 2 == 0;
        final float float5 = 0.6666667f;
        cyl5.pushPose();
        cyl5.scale(0.6666667f, -0.6666667f, -0.6666667f);
        final MultiBufferSource.BufferSource a11 = this.minecraft.renderBuffers().bufferSource();
        final Material eby12 = SignRenderer.getMaterial(byj7.getBlock());
        final VertexConsumer cyo13 = eby12.buffer(a11, this.signModel::renderType);
        this.signModel.sign.render(cyl5, cyo13, 15728880, OverlayTexture.NO_OVERLAY);
        if (boolean8) {
            this.signModel.stick.render(cyl5, cyo13, 15728880, OverlayTexture.NO_OVERLAY);
        }
        cyl5.popPose();
        final float float6 = 0.010416667f;
        cyl5.translate(0.0, 0.3333333432674408, 0.046666666865348816);
        cyl5.scale(0.010416667f, -0.010416667f, 0.010416667f);
        final int integer3 = this.sign.getColor().getTextColor();
        final String[] arr16 = new String[4];
        for (int integer4 = 0; integer4 < arr16.length; ++integer4) {
            final List<Component> list3;
            arr16[integer4] = this.sign.getRenderMessage(integer4, lf -> {
                list3 = ComponentRenderUtils.wrapComponents(lf, 90, this.minecraft.font, false, true);
                return list3.isEmpty() ? "" : list3.get(0).getColoredString();
            });
        }
        final Matrix4f b17 = cyl5.last().pose();
        final int integer5 = this.signField.getCursorPos();
        final int integer6 = this.signField.getSelectionPos();
        final int integer7 = this.minecraft.font.isBidirectional() ? -1 : 1;
        final int integer8 = this.line * 10 - this.sign.messages.length * 5;
        for (int integer9 = 0; integer9 < arr16.length; ++integer9) {
            final String string23 = arr16[integer9];
            if (string23 != null) {
                final float float7 = (float)(-this.minecraft.font.width(string23) / 2);
                this.minecraft.font.drawInBatch(string23, float7, (float)(integer9 * 10 - this.sign.messages.length * 5), integer3, false, b17, a11, false, 0, 15728880);
                if (integer9 == this.line && integer5 >= 0) {
                    if (boolean9) {
                        final int integer10 = this.minecraft.font.width(string23.substring(0, Math.max(Math.min(integer5, string23.length()), 0)));
                        final int integer11 = (integer10 - this.minecraft.font.width(string23) / 2) * integer7;
                        if (integer5 >= string23.length()) {
                            this.minecraft.font.drawInBatch("_", (float)integer11, (float)integer8, integer3, false, b17, a11, false, 0, 15728880);
                        }
                    }
                }
            }
        }
        a11.endBatch();
        for (int integer9 = 0; integer9 < arr16.length; ++integer9) {
            final String string23 = arr16[integer9];
            if (string23 != null && integer9 == this.line) {
                if (integer5 >= 0) {
                    final int integer12 = this.minecraft.font.width(string23.substring(0, Math.max(Math.min(integer5, string23.length()), 0)));
                    final int integer10 = (integer12 - this.minecraft.font.width(string23) / 2) * integer7;
                    if (boolean9 && integer5 < string23.length()) {
                        final Matrix4f b18 = b17;
                        final int integer18 = integer10;
                        final int integer19 = integer8 - 1;
                        final int integer20 = integer10 + 1;
                        final int n = integer8;
                        this.minecraft.font.getClass();
                        GuiComponent.fill(b18, integer18, integer19, integer20, n + 9, 0xFF000000 | integer3);
                    }
                    if (integer6 != integer5) {
                        final int integer11 = Math.min(integer5, integer6);
                        final int integer13 = Math.max(integer5, integer6);
                        final int integer14 = (this.minecraft.font.width(string23.substring(0, integer11)) - this.minecraft.font.width(string23) / 2) * integer7;
                        final int integer15 = (this.minecraft.font.width(string23.substring(0, integer13)) - this.minecraft.font.width(string23) / 2) * integer7;
                        final int integer16 = Math.min(integer14, integer15);
                        final int integer17 = Math.max(integer14, integer15);
                        final Tesselator cym32 = Tesselator.getInstance();
                        final BufferBuilder cyg33 = cym32.getBuilder();
                        RenderSystem.disableTexture();
                        RenderSystem.enableColorLogicOp();
                        RenderSystem.logicOp(GlStateManager.LogicOp.OR_REVERSE);
                        cyg33.begin(7, DefaultVertexFormat.POSITION_COLOR);
                        final BufferBuilder bufferBuilder = cyg33;
                        final Matrix4f b19 = b17;
                        final float float8 = (float)integer16;
                        final int n2 = integer8;
                        this.minecraft.font.getClass();
                        bufferBuilder.vertex(b19, float8, (float)(n2 + 9), 0.0f).color(0, 0, 255, 255).endVertex();
                        final BufferBuilder bufferBuilder2 = cyg33;
                        final Matrix4f b20 = b17;
                        final float float9 = (float)integer17;
                        final int n3 = integer8;
                        this.minecraft.font.getClass();
                        bufferBuilder2.vertex(b20, float9, (float)(n3 + 9), 0.0f).color(0, 0, 255, 255).endVertex();
                        cyg33.vertex(b17, (float)integer17, (float)integer8, 0.0f).color(0, 0, 255, 255).endVertex();
                        cyg33.vertex(b17, (float)integer16, (float)integer8, 0.0f).color(0, 0, 255, 255).endVertex();
                        cyg33.end();
                        BufferUploader.end(cyg33);
                        RenderSystem.disableColorLogicOp();
                        RenderSystem.enableTexture();
                    }
                }
            }
        }
        cyl5.popPose();
        Lighting.setupFor3DItems();
        super.render(integer1, integer2, float3);
    }
}
