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;

/** 0x1205 终端上传文件列表，摘要字段结构化，明细原始 body 保留。 */
public final class FileListBodyParser implements BodyParser {
    @Override public int messageId() { return Jt1078MessageId.TERMINAL_FILE_LIST; }

    @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;
        long fileCount = b.remaining() >= 4 ? b.getInt() & 0xFFFFFFFFL : 0L;
        return new Jt808Body.FileList(responseSerialNo, fileCount, raw);
    }
}
