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

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-2016 表 B.10。固定 20 字节：
 * vehicleState(1) + chargingState(1) + runningMode(1) + speedKmh(2)
 * + totalMileageKm(4) + totalVoltageV(2) + totalCurrentA(2)
 * + socPercent(1) + dcDcStatus(1) + gearRaw(1) + insulationKohm(2)
 * + accelPedal(1) + brakePedal(1)。
 *
 * <p>该块是实时事件和 snapshot 的核心公共字段来源，字段名变更会影响前端常用查询。
 */
public final class VehicleV2016BlockParser implements InfoBlockParser {

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

    @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);
        Integer accelPedal = ValueDecoder.u8(buf);
        Integer brakePedal = ValueDecoder.u8(buf);
        return new InfoBlock.Gb32960V2016.Vehicle(vehicleState, chargingState, runningMode,
                speedKmh, mileageKm, totalVoltageV, totalCurrentA, soc, dcDcStatus, gearRaw,
                insulation, accelPedal, brakePedal);
    }
}
