package vip.xiaonuo.coldchain.modular.qp.listener;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.coldchain.modular.monitordevice.service.MonitorDeviceService;
import vip.xiaonuo.coldchain.modular.monitortargetregion.entity.MonitorTargetRegion;
import vip.xiaonuo.coldchain.modular.monitortargetregion.enums.MonitorTargetRegionEvent;
import vip.xiaonuo.coldchain.modular.qp.config.QpApi;
import vip.xiaonuo.coldchain.modular.qp.vo.QpAlertConfigVo;
import vip.xiaonuo.common.listener.CommonDataChangeListener;
import vip.xiaonuo.dev.modular.config.service.DevConfigService;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Component
@RequiredArgsConstructor
public class QpListener implements CommonDataChangeListener {

    private final QpApi qpApi;
    private final DevConfigService devConfigService;
    private final MonitorDeviceService monitorDeviceService;

    @Override
    public void doAddWithDataIdList(String dataType, List<String> dataIdList) {

    }

    @Override
    public void doAddWithDataList(String dataType, JSONArray jsonArray) {

    }

    @Override
    public void doUpdateWithDataIdList(String dataType, List<String> dataIdList) {

    }

    @Override
    public void doUpdateWithDataList(String dataType, JSONArray jsonArray) {
        //监测点配置更新
        if(!dataType.equals(MonitorTargetRegionEvent.MONITOR_TARGET_REGION_EDIT_EVENT)) {
            return;
        }

        //获取青萍的设备型号
        String qingpingType = devConfigService.getValueByKey("qingping_type");
        Set<String> qpDeviceType = Stream.of(qingpingType.split(",")).filter(StrUtil::isNotBlank).collect(Collectors.toSet());

        List<MonitorTargetRegion> monitorTargetRegionList = jsonArray.toList(MonitorTargetRegion.class)
                .stream()
                .filter(x -> qpDeviceType.contains(x.getModelName()))
                .toList();
        if (monitorTargetRegionList.isEmpty()) {
            return;
        }

        //监测点配置更新中的青萍设备
        Map<String, List<QpAlertConfigVo>> qpAlertConfigMap = new LinkedHashMap<>();
        for (MonitorTargetRegion region : monitorTargetRegionList) {
            String mac = region.getSensorCode();
            if (!qpAlertConfigMap.containsKey(mac)) {
                qpAlertConfigMap.put(mac, new ArrayList<>());
            }
            //温度下限
            if (region.getTemperatureDown() != null) {
                QpAlertConfigVo configVo = new QpAlertConfigVo()
                        .setMetric_name("temperature")
                        .setOperator("lt")
                        .setThreshold(region.getTemperatureDown().intValue());
                qpAlertConfigMap.get(mac).add(configVo);
            }
            //温度上限
            if (region.getTemperatureUp() != null) {
                QpAlertConfigVo configVo = new QpAlertConfigVo()
                        .setMetric_name("temperature")
                        .setOperator("gt")
                        .setThreshold(region.getTemperatureUp().intValue());
                qpAlertConfigMap.get(mac).add(configVo);
            }
            //湿度下限
            if (region.getHumidityDown() != null) {
                QpAlertConfigVo configVo = new QpAlertConfigVo()
                        .setMetric_name("humidity")
                        .setOperator("lt")
                        .setThreshold(region.getHumidityDown().intValue());
                qpAlertConfigMap.get(mac).add(configVo);
            }
            //湿度上限
            if (region.getHumidityUp() != null) {
                QpAlertConfigVo configVo = new QpAlertConfigVo()
                        .setMetric_name("humidity")
                        .setOperator("gt")
                        .setThreshold(region.getHumidityUp().intValue());
                qpAlertConfigMap.get(mac).add(configVo);
            }
            //Co2下限
            if (region.getCo2Down() != null) {
                QpAlertConfigVo configVo = new QpAlertConfigVo()
                        .setMetric_name("co2")
                        .setOperator("lt")
                        .setThreshold(region.getCo2Down().intValue());
                qpAlertConfigMap.get(mac).add(configVo);
            }
            //Co2上限
            if (region.getCo2Up() != null) {
                QpAlertConfigVo configVo = new QpAlertConfigVo()
                        .setMetric_name("co2")
                        .setOperator("gt")
                        .setThreshold(region.getCo2Up().intValue());
                qpAlertConfigMap.get(mac).add(configVo);
            }
            //电量下限
            if (region.getBatteryDown() != null) {
                QpAlertConfigVo configVo = new QpAlertConfigVo()
                        .setMetric_name("battery")
                        .setOperator("lt")
                        .setThreshold(region.getBatteryDown().intValue());
                qpAlertConfigMap.get(mac).add(configVo);
            }
        }

        qpAlertConfigMap.forEach(qpApi::saveConfig);
    }

    @Override
    public void doDeleteWithDataIdList(String dataType, List<String> dataIdList) {

    }
}
