package com.tiedup.remake.network.sync;

import com.tiedup.remake.network.base.AbstractPlayerSyncPacket;
import java.util.UUID;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * Test packet to play an arbitrary animation on a player.
 * Direction: Server → Client (S2C)
 * Used by /tiedup testanim command for debugging animations.
 */
public class PacketPlayTestAnimation extends AbstractPlayerSyncPacket {

    private final String animId;

    public PacketPlayTestAnimation(UUID playerUUID, String animId) {
        super(playerUUID);
        this.animId = animId;
    }

    public void encode(FriendlyByteBuf buf) {
        encodeUUID(buf);
        buf.writeUtf(animId);
    }

    public static PacketPlayTestAnimation decode(FriendlyByteBuf buf) {
        UUID uuid = decodeUUID(buf);
        String animId = buf.readUtf();
        return new PacketPlayTestAnimation(uuid, animId);
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    protected void applySync(Player player) {
        ClientHandler.handle(this, player);
    }

    @OnlyIn(Dist.CLIENT)
    private static class ClientHandler {

        private static void handle(PacketPlayTestAnimation pkt, Player player) {
            if (pkt.animId.isEmpty()) {
                com.tiedup.remake.client.animation.BondageAnimationManager.stopAnimation(
                    player
                );
            } else {
                com.tiedup.remake.client.animation.BondageAnimationManager.playAnimation(
                    player,
                    pkt.animId
                );
            }
        }
    }
}
