package me.koba1.led.device.packets;

import me.koba1.led.Main;
import me.koba1.led.model.LedStrip;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Collection;

public class UpdateLedStripsPacket extends Packet {
    @Override
    public byte getPacketId() {
        return PacketType.UPDATE_LEDSTRIPS.getId();
    }

    @Override
    protected byte[] getPayload() {
        Collection<LedStrip> strips = Main.getInstance().getLedStripManager().getLedStrips().values();
        ByteBuffer buffer = ByteBuffer.allocate(1 + strips.size() * 4).order(ByteOrder.LITTLE_ENDIAN);
        buffer.put((byte) strips.size());

        for (LedStrip strip : strips) {
            buffer.put((byte) strip.getId())
                    .put((byte) strip.getPin())
                    .putShort((short) strip.getLedCount());
        }
        return buffer.array();
    }
}
