package com.tiedup.remake.network.sync;

import com.tiedup.remake.network.base.AbstractPlayerSyncPacket;
import com.tiedup.remake.v2.blocks.PetBedBlock;
import java.util.UUID;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * Sync packet for pet bed state (SIT/SLEEP/CLEAR).
 * Direction: Server → Client (S2C)
 */
public class PacketSyncPetBedState extends AbstractPlayerSyncPacket {

    /** 0=CLEAR, 1=SIT, 2=SLEEP */
    private final byte mode;
    private final BlockPos pos;

    public PacketSyncPetBedState(UUID playerUUID, byte mode, BlockPos pos) {
        super(playerUUID);
        this.mode = mode;
        this.pos = pos;
    }

    public void encode(FriendlyByteBuf buf) {
        encodeUUID(buf);
        buf.writeByte(mode);
        buf.writeBlockPos(pos);
    }

    public static PacketSyncPetBedState decode(FriendlyByteBuf buf) {
        UUID uuid = decodeUUID(buf);
        byte mode = buf.readByte();
        BlockPos pos = buf.readBlockPos();
        return new PacketSyncPetBedState(uuid, mode, pos);
    }

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

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

        private static void handle(PacketSyncPetBedState pkt, Player player) {
            if (pkt.mode == 0) {
                // CLEAR — only stop animation if no bondage animation should be playing
                // (AnimationTickHandler will re-apply bondage anim on next tick if needed)
                com.tiedup.remake.client.animation.BondageAnimationManager.stopAnimation(
                    player
                );
                com.tiedup.remake.client.state.PetBedClientState.clear(
                    player.getUUID()
                );
            } else {
                // Compute bed facing angle from block state
                float facingYRot = 0f;
                BlockState state = player.level().getBlockState(pkt.pos);
                if (state.hasProperty(PetBedBlock.FACING)) {
                    facingYRot = state.getValue(PetBedBlock.FACING).toYRot();
                }

                com.tiedup.remake.client.state.PetBedClientState.set(
                    player.getUUID(),
                    pkt.mode,
                    facingYRot
                );

                if (pkt.mode == 1) {
                    com.tiedup.remake.client.animation.BondageAnimationManager.playAnimation(
                        player,
                        "pet_bed_sit"
                    );
                } else if (pkt.mode == 2) {
                    com.tiedup.remake.client.animation.BondageAnimationManager.playAnimation(
                        player,
                        "pet_bed_sleep"
                    );
                }
            }
        }
    }
}
