package net.mcreator.puzzle_code.client.gui;

import net.neoforged.neoforge.network.PacketDistributor;

import net.minecraft.world.level.Level;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.Minecraft;

import net.mcreator.puzzle_code.world.inventory.PositionSetGUIMenu;
import net.mcreator.puzzle_code.procedures.VarReturnPositionSetZPos2Procedure;
import net.mcreator.puzzle_code.procedures.VarReturnPositionSetYPos2Procedure;
import net.mcreator.puzzle_code.procedures.VarReturnPositionSetXPos2Procedure;
import net.mcreator.puzzle_code.network.PositionSetGUIButtonMessage;

import java.util.HashMap;

import com.mojang.blaze3d.systems.RenderSystem;

public class PositionSetGUIScreen extends AbstractContainerScreen<PositionSetGUIMenu> {
	private final static HashMap<String, Object> guistate = PositionSetGUIMenu.guistate;
	private final Level world;
	private final int x, y, z;
	private final Player entity;
	EditBox setNBTField;
	Button button_copy;
	Button button_paste;
	Button button_paste2;
	Button button_paste_area_1;

	public PositionSetGUIScreen(PositionSetGUIMenu container, Inventory inventory, Component text) {
		super(container, inventory, text);
		this.world = container.world;
		this.x = container.x;
		this.y = container.y;
		this.z = container.z;
		this.entity = container.entity;
		this.imageWidth = 179;
		this.imageHeight = 139;
	}

	private static final ResourceLocation texture = ResourceLocation.parse("puzzle_code:textures/screens/position_set_gui.png");

	@Override
	public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
		this.renderBackground(guiGraphics, mouseX, mouseY, partialTicks);
		super.render(guiGraphics, mouseX, mouseY, partialTicks);
		setNBTField.render(guiGraphics, mouseX, mouseY, partialTicks);
		this.renderTooltip(guiGraphics, mouseX, mouseY);
	}

	@Override
	protected void renderBg(GuiGraphics guiGraphics, float partialTicks, int gx, int gy) {
		RenderSystem.setShaderColor(1, 1, 1, 1);
		RenderSystem.enableBlend();
		RenderSystem.defaultBlendFunc();
		guiGraphics.blit(texture, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight, this.imageWidth, this.imageHeight);
		RenderSystem.disableBlend();
	}

	@Override
	public boolean keyPressed(int key, int b, int c) {
		if (key == 256) {
			this.minecraft.player.closeContainer();
			return true;
		}
		if (setNBTField.isFocused())
			return setNBTField.keyPressed(key, b, c);
		return super.keyPressed(key, b, c);
	}

	@Override
	public void resize(Minecraft minecraft, int width, int height) {
		String setNBTFieldValue = setNBTField.getValue();
		super.resize(minecraft, width, height);
		setNBTField.setValue(setNBTFieldValue);
	}

	@Override
	protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
		guiGraphics.drawString(this.font, Component.translatable("gui.puzzle_code.position_set_gui.label_x_varpositionsetxpos2"), 8, 70, -12829636, false);
		guiGraphics.drawString(this.font, Component.translatable("gui.puzzle_code.position_set_gui.label_y_varpositionsetypos2"), 8, 83, -12829636, false);
		guiGraphics.drawString(this.font, Component.translatable("gui.puzzle_code.position_set_gui.label_z_varpositionsetzpos2"), 8, 97, -12829636, false);
		guiGraphics.drawString(this.font,

				VarReturnPositionSetXPos2Procedure.execute(entity), 71, 70, -12829636, false);
		guiGraphics.drawString(this.font,

				VarReturnPositionSetYPos2Procedure.execute(entity), 71, 83, -12829636, false);
		guiGraphics.drawString(this.font,

				VarReturnPositionSetZPos2Procedure.execute(entity), 71, 97, -12829636, false);
		guiGraphics.drawString(this.font, Component.translatable("gui.puzzle_code.position_set_gui.label_block_position"), 8, 56, -12829636, false);
	}

	@Override
	public void init() {
		super.init();
		setNBTField = new EditBox(this.font, this.leftPos + 9, this.topPos + 111, 160, 18, Component.translatable("gui.puzzle_code.position_set_gui.setNBTField")) {
			@Override
			public void insertText(String text) {
				super.insertText(text);
				if (getValue().isEmpty())
					setSuggestion(Component.translatable("gui.puzzle_code.position_set_gui.setNBTField").getString());
				else
					setSuggestion(null);
			}

			@Override
			public void moveCursorTo(int pos, boolean flag) {
				super.moveCursorTo(pos, flag);
				if (getValue().isEmpty())
					setSuggestion(Component.translatable("gui.puzzle_code.position_set_gui.setNBTField").getString());
				else
					setSuggestion(null);
			}
		};
		setNBTField.setMaxLength(32767);
		setNBTField.setSuggestion(Component.translatable("gui.puzzle_code.position_set_gui.setNBTField").getString());
		guistate.put("text:setNBTField", setNBTField);
		this.addWidget(this.setNBTField);
		button_copy = Button.builder(Component.translatable("gui.puzzle_code.position_set_gui.button_copy"), e -> {
			if (true) {
				PacketDistributor.sendToServer(new PositionSetGUIButtonMessage(0, x, y, z));
				PositionSetGUIButtonMessage.handleButtonAction(entity, 0, x, y, z);
			}
		}).bounds(this.leftPos + 8, this.topPos + 7, 81, 20).build();
		guistate.put("button:button_copy", button_copy);
		this.addRenderableWidget(button_copy);
		button_paste = Button.builder(Component.translatable("gui.puzzle_code.position_set_gui.button_paste"), e -> {
			if (true) {
				PacketDistributor.sendToServer(new PositionSetGUIButtonMessage(1, x, y, z));
				PositionSetGUIButtonMessage.handleButtonAction(entity, 1, x, y, z);
			}
		}).bounds(this.leftPos + 94, this.topPos + 7, 76, 20).build();
		guistate.put("button:button_paste", button_paste);
		this.addRenderableWidget(button_paste);
		button_paste2 = Button.builder(Component.translatable("gui.puzzle_code.position_set_gui.button_paste2"), e -> {
			if (true) {
				PacketDistributor.sendToServer(new PositionSetGUIButtonMessage(2, x, y, z));
				PositionSetGUIButtonMessage.handleButtonAction(entity, 2, x, y, z);
			}
		}).bounds(this.leftPos + 94, this.topPos + 29, 76, 20).build();
		guistate.put("button:button_paste2", button_paste2);
		this.addRenderableWidget(button_paste2);
		button_paste_area_1 = Button.builder(Component.translatable("gui.puzzle_code.position_set_gui.button_paste_area_1"), e -> {
			if (true) {
				PacketDistributor.sendToServer(new PositionSetGUIButtonMessage(3, x, y, z));
				PositionSetGUIButtonMessage.handleButtonAction(entity, 3, x, y, z);
			}
		}).bounds(this.leftPos + 8, this.topPos + 29, 81, 20).build();
		guistate.put("button:button_paste_area_1", button_paste_area_1);
		this.addRenderableWidget(button_paste_area_1);
	}
}
