package com.css.simulation.resource.scheduler.adapter.consumer;


import api.common.pojo.param.project.MultiSimulationProjectKafkaParam;
import api.common.util.JsonUtil;
import com.alibaba.fastjson.JSONObject;
import com.css.simulation.resource.scheduler.adapter.entity.ProjectStartMessageEntity;
import com.css.simulation.resource.scheduler.adapter.entity.ProjectStopMessageEntity;
import com.css.simulation.resource.scheduler.app.service.ProjectApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class ProjectConsumer {

    @Resource
    private ProjectApplicationService projectApplicationService;

    /**
     * @param projectStartMessageRecord 项目启动消息
     */
    @KafkaListener(groupId = "simulation-resource-scheduler", topics = "${custom.mq-start-project-topic}")
    public void acceptMessage(ConsumerRecord<String, String> projectStartMessageRecord) {
        log.info("消费者组 simulation-resource-scheduler 接收到项目开始消息：" + projectStartMessageRecord);
        projectApplicationService.runProject(JsonUtil.jsonToBean(projectStartMessageRecord.value(), ProjectStartMessageEntity.class));
    }


    /**
     * @param projectStopMessageRecord 项目终止消息
     */
    @KafkaListener(groupId = "simulation-resource-scheduler", topics = "${custom.mq-stop-project-topic}")
    public void stopProject(ConsumerRecord<String, String> projectStopMessageRecord) {
        log.info("消费者组 simulation-resource-scheduler 接收到的项目终止消息：" + projectStopMessageRecord);
        try {
            TimeUnit.SECONDS.sleep(10);
            projectApplicationService.stopProject(JsonUtil.jsonToBean(projectStopMessageRecord.value(), ProjectStopMessageEntity.class));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @KafkaListener(groupId = "simulation-resource-scheduler", topics = "${custom.mq-start-multi-project-topic}")
    public void acceptMultiMessage(ConsumerRecord<String, String> projectStartMessageRecord) {
        log.info("消费者组 simulation-resource-scheduler 接收到项目开始消息：" + projectStartMessageRecord);
        projectApplicationService.runMultiProject(JsonUtil.jsonToBean(projectStartMessageRecord.value(), MultiSimulationProjectKafkaParam.class));
    }


    /**
     */
    @KafkaListener(groupId = "simulation-resource-scheduler", topics = "${custom.mq-stop-multi-project-topic}")
    public void stopMultiProject(ConsumerRecord<String, String> projectStopMessageRecord) {
        log.info("消费者组 simulation-resource-scheduler 接收到的项目终止消息：" + projectStopMessageRecord);
        try {
            TimeUnit.SECONDS.sleep(10);
            projectApplicationService.stopMultiProject(JsonUtil.jsonToBean(projectStopMessageRecord.value(), MultiSimulationProjectKafkaParam.class));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


}
