
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.CandyConfigGuiMenu;
import net.mcreator.gts.procedures.DisableCandyVoreProcedure;
import net.mcreator.gts.procedures.DisableCandyUpperSwipeProcedure;
import net.mcreator.gts.procedures.DisableCandyUpperSmashProcedure;
import net.mcreator.gts.procedures.DisableCandyThunderMagicProcedure;
import net.mcreator.gts.procedures.DisableCandyStompProcedure;
import net.mcreator.gts.procedures.DisableCandySpitProcedure;
import net.mcreator.gts.procedures.DisableCandyKickProcedure;
import net.mcreator.gts.procedures.DisableCandyHeartMagicProcedure;
import net.mcreator.gts.procedures.DisableCandyGroundSlamProcedure;
import net.mcreator.gts.procedures.DisableCandyBreachProcedure;
import net.mcreator.gts.procedures.CandyConfigMainMenuProcedure;
import net.mcreator.gts.GtsMod;

import java.util.HashMap;

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

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

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

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

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

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

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

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

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

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

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

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

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

			DisableCandyThunderMagicProcedure.execute(world);
		}
	}

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