package net.geforcemods.securitycraft.network.server;

import java.util.function.Supplier;

import net.geforcemods.securitycraft.api.CustomizableBlockEntity;
import net.geforcemods.securitycraft.api.IModuleInventory;
import net.geforcemods.securitycraft.api.IOwnable;
import net.geforcemods.securitycraft.misc.ModuleType;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.NetworkEvent;

public class ToggleModule {
	private BlockPos pos;
	private ModuleType moduleType;

	public ToggleModule() {}

	public ToggleModule(BlockPos pos, ModuleType moduleType) {
		this.pos = pos;
		this.moduleType = moduleType;
	}

	public static void encode(ToggleModule message, FriendlyByteBuf buf) {
		buf.writeLong(message.pos.asLong());
		buf.writeEnum(message.moduleType);
	}

	public static ToggleModule decode(FriendlyByteBuf buf) {
		ToggleModule message = new ToggleModule();

		message.pos = BlockPos.of(buf.readLong());
		message.moduleType = buf.readEnum(ModuleType.class);
		return message;
	}

	public static void onMessage(ToggleModule message, Supplier<NetworkEvent.Context> ctx) {
		ctx.get().enqueueWork(() -> {
			BlockPos pos = message.pos;
			Player player = ctx.get().getSender();
			BlockEntity be = player.level.getBlockEntity(pos);

			if (be instanceof IModuleInventory moduleInv && (!(be instanceof IOwnable ownable) || ownable.getOwner().isOwner(player))) {
				ModuleType moduleType = message.moduleType;

				if (moduleInv.isModuleEnabled(moduleType))
					moduleInv.removeModule(moduleType, true);
				else
					moduleInv.insertModule(moduleInv.getModule(moduleType), true);

				if (be instanceof CustomizableBlockEntity)
					player.level.sendBlockUpdated(pos, be.getBlockState(), be.getBlockState(), 3);
			}
		});

		ctx.get().setPacketHandled(true);
	}
}

@@ -302,23 +302,195 @@
 aft.
-misc.ModuleType
+api.LinkableBlockEntity;%0Aimport net.geforcemods.securitycraft.api.LinkedAction;%0Aimport net.geforcemods.securitycraft.misc.ModuleType;%0Aimport net.geforcemods.securitycraft.util.ModuleUtils
 ;%0Aim
@@ -1754,16 +1754,47 @@
 uleType;
+%0A%09%09%09%09LinkedAction linkedAction;
 %0A%0A%09%09%09%09if
@@ -1833,16 +1833,18 @@
 leType))
+ %7B
 %0A%09%09%09%09%09mo
@@ -1887,16 +1887,71 @@
 e);%0A
+%09%09%09%09%09linkedAction = LinkedAction.MODULE_REMOVED;%0A%09%09%09%09%7D%0A
 %09%09%09%09else
 %0A%09%09%09
@@ -1946,16 +1946,18 @@
 %09%09%09%09else
+ %7B
 %0A%09%09%09%09%09mo
@@ -2016,16 +2016,225 @@
 , true);
+%0A%09%09%09%09%09linkedAction = LinkedAction.MODULE_INSERTED;%0A%09%09%09%09%7D%0A%0A%09%09%09%09if (be instanceof LinkableBlockEntity linkable)%0A%09%09%09%09%09ModuleUtils.createLinkedAction(linkedAction, moduleInv.getModule(moduleType), linkable, true);
 %0A%0A%09%09%09%09if
@@ -2369,17 +2369,16 @@
 %7D%0A%09%09%7D);%0A
-%0A
 %09%09ctx.ge

fix module toggling not syncing between lasers