package com.lingniu.ingest.protocol.gb32960.codec.parser.vendor.guangdong;

import com.lingniu.ingest.protocol.gb32960.codec.InfoBlockParser;
import com.lingniu.ingest.protocol.gb32960.codec.ValueDecoder;
import com.lingniu.ingest.protocol.gb32960.model.InfoBlock;
import com.lingniu.ingest.protocol.gb32960.model.ProtocolVersion;

import java.nio.ByteBuffer;

/**
 * 广东燃料电池规范 §7.2.3.8 表 19：车辆信息（补充）。typeCode {@code 0x34}。
 *
 * <p>固定 7 字节：
 * <pre>
 *   collisionAlarm(1)                0 无 / 1 有
 *   ambientTempC(2, -40 offset, 1°C)
 *   ambientPressureKpa(2, -100 offset, 0.1 kPa)
 *   hydrogenMassKg(2, 0.1 kg)
 * </pre>
 */
public final class GdFcVehicleInfoBlockParser implements InfoBlockParser {

    @Override public ProtocolVersion version() { return ProtocolVersion.V2016; }
    @Override public int typeCode() { return 0x34; }
    @Override public int fixedLength() { return 7; }

    @Override
    public InfoBlock parse(ByteBuffer buf) {
        Integer collision = ValueDecoder.u8raw(buf);
        int rawTemp = buf.getShort() & 0xFFFF;
        // 规范保留值按缺失处理，避免 snapshot 中出现 65494/65495 这种不可读温度。
        Integer tempC = (rawTemp == 0xFFFE || rawTemp == 0xFFFF) ? null : rawTemp - 40;
        Double pressure = ValueDecoder.scaledU16WithOffset(buf, 0.1, -100.0);
        Double h2Mass = ValueDecoder.scaledU16(buf, 0.1);
        return new InfoBlock.GuangdongFc.VehicleInfo(collision, tempC, pressure, h2Mass);
    }
}
