package de.ctdo.bunti.artnet.packets;

import de.ctdo.bunti.artnet.ByteUtils;

public abstract class ArtNetPacket {
    private static final byte[] HEADER = "Art-Net\0".getBytes();
    private static final int PROTOCOL_VERSION = 14;
    private static final int OFFSET_HEADER = 0;
    private static final int OFFSET_OPCODE = 8;
    private static final int OFFSET_PROTOCOL_VERSION = 10;

    private ByteUtils data;
    private final PacketType type;

    public ArtNetPacket(PacketType type, int length) {
        this.type = type;
        setData(new byte[length]);

        // Set header
        data.setByteChunk(HEADER, OFFSET_HEADER, HEADER.length);
        data.setInt16LE(type.getOpCode(), OFFSET_OPCODE);

        // Set protocol
        data.setInt16(PROTOCOL_VERSION, OFFSET_PROTOCOL_VERSION);
    }

    public final ByteUtils getData() {
        return data;
    }

    public final byte[] getRawData() {
        return data.getBytes();
    }

    public int getLength() {
        return getData().getLength();
    }

    public final void setData(byte[] data) {
        this.data = new ByteUtils(data);
    }

    public final PacketType getType() {
        return type;
    }
}
