
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.EffectBlockGUI3Menu;
import net.mcreator.puzzle_code.procedures.EffectBlockGUIPage3SettingsProcedure;
import net.mcreator.puzzle_code.procedures.EffectBlockGUIPage3Procedure;
import net.mcreator.puzzle_code.procedures.EffectBlockGUIPage3Button5Procedure;
import net.mcreator.puzzle_code.procedures.EffectBlockGUIPage3Button4Procedure;
import net.mcreator.puzzle_code.procedures.EffectBlockGUIPage3Button3Procedure;
import net.mcreator.puzzle_code.procedures.EffectBlockGUIPage3Button2Procedure;
import net.mcreator.puzzle_code.procedures.EffectBlockGUIPage3Button1Procedure;
import net.mcreator.puzzle_code.procedures.EffectBlockGUIPage2Procedure;
import net.mcreator.puzzle_code.procedures.EffectBlockGUIPage1Procedure;
import net.mcreator.puzzle_code.PuzzleCodeMod;

import java.util.HashMap;

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

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

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

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

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

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

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

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

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

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

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

			EffectBlockGUIPage3Procedure.execute(world, x, y, z, entity);
		}
	}

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