package mtr.gui;

import mtr.block.BlockTrainAnnouncer;
import mtr.data.IGui;
import mtr.packet.IPacket;
import mtr.packet.PacketTrainDataGuiClient;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class TrainAnnouncerScreen extends Screen implements IGui, IPacket {

	private final BlockPos pos;
	private final String initialMessage;
	private final TextFieldWidget textFieldName;
	private final Text text = new TranslatableText("gui.mtr.announcement_message");

	public TrainAnnouncerScreen(BlockPos pos) {
		super(new LiteralText(""));

		client = MinecraftClient.getInstance();
		this.pos = pos;
		textFieldName = new TextFieldWidget(client.textRenderer, 0, 0, 0, SQUARE_SIZE, new LiteralText(""));

		final World world = client.world;
		if (world != null) {
			final BlockEntity entity = world.getBlockEntity(pos);
			initialMessage = entity instanceof BlockTrainAnnouncer.TileEntityTrainAnnouncer ? ((BlockTrainAnnouncer.TileEntityTrainAnnouncer) entity).getMessage() : "";
		} else {
			initialMessage = "";
		}
	}

	@Override
	protected void init() {
		super.init();
		IDrawing.setPositionAndWidth(textFieldName, SQUARE_SIZE + TEXT_FIELD_PADDING / 2, SQUARE_SIZE * 2 + TEXT_FIELD_PADDING / 2, width - SQUARE_SIZE * 2 - TEXT_FIELD_PADDING);
		textFieldName.setText(initialMessage);
		addChild(textFieldName);
	}

	@Override
	public void tick() {
		textFieldName.tick();
	}

	@Override
	public void onClose() {
		PacketTrainDataGuiClient.sendTrainAnnouncerMessageC2S(pos, IGui.formatStationName(textFieldName.getText()));
		super.onClose();
	}

	@Override
	public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
		try {
			renderBackground(matrices);
			textRenderer.draw(matrices, text, SQUARE_SIZE + TEXT_PADDING, SQUARE_SIZE + TEXT_PADDING, ARGB_WHITE);
			textFieldName.render(matrices, mouseX, mouseY, delta);
			super.render(matrices, mouseX, mouseY, delta);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean isPauseScreen() {
		return false;
	}
}

@@ -763,19 +763,22 @@
 extField
-Nam
+Messag
 e;%0A%09priv
@@ -854,16 +854,69 @@
 age%22);%0A%0A
+%09private static final int MAX_MESSAGE_LENGTH = 256;%0A%0A
 %09public 
@@ -1054,19 +1054,22 @@
 extField
-Nam
+Messag
 e = new 
@@ -1561,19 +1561,22 @@
 extField
-Nam
+Messag
 e, SQUAR
@@ -1708,19 +1708,22 @@
 extField
-Nam
+Messag
 e.setTex
@@ -1741,16 +1741,69 @@
 ssage);%0A
+%09%09textFieldMessage.setMaxLength(MAX_MESSAGE_LENGTH);%0A
 %09%09addChi
@@ -1814,19 +1814,22 @@
 extField
-Nam
+Messag
 e);%0A%09%7D%0A%0A
@@ -1872,19 +1872,22 @@
 extField
-Nam
+Messag
 e.tick()
@@ -2021,19 +2021,22 @@
 extField
-Nam
+Messag
 e.getTex
@@ -2314,19 +2314,22 @@
 extField
-Nam
+Messag
 e.render

Fix announcement box text limit too short