package com.ruoyi.dj.mqtt.handler;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.dj.mqtt.common.CommonResponse;
import com.ruoyi.dj.mqtt.domain.VideoRoot;
import com.ruoyi.dj.mqtt.utils.ParseMethodUtil;
import com.ruoyi.system.domain.VideoInfo;
import com.ruoyi.system.service.IVideoInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class StateHandler {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Resource
    private IVideoInfoService videoInfoService;

    public void handle(String matchedPart, String message) {
        try {
            JsonNode rootNode = objectMapper.readTree(message);
            JsonNode dataNode = rootNode.path("data");
            String gateway = rootNode.path("gateway").asText();

            if (dataNode.has("live_capacity")) {
                System.out.println(message);
                log.info("是直播设备的相关属性状态、开始处理");
                try {
                    VideoRoot videoRoot = objectMapper.readValue(message, VideoRoot.class);
                    for (VideoRoot.DataContent.LiveCapacity.Device device : videoRoot.getData().getLive_capacity().getDevice_list()) {
                        String sn = device.getSn();
                        for (VideoRoot.DataContent.LiveCapacity.Device.Camera camera : device.getCamera_list()) {
                            String cameraIndex = camera.getCamera_index();
                            for (VideoRoot.DataContent.LiveCapacity.Device.Camera.Video video : camera.getVideo_list()) {
                                String videoIndex = video.getVideo_index();
                                if (gateway != null && !gateway.isEmpty() && sn != null && !sn.isEmpty() && cameraIndex != null && !cameraIndex.isEmpty() && videoIndex != null && !videoIndex.isEmpty()) {
                                    VideoInfo videoInfo = new VideoInfo();
                                    videoInfo.setGateway(gateway);
                                    videoInfo.setSn(sn);
                                    videoInfo.setVideoIndex(videoIndex);
                                    videoInfo.setType(sn.equals(gateway) ? "机场" : "飞机");

                                    // 查询数据库中是否存在这条记录
                                    VideoInfo existingVideoInfo = videoInfoService.findVideoInfo(videoInfo);
                                    if (existingVideoInfo == null) {
                                        // 数据不存在，新增
                                        videoInfo.setCameraIndex(cameraIndex);
                                        videoInfoService.insertVideoInfo(videoInfo);
                                        log.info("新增数据: " + videoInfo);
                                    } else {
                                        // 数据已存在
                                        String cameraIndexList = existingVideoInfo.getCameraIndex();
                                        List<String> cameraIndexSet = Arrays.stream(cameraIndexList.split(",")).collect(Collectors.toList());

                                        if (!cameraIndexSet.contains(cameraIndex)) {
                                            // 如果 cameraIndex 不存在于现有记录中，追加并更新
                                            existingVideoInfo.setCameraIndex(cameraIndexList + "," + cameraIndex);
                                            videoInfoService.updateVideoInfo(existingVideoInfo);
                                            log.info("更新后的数据: " + existingVideoInfo);
                                        } else {
                                            // cameraIndex 已存在，无需处理
                                            log.info("cameraIndex 已存在，无需更新: " + existingVideoInfo);
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                log.info("不是直播设备的相关属性状态、不处理");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
