package com.fdkankan.scene.listener;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.BuildStatus;
import com.fdkankan.common.constant.ConstantFilePath;
import com.fdkankan.common.constant.ExpiredStatus;
import com.fdkankan.common.constant.SceneFrom;
import com.fdkankan.common.constant.SceneResolution;
import com.fdkankan.common.constant.SceneSource;
import com.fdkankan.common.constant.SceneStatus;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.constant.UploadFilePath;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.common.util.ComputerUtil;
import com.fdkankan.common.util.MatrixToImageWriterUtil;
import com.fdkankan.common.util.SceneUtil;
import com.fdkankan.fyun.oss.UploadToOssUtil;
import com.fdkankan.mq.message.BuildSceneMqMessage;
import com.fdkankan.mq.message.BuildSceneResultMqMessage;
import com.fdkankan.mq.util.RocketMQProducer;
import com.fdkankan.platform.api.feign.PlatformGoodsClient;
import com.fdkankan.platform.api.feign.PlatformUserClient;
import com.fdkankan.platform.api.vo.CameraDetail;
import com.fdkankan.platform.api.vo.UserIncrement;
import com.fdkankan.scene.entity.SceneEditControls;
import com.fdkankan.scene.entity.SceneEditInfo;
import com.fdkankan.scene.entity.SceneFileBuild;
import com.fdkankan.scene.entity.ScenePro;
import com.fdkankan.scene.entity.SceneProExt;
import com.fdkankan.scene.service.IBuildScenePreService;
import com.fdkankan.scene.service.ISceneEditControlsService;
import com.fdkankan.scene.service.ISceneEditInfoService;
import com.fdkankan.scene.service.ISceneFileBuildService;
import com.fdkankan.scene.service.ISceneProExtService;
import com.fdkankan.scene.service.ISceneProService;
import com.fdkankan.scene.service.ISceneService;
import com.fdkankan.scene.service.impl.BuildScenePreServiceImpl;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * <p>
        场景计算前准备工作，如下载资源等
 * </p>
 * @author dengsixing
 * @date 2022/2/23
 **/
@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "${rocketmq.consumer.build-scene-pre-group}",
        topic = "${rocketmq.build-scene.topicName.topic-modeling-a-pre}",
        messageModel = MessageModel.CLUSTERING,//负载均衡模式
        consumeThreadMax = 10,
        consumeTimeout = 172801000L
)
public class BuildScenePreMQListener implements RocketMQListener<String> {


    @Value("${main.url}")
    private String mainUrl;

    @Value("${scene.pro.url}")
    private String sceneProUrl;

    @Value("${scene.pro.new.url}")
    private String sceneProNewUrl;

    @Autowired
    ISceneService sceneService;
    @Autowired
    ISceneFileBuildService sceneFileBuildService;
    @Autowired
    ISceneProService sceneProService;
    @Autowired
    ISceneProExtService sceneProExtService;
    @Autowired
    PlatformGoodsClient platformGoodsClient;
    @Autowired
    PlatformUserClient platformUserClient;
    @Autowired
    ISceneEditInfoService sceneEditInfoService;
    @Autowired
    ISceneEditControlsService sceneEditControlsService;
    @Autowired
    UploadToOssUtil uploadToOssUtil;
    @Autowired
    IBuildScenePreService buildScenePreService;
    @Autowired
    private RocketMQProducer producer;
    @Value("${rocketmq.build-scene.topicName.topic-modeling-a}")
    private String topicName;


    @Override
    public void onMessage(String message) {

        try {
            BuildSceneMqMessage buildSceneMqMessage = JSONObject.parseObject(message, BuildSceneMqMessage.class);
            log.info("开始处理消息，消费者组：{}，主题名：{}， 消息内容：{}", "build-scene-pre-group", "topic-modeling-a-pre", message);
            process(buildSceneMqMessage);
        }catch (Exception e){

        }

    }

    private void process(BuildSceneMqMessage message){
        try {
            //根据相机类型，组装资源路径
            String path = SceneUtil.getPath(message.getPath(), message.getCameraName(),
                message.getFileId(), Integer.parseInt(message.getCameraType()), message.getUnicode());

            //下载资源到本地
            this.buildScenePreService.downLoadSource(message, path);

            //发送mq，就进行计算
            producer.syncSend(topicName, message);

            log.info("场景计算前置处理完成，场景码：{}", message.getSceneNum());

        }catch (Exception e){
            log.error("场景计算前置处理出错", e);
            //发送钉钉
            String serverPath = message.getPath().substring(0,message.getPath().lastIndexOf("/")+1).concat(message.getPrefix());
//            buildScenePreService.handFail("前置处理失败", serverPath, message.getSceneNum(), BuildScenePreServiceImpl.hostName, null);
        }
    }

}
