package com.keystar.plane.inspection.mqtt;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.keystar.mqtt.annotation.MqttHandlerType;
import com.keystar.mqtt.annotation.MqttSubScribe;
import com.keystar.mqtt.handler.AbstractMqttHandler;
import com.keystar.plane.inspection.bo.RecognizeMessageBo;
import com.keystar.plane.inspection.entity.PhotoResultEntity;
import com.keystar.plane.inspection.entity.PlanePhotoEntity;
import com.keystar.plane.inspection.service.PhotoResultService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@MqttHandlerType(
        {
                @MqttSubScribe(topic = "LightSpot_recognize_result", qos = 0)
        }
)
@Slf4j
public class RecognizeMqttHandler implements AbstractMqttHandler {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    @Autowired
    PhotoResultService photoResultService;

    @Override
    public void handle(MqttMessage message) throws IOException {

        String messageContent = new String(message.getPayload());
        log.info("======接收到的识别结果mq: " + messageContent);
        JSONObject messageObject = JSONObject.parseObject(messageContent);
        JSONArray dataList = messageObject.getJSONArray("data");
        // 将dataList中的结果遍历
        for( Object data : dataList){
            JSONObject dataObject = JSONObject.parseObject(data.toString());
            // 图片id
            String id = dataObject.getString("id");
//            JSONObject id1 = dataObject.getJSONObject("id");
//            String id = id1.toJSONString();
//            String id = dataObject.getJSONObject("id").toJSONString();
            // 检测功能(0热斑检测，1掉串检测)
            List<String> funcIndex = dataObject.getJSONArray("funcIndex").toJavaList(String.class);
            // 可见光图片
            String visiblePath = dataObject.getString("visibleImagePath");
            // 红外图片
            String infraredPath = dataObject.getString("infraredImagePath");
            // 识别结果
            JSONArray detectResult = dataObject.getJSONArray("detectResult");
            List<RecognizeMessageBo> recognizeMessageBoList = detectResult.toJavaList(RecognizeMessageBo.class);

            // 将识别结果入库
            for (RecognizeMessageBo recognizeMessageBo : recognizeMessageBoList){
                PhotoResultEntity photoResultEntity = new PhotoResultEntity();
                photoResultEntity.setId(IdUtil.getSnowflake().nextId());
                photoResultEntity.setPhotoId(Long.parseLong(id));
                photoResultEntity.setPath(visiblePath);
                photoResultEntity.setArrayIndex(recognizeMessageBo.getArrayIndex());
                photoResultEntity.setRowIndex(recognizeMessageBo.getRowIndex());
                photoResultEntity.setColumnIndex(recognizeMessageBo.getColumnIndex());
                photoResultEntity.setType(recognizeMessageBo.getResult().getType());
                String joined = String.join(",", recognizeMessageBo.getResult().getCoordinates());
                photoResultEntity.setCoordinates(joined);
                log.info("======识别结果入库：" + photoResultEntity);
                photoResultService.save(photoResultEntity);
            }

        }



    }
}
