package com.lingniu.ingest.protocol.jt1078.codec.parser;

import com.lingniu.ingest.protocol.jt1078.model.Jt1078MessageId;
import com.lingniu.ingest.protocol.jt808.codec.BodyParser;
import com.lingniu.ingest.protocol.jt808.model.Jt808Body;
import com.lingniu.ingest.protocol.jt808.model.Jt808Header;

import java.nio.ByteBuffer;

/** 0x1003 终端上传音视频属性。 */
public final class MediaAttributesBodyParser implements BodyParser {
    @Override public int messageId() { return Jt1078MessageId.TERMINAL_MEDIA_ATTRS_REPORT; }

    @Override
    public Jt808Body parse(Jt808Header header, ByteBuffer body) {
        byte[] raw = new byte[body.remaining()];
        body.get(raw);
        ByteBuffer b = ByteBuffer.wrap(raw);
        return new Jt808Body.MediaAttributes(
                readU8(b),
                readU8(b),
                readU8(b),
                readU8(b),
                readU16(b),
                readU8(b) == 1,
                readU8(b),
                readU8(b),
                readU8(b),
                raw);
    }

    private static int readU8(ByteBuffer b) {
        return b.hasRemaining() ? b.get() & 0xFF : 0;
    }

    private static int readU16(ByteBuffer b) {
        return b.remaining() >= 2 ? b.getShort() & 0xFFFF : 0;
    }
}
