package com.zksy.manhole.utils;

import com.zksy.manhole.domain.ManholeData;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class DataParser {
    private static Logger logger = LoggerFactory.getLogger(DataParser.class);

    public static ManholeData parseMessage(String msgString) {
        ManholeData result = new ManholeData();
        try {
            List<String> dataParts = new ArrayList<>();
            for (int i = 0; i < msgString.length(); i += 2) {
                int end = Math.min(i + 2, msgString.length());
                dataParts.add(msgString.substring(i, end));
            }
            getStringList(dataParts,7);
            String res1 = getStringList(dataParts,2);
            result.setVersionNumber(String.valueOf(Integer.parseInt(res1, 16)));
            String res2 = getStringList(dataParts,2);
            result.setBatteryLevel(String.valueOf(Integer.parseInt(res2, 16)));
            String res3 = getStringList(dataParts,2);
            result.setUploadTime(String.valueOf(Integer.parseInt(res3, 16)));
            String res4 = getStringList(dataParts,4);
            result.setTemperatureValue(String.valueOf(Integer.parseInt(res4, 16)));
            String res5 = getStringList(dataParts,2);
            result.setSignalStrength(String.valueOf(Integer.parseInt(res5, 16)));
            String res6 = getStringList(dataParts,2);
            result.setAlarmStatus(String.valueOf(Integer.parseInt(res6, 16)));
            String res7 = getStringList(dataParts,4);
            result.setAngleAlarmThreshold(String.valueOf(Integer.parseInt(res7, 16)));
            String res8 = getStringList(dataParts,4);
            result.setTiltAngle(String.valueOf(Integer.parseInt(res8, 16)));
            String res9 = getStringList(dataParts,2);
            result.setActivationStatus(String.valueOf(Integer.parseInt(res9, 16)));
            String res10 = getStringList(dataParts,2);
            result.setWaterInfiltrationAlarmStatus(String.valueOf(Integer.parseInt(res10, 16)));
            String res11 = getStringList(dataParts,2);
            result.setWaterLevelAlarmStatus(String.valueOf(Integer.parseInt(res11, 16)));
            String res12 = getStringList(dataParts,20);
            result.setSimCardNumber(hexToAscii(res12));
            String res13 = getStringList(dataParts,15);
            result.setImeiCardNumber(hexToAscii(res13));
            result.setOriginalData(msgString);
            result.setCreateTime(new Date());

        } catch (Exception e) {
            logger.error("解析消息时出错", e);
        }
        return result;
    }
    // 十六进制转 ASCII 码的方法
    public static String hexToAscii(String hex) {
        StringBuilder ascii = new StringBuilder();
        for (int i = 0; i < hex.length(); i += 2) {
            String str = hex.substring(i, i + 2);
            ascii.append((char) Integer.parseInt(str, 16));
        }
        return ascii.toString();
    }
    public static String shiftFromList(List<String> list) {
        if (list == null || list.isEmpty()) {
            return null; // 或者抛出异常，根据需求决定
        }
        return list.remove(0);
    }

    private static String getStringList(List<String> dataParts,Integer q) {
        StringBuilder res1 = new StringBuilder();
        for (int i = 0; i < q; i++) {
            res1.append(shiftFromList(dataParts));
        }
        return res1.toString();
    }
}