package com.tiedup.remake.network.base;

import java.util.function.Supplier;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.network.NetworkEvent;

/**
 * Abstract base class for client-bound packets.
 *
 * Provides the standard handle() implementation that:
 * 1. Enqueues work to the main thread
 * 2. Checks for client distribution
 * 3. Calls the abstract handleClientImpl()
 *
 * Subclasses must implement:
 * - encode(FriendlyByteBuf) - serialize packet data
 * - decode(FriendlyByteBuf) - static, deserialize packet data
 * - handleClientImpl() - client-side logic
 */
public abstract class AbstractClientPacket {

    /**
     * Handle the packet on the receiving side (client).
     * This runs on the network thread, so we enqueue to main thread.
     *
     * @param ctx The network context
     */
    public void handle(Supplier<NetworkEvent.Context> ctx) {
        ctx
            .get()
            .enqueueWork(() -> {
                if (FMLEnvironment.dist == Dist.CLIENT) {
                    handleClientImpl();
                }
            });
        ctx.get().setPacketHandled(true);
    }

    /**
     * Client-side packet handling implementation.
     * Called on the main client thread after the packet is received.
     */
    @OnlyIn(Dist.CLIENT)
    protected abstract void handleClientImpl();
}
