package com.lingniu.ingest.protocol.gb32960.codec.parser.v2025;

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;

/**
 * 整车数据 (0x01) —— GB/T 32960.3-2025。固定 18 字节。
 * 相比 2016 版删除加速踏板和制动踏板。
 *
 * <p>为保持查询接口稳定，后续转换层会把 2016/2025 两版整车字段映射到同一组逻辑字段。
 */
public final class VehicleV2025BlockParser implements InfoBlockParser {

    @Override public ProtocolVersion version() { return ProtocolVersion.V2025; }
    @Override public int typeCode() { return 0x01; }
    @Override public int fixedLength() { return 18; }

    @Override
    public InfoBlock parse(ByteBuffer buf) {
        Integer vehicleState = ValueDecoder.u8(buf);
        Integer chargingState = ValueDecoder.u8(buf);
        Integer runningMode = ValueDecoder.u8(buf);
        Double speedKmh = ValueDecoder.scaledU16(buf, 0.1);
        Double mileageKm = ValueDecoder.scaledU32(buf, 0.1);
        Double totalVoltageV = ValueDecoder.scaledU16(buf, 0.1);
        Double totalCurrentA = ValueDecoder.scaledU16WithOffset(buf, 0.1, -1000.0);
        Integer soc = ValueDecoder.u8(buf);
        Integer dcDcStatus = ValueDecoder.u8(buf);
        Integer gearRaw = ValueDecoder.u8raw(buf);
        Integer insulation = ValueDecoder.u16(buf);
        return new InfoBlock.Gb32960V2025.Vehicle(vehicleState, chargingState, runningMode,
                speedKmh, mileageKm, totalVoltageV, totalCurrentA, soc, dcDcStatus, gearRaw,
                insulation);
    }
}
