
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.GtsCommonGUIMenu;
import net.mcreator.gts.procedures.MainPage1NextPageProcedure;
import net.mcreator.gts.procedures.MainMenuTokiConfigProcedure;
import net.mcreator.gts.procedures.MainMenuCandyConfigProcedure;
import net.mcreator.gts.procedures.MainMenuAraConfigProcedure;
import net.mcreator.gts.procedures.GtsNaturalHeightIncreaseProcedure;
import net.mcreator.gts.procedures.GtsNaturalHeightDecreaseProcedure;
import net.mcreator.gts.procedures.GiantessTurnSpeedIncreaseProcedure;
import net.mcreator.gts.procedures.GiantessTurnSpeedDecreaseProcedure;
import net.mcreator.gts.procedures.DisableStomachSoundProcedure;
import net.mcreator.gts.procedures.DisableRaidWaveSizeIncreaseProcedure;
import net.mcreator.gts.procedures.DisableNaturalSpawnProcedure;
import net.mcreator.gts.procedures.DisableLockOntoTargetProcedure;
import net.mcreator.gts.procedures.DisableClothingDamageProcedure;
import net.mcreator.gts.procedures.DisableBreakLeavesWhenContactProcedure;
import net.mcreator.gts.procedures.DisableBelchProcedure;
import net.mcreator.gts.procedures.ConfigResetProcedure;
import net.mcreator.gts.GtsMod;

import java.util.HashMap;

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

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

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

			GtsNaturalHeightIncreaseProcedure.execute(world);
		}
		if (buttonID == 1) {

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

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

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

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

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

			ConfigResetProcedure.execute(world, entity);
		}
		if (buttonID == 7) {

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

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

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

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

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

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

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

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

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

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