
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.TamedGtsInteractionMenuMenu;
import net.mcreator.gts.procedures.TamedGtsSizeIncreaseProcedure;
import net.mcreator.gts.procedures.TamedGtsSizeDecreaseProcedure;
import net.mcreator.gts.procedures.TamedGtsSitProcedure;
import net.mcreator.gts.GtsMod;

import java.util.HashMap;

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

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

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

			TamedGtsSitProcedure.execute(world, entity);
		}
		if (buttonID == 1) {

			TamedGtsSizeIncreaseProcedure.execute(world, entity);
		}
		if (buttonID == 2) {

			TamedGtsSizeDecreaseProcedure.execute(world, entity);
		}
	}

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