package com.ruoyi.dj.mqtt.handler;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.dj.mqtt.domain.*;
import com.ruoyi.dj.websocket.service.MessagingService;
import com.ruoyi.system.domain.Drone;
import com.ruoyi.system.domain.VideoInfo;
import com.ruoyi.system.service.IDroneService;
import com.ruoyi.system.service.IVideoInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class OSDHandler {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private MessagingService messagingService;
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Resource
    private IDroneService droneService;
    @Resource
    private IVideoInfoService videoInfoService;

    public void handle(String matchedPart, String message) {

        System.out.println("收到消息:"+message);
        Object object = null;
        try {
            object = convertJsonToOSDMessage(matchedPart, message);
            if (object instanceof OSDMessage3) {
                // 将 object 转化为 OSDMessage3
                OSDMessage3 osdMessage3 = (OSDMessage3) object;
                String deviceSn = osdMessage3.getData().getSub_device().getDevice_sn();
                if (deviceSn != null) {
                    Drone drone = droneService.selectDroneBySn(deviceSn);
                    if (drone != null) {
                        // 设置网关在线状态【实际上是飞机的在线状态】
                        redisTemplate.opsForValue().set(matchedPart, String.valueOf(osdMessage3.getData().getSub_device().getDevice_online_status()));
                        // 更新 drone 的 isOnline
                        drone.setIsOnline(osdMessage3.getData().getSub_device().getDevice_online_status() == 0 ? "离线" : "在线");
                        droneService.updateDrone(drone);
                    } else {
                        // 新增设备数据
                        drone = new Drone();
                        drone.setSn(deviceSn);
                        drone.setGateway(matchedPart);
                        drone.setIsOnline(osdMessage3.getData().getSub_device().getDevice_online_status() == 0 ? "离线" : "在线");
                        droneService.insertDrone(drone);
                    }
                }
            }
        } catch (Exception e) {
            log.error("解析 OSD 消息失败: " + e.toString());
            throw new RuntimeException(e);
        }
    }

    public Object convertJsonToOSDMessage(String gateway, String json) throws Exception {
        JsonNode rootNode = objectMapper.readTree(json);

        if (rootNode.has("data") && rootNode.get("data").has("job_number")) {
            // 大疆文档中的 thing/product/{dock_sn}/osd 的第一个 OSD 消息
            messagingService.sendMessage("osd1", gateway, json);
            return objectMapper.readValue(json, OSDMessage1.class);
        } else if (rootNode.has("data") && rootNode.get("data").has("drc_state")) {
            messagingService.sendMessage("osd2", gateway, json);
            // 大疆文档中的 thing/product/{dock_sn}/osd 的第二个 OSD 消息
            return objectMapper.readValue(json, OSDMessage2.class);
        } else if (rootNode.has("data") && rootNode.get("data").has("network_state")) {
            messagingService.sendMessage("osd3", gateway, json);
            // 大疆文档中的 thing/product/{dock_sn}/osd 的第三个 OSD 消息
            return objectMapper.readValue(json, OSDMessage3.class);
        } else {
            // 大疆文档中的 thing/product/{dock_sn}/osd 的第四个 OSD 消息
            VideoInfo videoInfo = videoInfoService.selectGatewayByCondition(gateway);
            if (videoInfo != null) {
                messagingService.sendMessage("osd4", videoInfo.getGateway(), json);
            }
            return objectMapper.readValue(json, OSDMessage4.class);
        }
    }
}

