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

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 com.lingniu.ingest.protocol.jt808.model.Jt808MessageId;

import java.nio.ByteBuffer;

public final class MediaEventBodyParser implements BodyParser {

    @Override
    public int messageId() {
        return Jt808MessageId.TERMINAL_MEDIA_EVENT;
    }

    @Override
    public Jt808Body parse(Jt808Header header, ByteBuffer body) {
        long mediaId = body.remaining() >= 4 ? body.getInt() & 0xFFFFFFFFL : 0;
        int mediaType = body.hasRemaining() ? body.get() & 0xFF : 0;
        int mediaFormat = body.hasRemaining() ? body.get() & 0xFF : 0;
        int eventCode = body.hasRemaining() ? body.get() & 0xFF : 0;
        int channelId = body.hasRemaining() ? body.get() & 0xFF : 0;
        return new Jt808Body.MediaEvent(mediaId, mediaType, mediaFormat, eventCode, channelId);
    }
}
