package vip.xiaonuo.coldchain.core.service.dataprocess.dataclean.impl;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import rk.netDevice.sdk.p2.NodeData;
import rk.netDevice.sdk.p2.RealTimeData;
import vip.xiaonuo.coldchain.core.bean.influxdb.SensorData;
import vip.xiaonuo.coldchain.core.service.dataprocess.dataclean.MonitorDataProcessor;
import vip.xiaonuo.coldchain.core.service.dataprocess.model.PowerEnum;
import vip.xiaonuo.coldchain.modular.monitordevice.enums.DeviceModelEnum;

import java.util.List;

/**
 * @author jackzhou
 * @version 1.0
 * @project jfcloud-coldchain
 * @description
 * @date 2024/11/20 21:25:30
 */
@Slf4j
@Component(MonitorDataProcessor.RS_WS_DEFAULT)
public class DefaultWifiLowTempRecorderProcessor extends AbsRenkeMonitorDataProcessor {
    public DefaultWifiLowTempRecorderProcessor(ApplicationEventPublisher applicationEventPublisher) {
        super(applicationEventPublisher);
    }

    @Override
    List<SensorData> transRealTimeData2SensorDatas(@NotNull RealTimeData data) {
        List<NodeData> nodeList = data.getNodeList();
        List<SensorData> resultList = Lists.newArrayList();

        if (nodeList == null || nodeList.isEmpty()) {
            log.warn("节点数据为空，无法转换为传感器数据");
            return resultList;
        }

        final int numNodes = nodeList.size();

        // 如果节点数少于3个，记录警告并设置默认值
        if (numNodes <= 2) {
            log.warn("节点数量不足，实际节点数为 {}，需至少 3 个", numNodes);
            for (NodeData nodeData : nodeList) {
                SensorData sensorData = createSensorData(data, nodeData, null, null);
                resultList.add(sensorData);
            }
            return resultList;
        }

        // 提取倒数第二路和最后一路的特殊数据
        final float battery = nodeList.get(numNodes - 2).getTem();
        final String plugInStatus = nodeList.get(numNodes - 1).getTem() < 1 ? PowerEnum.AC.getCode() : PowerEnum.DC.getCode();

        // 处理前面所有节点的数据
        for (int i = 0; i < numNodes - 2; i++) {
            NodeData nodeData = nodeList.get(i);
            SensorData sensorData = createSensorData(data, nodeData, battery, plugInStatus);
            resultList.add(sensorData);
        }

        return resultList;
    }

    /**
     * 创建单个 SensorData 对象
     */
    private SensorData createSensorData(RealTimeData data, NodeData nodeData, Float battery, String plugInStatus) {
        SensorData sensorData = defaultSensorData(data);
        sensorData.setTemperature(floatValue(nodeData.getTem()));
        sensorData.setHumidity(floatValue(nodeData.getHum()));
        sensorData.setRoads(nodeData.getNodeId());
        sensorData.setBattery(battery);
        sensorData.setPlugInStatus(plugInStatus);
        return sensorData;
    }


    @Override
    DeviceModelEnum deviceModel() {
        return DeviceModelEnum.WIFI_MULTI_PROBE_TEMP_HUMIDITY_RECORDER_1;
    }
}
