package com.zksy.gas.utils;

import com.zksy.gas.domain.GasMonitorData;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

public class DataParser {

    public static GasMonitorData parseMessage(byte[] frame) {
        GasMonitorData data = new GasMonitorData();

        // 帧类型
        data.setFrameType(frame[3] & 0xFF);

        // MAC 地址 (4~11)
        byte[] macBytes = new byte[8];
        System.arraycopy(frame, 4, macBytes, 0, 8);
        data.setMacAddress(ProtocolUtils.bytesToHex(macBytes));

        // 短地址 (12~13)
        int shortAddressDecimal = ((frame[12] & 0xFF) << 8) | (frame[13] & 0xFF);
        data.setShortAddress(String.valueOf(shortAddressDecimal));

        // 自动应答 (14)
        data.setAutoReplyOption( frame[14] & 0xFF);

        // 跳过 AA 帧头(15) 和数据长度(16)
        int offset = 17;

        // 序号 (17)
        data.setSequenceNo(frame[offset] & 0xFF);
        offset++;

        // 命令 (18)
        data.setCommand(frame[offset] & 0xFF);
        offset++;

        // 设备属性 (19)
        data.setDeviceAttr(frame[offset] & 0xFF);
        offset++;

        // 协议版本 (20)
        data.setProtocolVersion(String.valueOf(frame[offset] & 0xFF));
        offset++;

        // 报警信息 (21~22)
        int alarmInfo = (frame[offset] & 0xFF ) | ((frame[offset + 1] & 0xFF) << 8);
        data.setAlarmInfo(alarmInfo);
        offset += 2;

        // 时间戳 (23~28)
        LocalDateTime reportTime = ProtocolUtils.parseTimestamp(frame, offset);
        data.setReportTime(reportTime);
        offset += 6;

        // 数据个数 (29)
        int dataCount = frame[offset] & 0xFF;
        offset++;

        // ------------------- 数据段 -------------------

        // 1. 气体浓度 + 类型 (6 字节)
        int gasTypeCode = frame[offset] & 0xFF; // 第1字节：气体类型
        int unitCode = frame[offset + 1] & 0xFF; // 第2字节：单位
        offset += 2;

        long gasValueRaw = ((long)(frame[offset + 3] & 0xFF) << 24)
                | ((long)(frame[offset + 2] & 0xFF) << 16)
                | ((long)(frame[offset + 1] & 0xFF) << 8)
                | ((long)(frame[offset] & 0xFF));
        offset += 4;

        String gasType;
        switch (gasTypeCode) {
            case 0x04: gasType = "CH4"; break;
            case 0x05: gasType = "CO"; break;
            case 0x06: gasType = "O2"; break;
            default: gasType = "未知";
        }

        BigDecimal gasConcentration = BigDecimal.valueOf(gasValueRaw / 10.0);
        data.setGasType(gasType);
        data.setGasConcentration(gasConcentration);
        data.setGasUnit(unitCode == 0x10 ? "%LEL" : "未知单位");

        // 2. 电池电量
        float battery = ProtocolUtils.readFloat6(frame, offset);
        data.setBattery(BigDecimal.valueOf(battery));
        offset += 6;

        // 3. 信号强度
        float signal = ProtocolUtils.readFloat6(frame, offset);
        data.setSignalStrength(signal);
        offset += 6;

        // 4. 温度（保留两位小数）
        float temperature = ProtocolUtils.readFloat6(frame, offset);
        data.setTemperature(new BigDecimal(temperature).setScale(2, RoundingMode.HALF_UP).floatValue());
        offset += 6;

        // 5. 湿度（保留两位小数）
        float humidity = ProtocolUtils.readFloat6(frame, offset);
        data.setHumidity(new BigDecimal(humidity).setScale(2, RoundingMode.HALF_UP).floatValue());
        offset += 6;

        // 6. 经度
        float lon = ProtocolUtils.readFloat6(frame, offset);
        data.setLongitude(BigDecimal.valueOf(lon));
        offset += 6;

        // 7. 纬度
        float lat = ProtocolUtils.readFloat6(frame, offset);
        data.setLatitude(BigDecimal.valueOf(lat));
        offset += 6;

        // 入库时间
        data.setCreateTime(LocalDateTime.now());

        return data;
    }

    public static void main(String[] args) {
        String hex = "7E0046902509250560010000FFFE01AA37FA350813180025101614432007041000000000F96F00004442F76F00000000FF6F102CE841FE6F96646642FA6FEEB3E142FB6FF368E141C08E";
        byte[] frame = ProtocolUtils.hexStringToBytes(hex);
        GasMonitorData parsed = parseMessage(frame);

        System.out.println("========== 解析结果 ==========");
        System.out.println("帧类型: " + parsed.getFrameType());
        System.out.println("MAC地址: " + parsed.getMacAddress());
        System.out.println("短地址: " + parsed.getShortAddress());
        System.out.println("自动应答: " + parsed.getAutoReplyOption());
        System.out.println("序号: " + parsed.getSequenceNo());
        System.out.println("命令: " + parsed.getCommand());
        System.out.println("设备属性: " + parsed.getDeviceAttr());
        System.out.println("协议版本: " + parsed.getProtocolVersion());
        System.out.println("报警信息: " + parsed.getAlarmInfo()); // 此处输出已为大小端转换后的值
        System.out.println("时间戳: " + parsed.getReportTime());
        System.out.println("气体浓度: " + parsed.getGasConcentration() + " " + parsed.getGasUnit());
        System.out.println("气体类型: " + parsed.getGasType());
        System.out.println("电池电量: " + parsed.getBattery());
        System.out.println("信号强度: " + parsed.getSignalStrength());
        System.out.println("温度: " + parsed.getTemperature() + " °C");
        System.out.println("湿度: " + parsed.getHumidity() + " %");
        System.out.println("经度: " + parsed.getLongitude());
        System.out.println("纬度: " + parsed.getLatitude());
        System.out.println("入库时间: " + parsed.getCreateTime());
    }
}