
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.CreativeSettingsGUIMenu;
import net.mcreator.puzzle_code.procedures.TimeSetNightCreativeItemProcedure;
import net.mcreator.puzzle_code.procedures.TimeSetDayCreativeItemProcedure;
import net.mcreator.puzzle_code.procedures.SpawnpointCreativeItemProcedure;
import net.mcreator.puzzle_code.procedures.ResetHungerCreativeItemProcedure;
import net.mcreator.puzzle_code.procedures.ResetHPCreativeItemProcedure;
import net.mcreator.puzzle_code.procedures.PlaceAFakeBlockProcedure;
import net.mcreator.puzzle_code.procedures.OpenCreativeSettingsGuiCreativeItemsProcedure;
import net.mcreator.puzzle_code.procedures.OpenCreativeSettingsGUIGamerulesProcedure;
import net.mcreator.puzzle_code.procedures.GamemodeSurvivalCreativeItemProcedure;
import net.mcreator.puzzle_code.procedures.GamemodeSpectatorCreativeItemProcedure;
import net.mcreator.puzzle_code.procedures.GamemodeCreativeCreativeItemProcedure;
import net.mcreator.puzzle_code.procedures.CleanEffectsCreativeItemProcedure;
import net.mcreator.puzzle_code.procedures.BlocksAffectsYouSwichProcedure;
import net.mcreator.puzzle_code.PuzzleCodeMod;

import java.util.HashMap;

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

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

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

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

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

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

			CleanEffectsCreativeItemProcedure.execute(entity);
		}
		if (buttonID == 4) {

			ResetHPCreativeItemProcedure.execute(entity);
		}
		if (buttonID == 5) {

			ResetHungerCreativeItemProcedure.execute(entity);
		}
		if (buttonID == 6) {

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

			GamemodeCreativeCreativeItemProcedure.execute(entity);
		}
		if (buttonID == 8) {

			GamemodeSurvivalCreativeItemProcedure.execute(entity);
		}
		if (buttonID == 9) {

			GamemodeSpectatorCreativeItemProcedure.execute(entity);
		}
		if (buttonID == 10) {

			SpawnpointCreativeItemProcedure.execute(x, y, z, entity);
		}
		if (buttonID == 11) {

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

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

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