
package net.mcreator.puzzle_code.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.puzzle_code.world.inventory.CustomEffectBlockGUI2Menu;
import net.mcreator.puzzle_code.procedures.SwitchParticlesProcedure;
import net.mcreator.puzzle_code.procedures.SwitchAmbientProcedure;
import net.mcreator.puzzle_code.procedures.OpenPowerEffectGUIProcedure;
import net.mcreator.puzzle_code.procedures.OpenDurationEffectGUIProcedure;
import net.mcreator.puzzle_code.procedures.FakeProcedureProcedure;
import net.mcreator.puzzle_code.procedures.CustomEffectBlockGUIScreenProcedure;
import net.mcreator.puzzle_code.procedures.CustomEffectBlockGUIScreen4Procedure;
import net.mcreator.puzzle_code.procedures.CustomEffectBlockGUIScreen3Procedure;
import net.mcreator.puzzle_code.PuzzleCodeMod;

import java.util.HashMap;

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

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

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

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

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

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

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

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

			SwitchAmbientProcedure.execute(world, x, y, z);
		}
		if (buttonID == 7) {

			FakeProcedureProcedure.execute();
		}
		if (buttonID == 9) {

			SwitchParticlesProcedure.execute(world, x, y, z);
		}
		if (buttonID == 10) {

			FakeProcedureProcedure.execute();
		}
	}

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