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;

/** 0x1206 文件上传完成通知。 */
public final class FileUploadCompleteBodyParser implements BodyParser {
    @Override public int messageId() { return Jt1078MessageId.TERMINAL_FILE_UPLOAD_DONE; }

    @Override
    public Jt808Body parse(Jt808Header header, ByteBuffer body) {
        byte[] raw = new byte[body.remaining()];
        body.get(raw);
        ByteBuffer b = ByteBuffer.wrap(raw);
        int responseSerialNo = b.remaining() >= 2 ? b.getShort() & 0xFFFF : 0;
        int result = b.hasRemaining() ? b.get() & 0xFF : 0;
        return new Jt808Body.FileUploadComplete(responseSerialNo, result, raw);
    }
}
