
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.AdvancedTeleporterBlockGuiMenu;
import net.mcreator.puzzle_code.procedures.EntityTeleporterBlockPage3Procedure;
import net.mcreator.puzzle_code.procedures.EntityTeleporterBlockPage2Procedure;
import net.mcreator.puzzle_code.procedures.EntityTeleporterBlockPage1Procedure;
import net.mcreator.puzzle_code.procedures.EditzPosProcedure;
import net.mcreator.puzzle_code.procedures.EdityPosProcedure;
import net.mcreator.puzzle_code.procedures.EditxPosProcedure;
import net.mcreator.puzzle_code.procedures.ApplyzPosProcedure;
import net.mcreator.puzzle_code.procedures.ApplyyPosProcedure;
import net.mcreator.puzzle_code.procedures.ApplyxPosProcedure;
import net.mcreator.puzzle_code.PuzzleCodeMod;

import java.util.HashMap;

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

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

	public static void handleData(final AdvancedTeleporterBlockGuiButtonMessage 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 = AdvancedTeleporterBlockGuiMenu.guistate;
		// security measure to prevent arbitrary chunk generation
		if (!world.hasChunkAt(new BlockPos(x, y, z)))
			return;
		if (buttonID == 0) {

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

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

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

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

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

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

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

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

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

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