
package net.mcreator.gts.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.gts.world.inventory.AraConfigGUIMenu;
import net.mcreator.gts.procedures.DisableAraTailSqueezeProcedure;
import net.mcreator.gts.procedures.DisableAraStepProcedure;
import net.mcreator.gts.procedures.DisableAraMiddleTailSlapProcedure;
import net.mcreator.gts.procedures.DisableAraMiddleGrabProcedure;
import net.mcreator.gts.procedures.DisableAraLowTailSwipeProcedure;
import net.mcreator.gts.procedures.DisableAraLowGrabProcedure;
import net.mcreator.gts.procedures.DisableAraEatProcedure;
import net.mcreator.gts.procedures.DisableAraChompProcedure;
import net.mcreator.gts.procedures.DisableAraBreachProcedure;
import net.mcreator.gts.procedures.DisableAraAboveAttackProcedure;
import net.mcreator.gts.procedures.AraConfigMainMenuProcedure;
import net.mcreator.gts.GtsMod;

import java.util.HashMap;

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

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

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

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

			DisableAraStepProcedure.execute(world);
		}
		if (buttonID == 2) {

			DisableAraLowTailSwipeProcedure.execute(world);
		}
		if (buttonID == 3) {

			DisableAraTailSqueezeProcedure.execute(world);
		}
		if (buttonID == 4) {

			DisableAraLowGrabProcedure.execute(world);
		}
		if (buttonID == 5) {

			DisableAraMiddleTailSlapProcedure.execute(world);
		}
		if (buttonID == 6) {

			DisableAraMiddleGrabProcedure.execute(world);
		}
		if (buttonID == 7) {

			DisableAraChompProcedure.execute(world);
		}
		if (buttonID == 8) {

			DisableAraAboveAttackProcedure.execute(world);
		}
		if (buttonID == 9) {

			DisableAraBreachProcedure.execute(world);
		}
		if (buttonID == 10) {

			DisableAraEatProcedure.execute(world);
		}
	}

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