
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.GtsCommonGuiPage2Menu;
import net.mcreator.gts.procedures.MainPage2PreviousPageProcedure;
import net.mcreator.gts.procedures.DisableTamedSeekOutHostileMobProcedure;
import net.mcreator.gts.procedures.DisableTamedFriendlyFireProcedure;
import net.mcreator.gts.procedures.DisableTamedBreachProcedure;
import net.mcreator.gts.procedures.DisableEatAttacksDependonHealthProcedure;
import net.mcreator.gts.procedures.DisableBiomeSpawnProcedure;
import net.mcreator.gts.procedures.ChanceBasedGrabEscapeProcedure;
import net.mcreator.gts.GtsMod;

import java.util.HashMap;

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

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

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

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

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

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

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

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

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

			DisableBiomeSpawnProcedure.execute(world);
		}
	}

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