
package net.mcreator.puzzle_code.network;

import net.neoforged.neoforge.network.handling.IPayloadContext;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.bus.api.SubscribeEvent;

import net.minecraft.world.level.Level;
import net.minecraft.world.entity.player.Player;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.chat.Component;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.core.BlockPos;

import net.mcreator.puzzle_code.world.inventory.FillerCodeBlockGUI2Menu;
import net.mcreator.puzzle_code.procedures.FillerCodeBlockGUiPage2Procedure;
import net.mcreator.puzzle_code.procedures.FillerCodeBlockGUIPage3Procedure;
import net.mcreator.puzzle_code.procedures.FillerCodeBlockGUIPage1Procedure;
import net.mcreator.puzzle_code.procedures.EditzPos2Procedure;
import net.mcreator.puzzle_code.procedures.EdityPos2Procedure;
import net.mcreator.puzzle_code.procedures.EditxPos2Procedure;
import net.mcreator.puzzle_code.procedures.ApplyzPos2Procedure;
import net.mcreator.puzzle_code.procedures.ApplyyPos2Procedure;
import net.mcreator.puzzle_code.procedures.ApplyxPos2Procedure;
import net.mcreator.puzzle_code.PuzzleCodeMod;

import java.util.HashMap;

@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD)
public record FillerCodeBlockGUI2ButtonMessage(int buttonID, int x, int y, int z) implements CustomPacketPayload {

	public static final Type<FillerCodeBlockGUI2ButtonMessage> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(PuzzleCodeMod.MODID, "filler_code_block_gui_2_buttons"));
	public static final StreamCodec<RegistryFriendlyByteBuf, FillerCodeBlockGUI2ButtonMessage> STREAM_CODEC = StreamCodec.of((RegistryFriendlyByteBuf buffer, FillerCodeBlockGUI2ButtonMessage message) -> {
		buffer.writeInt(message.buttonID);
		buffer.writeInt(message.x);
		buffer.writeInt(message.y);
		buffer.writeInt(message.z);
	}, (RegistryFriendlyByteBuf buffer) -> new FillerCodeBlockGUI2ButtonMessage(buffer.readInt(), buffer.readInt(), buffer.readInt(), buffer.readInt()));
	@Override
	public Type<FillerCodeBlockGUI2ButtonMessage> type() {
		return TYPE;
	}

	public static void handleData(final FillerCodeBlockGUI2ButtonMessage message, final IPayloadContext context) {
		if (context.flow() == PacketFlow.SERVERBOUND) {
			context.enqueueWork(() -> {
				Player entity = context.player();
				int buttonID = message.buttonID;
				int x = message.x;
				int y = message.y;
				int z = message.z;
				handleButtonAction(entity, buttonID, x, y, z);
			}).exceptionally(e -> {
				context.connection().disconnect(Component.literal(e.getMessage()));
				return null;
			});
		}
	}

	public static void handleButtonAction(Player entity, int buttonID, int x, int y, int z) {
		Level world = entity.level();
		HashMap guistate = FillerCodeBlockGUI2Menu.guistate;
		// security measure to prevent arbitrary chunk generation
		if (!world.hasChunkAt(new BlockPos(x, y, z)))
			return;
		if (buttonID == 0) {

			FillerCodeBlockGUIPage1Procedure.execute(world, x, y, z, entity);
		}
		if (buttonID == 1) {

			FillerCodeBlockGUIPage3Procedure.execute(world, x, y, z, entity);
		}
		if (buttonID == 2) {

			FillerCodeBlockGUiPage2Procedure.execute(world, x, y, z, entity);
		}
		if (buttonID == 3) {

			ApplyxPos2Procedure.execute(world, x, y, z, guistate);
		}
		if (buttonID == 4) {

			ApplyyPos2Procedure.execute(world, x, y, z, guistate);
		}
		if (buttonID == 5) {

			ApplyzPos2Procedure.execute(world, x, y, z, guistate);
		}
		if (buttonID == 6) {

			EditxPos2Procedure.execute(world, x, y, z, guistate);
		}
		if (buttonID == 7) {

			EdityPos2Procedure.execute(world, x, y, z, guistate);
		}
		if (buttonID == 8) {

			EditzPos2Procedure.execute(world, x, y, z, guistate);
		}
	}

	@SubscribeEvent
	public static void registerMessage(FMLCommonSetupEvent event) {
		PuzzleCodeMod.addNetworkMessage(FillerCodeBlockGUI2ButtonMessage.TYPE, FillerCodeBlockGUI2ButtonMessage.STREAM_CODEC, FillerCodeBlockGUI2ButtonMessage::handleData);
	}
}
