
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.TokiConfigMenu1Menu;
import net.mcreator.gts.procedures.TokiConfigMainMenuProcedure;
import net.mcreator.gts.procedures.DisableVoreProcedure;
import net.mcreator.gts.procedures.DisableUpperBreachProcedure;
import net.mcreator.gts.procedures.DisableTrappedBreachProcedure;
import net.mcreator.gts.procedures.DisableThrowProcedure;
import net.mcreator.gts.procedures.DisableStompProcedure;
import net.mcreator.gts.procedures.DisableSpitProcedure;
import net.mcreator.gts.procedures.DisableSniperProcedure;
import net.mcreator.gts.procedures.DisableMiddleBreachProcedure;
import net.mcreator.gts.procedures.DisableLowerBreachProcedure;
import net.mcreator.gts.procedures.DisableKickProcedure;
import net.mcreator.gts.procedures.DisableGunSmashProcedure;
import net.mcreator.gts.procedures.DisableGunSlapProcedure;
import net.mcreator.gts.procedures.DisableGroundSlamProcedure;
import net.mcreator.gts.procedures.DisableDroneProcedure;
import net.mcreator.gts.procedures.DisableCelebrationProcedure;
import net.mcreator.gts.GtsMod;

import java.util.HashMap;

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

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

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

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

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

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

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

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

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

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

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

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

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

			DisableDroneProcedure.execute(world);
		}
		if (buttonID == 11) {

			DisableUpperBreachProcedure.execute(world);
		}
		if (buttonID == 12) {

			DisableMiddleBreachProcedure.execute(world);
		}
		if (buttonID == 13) {

			DisableLowerBreachProcedure.execute(world);
		}
		if (buttonID == 14) {

			DisableTrappedBreachProcedure.execute(world);
		}
		if (buttonID == 15) {

			DisableCelebrationProcedure.execute(world);
		}
	}

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