package net.glowstone.net.codec.play.entity;

import com.flowpowered.network.Codec;
import com.flowpowered.network.util.ByteBufUtils;
import io.netty.buffer.ByteBuf;
import net.glowstone.net.GlowBufUtils;
import net.glowstone.net.message.play.entity.SpawnPaintingMessage;
import org.bukkit.util.BlockVector;

import java.io.IOException;
import java.util.UUID;

public final class SpawnPaintingCodec implements Codec<SpawnPaintingMessage> {

    @Override
    public SpawnPaintingMessage decode(ByteBuf buf) throws IOException {
        int id = ByteBufUtils.readVarInt(buf);
        UUID uuid = GlowBufUtils.readUuid(buf);
        int artId = ByteBufUtils.readVarInt(buf);
        BlockVector vector = GlowBufUtils.readBlockPosition(buf);
        int facing = buf.readByte();
        return new SpawnPaintingMessage(id, uuid, artId, vector.getBlockX(), vector.getBlockY(),
            vector.getBlockZ(), facing);
    }

    @Override
    public ByteBuf encode(ByteBuf buf, SpawnPaintingMessage message) throws IOException {
        ByteBufUtils.writeVarInt(buf, message.getId());
        GlowBufUtils.writeUuid(buf, message.getUniqueId());
        ByteBufUtils.writeVarInt(buf, message.getArtId());
        GlowBufUtils.writeBlockPosition(buf, message.getX(), message.getY(), message.getZ());
        buf.writeByte(message.getFacing());
        return buf;
    }
}
