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;
import java.util.ArrayList;
import java.util.List;

/**
 * 驱动电机数据 (0x02) —— GB/T 32960.3-2025 表 16。变长。
 * 1 字节电机个数 + N × 14 字节电机：
 * serialNo(1) state(1) ctrlTempC(1) rpm(2,offset -32000)
 * torque(4,offset -20000,0.1 N·m) motorTempC(1)
 * ctrlVoltageV(2,0.1V) ctrlCurrentA(2,0.1A,offset -1000)。
 *
 * <p>相比 2016 版：rpm 偏移变为 -32000；torque 由 2 字节扩展为 4 字节。
 */
public final class DriveMotorV2025BlockParser implements InfoBlockParser {

    @Override public ProtocolVersion version() { return ProtocolVersion.V2025; }
    @Override public int typeCode() { return 0x02; }
    @Override public int fixedLength() { return -1; }

    @Override
    public InfoBlock parse(ByteBuffer buf) {
        int count = buf.get() & 0xFF;
        List<InfoBlock.Gb32960V2025.DriveMotor.Motor> motors = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            // 2025 版 torque 扩为 4 字节，不能沿用 2016 的 u16 解析。
            Integer serialNo = ValueDecoder.u8(buf);
            Integer state = ValueDecoder.u8(buf);
            Integer ctrlTempC = ValueDecoder.tempOffset40(buf);
            Integer rawRpm = ValueDecoder.u16(buf);
            Integer rpm = rawRpm == null ? null : rawRpm - 32_000;
            Long rawTq = ValueDecoder.u32(buf);
            Double torque = rawTq == null ? null : (rawTq - 20_000L) * 0.1;
            Integer motorTempC = ValueDecoder.tempOffset40(buf);
            Double ctrlVoltage = ValueDecoder.scaledU16(buf, 0.1);
            Double ctrlCurrent = ValueDecoder.scaledU16WithOffset(buf, 0.1, -1000.0);
            motors.add(new InfoBlock.Gb32960V2025.DriveMotor.Motor(
                    serialNo, state, ctrlTempC, rpm, torque, motorTempC, ctrlVoltage, ctrlCurrent));
        }
        return new InfoBlock.Gb32960V2025.DriveMotor(motors);
    }
}
