
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.NeutralCodeBlockGUIMenu;
import net.mcreator.puzzle_code.procedures.NBTTypeTextButtonProcedure;
import net.mcreator.puzzle_code.procedures.NBTTypeNumberButtonProcedure;
import net.mcreator.puzzle_code.procedures.NBTTypeLogicButtonProcedure;
import net.mcreator.puzzle_code.procedures.EditNBTValueProcedure;
import net.mcreator.puzzle_code.procedures.EditNBTNameProcedure;
import net.mcreator.puzzle_code.procedures.ApplyNBTValueProcedure;
import net.mcreator.puzzle_code.procedures.ApplyNBTNameProcedure;
import net.mcreator.puzzle_code.PuzzleCodeMod;

import java.util.HashMap;

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

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

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

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

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

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

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

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

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

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

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