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

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import rk.netDevice.sdk.p2.RealTimeData;
import rk.netDevice.sdk.p2.StoreData;
import vip.xiaonuo.coldchain.core.cache.monitordevice.MonitorDeviceCache;
import vip.xiaonuo.coldchain.core.service.dataprocess.dataclean.MonitorDataProcessor;
import vip.xiaonuo.coldchain.core.service.dataprocess.dataclean.impl.AbsRenkeMonitorDataProcessor;
import vip.xiaonuo.coldchain.core.service.dataprocess.handler.AbstractColdChainDataHandler;
import vip.xiaonuo.coldchain.core.service.dataprocess.model.RenKeColdChainMessageData;

import java.util.Map;

/**
 * @author jackzhou
 * @version 1.0
 * @project jfcloud-coldchain
 * @description
 * @date 2024/11/12 13:03:45
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RenKeColdChainDataHandler extends AbstractColdChainDataHandler<RenKeColdChainMessageData> {
    private final Map<String, MonitorDataProcessor<?>> monitorDataProcessorMap;
    private final MonitorDeviceCache monitorDeviceCache;

    @Override
    public boolean handleRealTimeData(RenKeColdChainMessageData renKeColdChainMessageData) {
        RealTimeData data = renKeColdChainMessageData.getRealTimeData();
        renKeColdChainMessageData.setDeviceId(data.getDeviceId());
        return handleData(data.getDeviceId(), renKeColdChainMessageData);
    }

    @Override
    public boolean handleStoreData(RenKeColdChainMessageData renKeColdChainMessageData) {
        StoreData data = renKeColdChainMessageData.getStoreData();
        renKeColdChainMessageData.setDeviceId(data.getDeviceId());
        return handleData(data.getDeviceId(), renKeColdChainMessageData);
    }

    private boolean handleData(Integer deviceId, RenKeColdChainMessageData renKeColdChainMessageData) {
        // 转换 deviceId 为字符串
        final String deviceIdStr = String.valueOf(deviceId);
        // 从缓存中获取设备模型名称
        final String modelName = monitorDeviceCache.getDeviceModel(deviceIdStr);
        // 默认处理器
        AbsRenkeMonitorDataProcessor monitorDataProcessor =
                (AbsRenkeMonitorDataProcessor) monitorDataProcessorMap.get(MonitorDataProcessor.RS_WS_DEFAULT);
        // 如果模型名称非空，则尝试获取对应处理器
        if (StrUtil.isNotBlank(modelName)) {
            renKeColdChainMessageData.setModelName(modelName);
            AbsRenkeMonitorDataProcessor specificProcessor =
                    (AbsRenkeMonitorDataProcessor) monitorDataProcessorMap.get(modelName);
            if (specificProcessor != null) {
                monitorDataProcessor = specificProcessor;
            } else {
                log.warn("No processor found for modelName: {}, using default processor.", modelName);
            }
        } else {
            log.warn("Model name not found for deviceId: {}, using default processor.", deviceIdStr);
        }
        // 如果处理器存在，则处理数据，否则记录警告
        if (monitorDataProcessor != null) {
//            log.info("Using processor [{}] to process data for deviceId: {}",monitorDataProcessor.getModelName(), deviceIdStr);
            monitorDataProcessor.processData(renKeColdChainMessageData);
            return Boolean.TRUE;
        } else {
            log.error("No valid data processor found for deviceId: {}", deviceIdStr);
            return Boolean.FALSE;
        }
    }



    @Override
    public void login(RenKeColdChainMessageData data) {
        processMonitorData(data, "login");
    }

    @Override
    public void heartbeat(RenKeColdChainMessageData data) {
        processMonitorData(data, "heartbeat");
    }

    // 公共的处理方法
    private void processMonitorData(RenKeColdChainMessageData data, String action) {
        if (!monitorDataProcessorMap.isEmpty()) {
            MonitorDataProcessor<?> monitorDataProcessor1 = monitorDataProcessorMap.get(monitorDataProcessorMap.keySet().iterator().next());
            if (monitorDataProcessor1 instanceof AbsRenkeMonitorDataProcessor monitorDataProcessor) {
                // 根据 action 调用相应的方法
                switch (action) {
                    case "login":
                        monitorDataProcessor.login(data);
                        break;
                    case "heartbeat":
                        monitorDataProcessor.heartbeat(data);
                        break;
                    default:
                        log.error("Unsupported action: {}", action);
                        break;
                }
            }
        } else {
            log.warn("Monitor data processor map is empty.");
        }
    }

}
