package com.friday.distributor.statemachine;

import com.friday.distributor.bean.Task;
import com.friday.distributor.common.ExecDataService;
import com.friday.distributor.common.PlatformInteractionService;
import com.friday.distributor.constant.Events.TaskEvent;
import com.friday.distributor.constant.States.TaskState;
import com.friday.distributor.exception.DistributorRuntimeException;
import com.friday.distributor.common.ExecutorMethodInvokeHelper;
import com.friday.distributor.queue.TaskPersistHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.stereotype.Component;

import static com.friday.distributor.constant.States.TaskState.EXECUTED;
import static com.friday.distributor.constant.States.TaskState.EXECUTING;
import static com.friday.operation.api.distributor.constant.TaskStateChangeEnum.EXECUTING_EXECUTED;
import static com.friday.operation.api.distributor.constant.TaskStateChangeEnum.UNEXECUTED_EXECUTING;

/**
 * @Author YaoYao
 * @Date Created In 13:46 2023/2/27
 * @Description 任务状态机监听器
 * @Modified By
 */
@Component
@WithStateMachine(id = "TaieDistributorTaskMachine")
@Slf4j
public class TaskEventListener extends StateMachineListenerAdapter<TaskState, TaskEvent> {
    @Autowired
    private TaskPersistHelper taskPersistHelper;
    @Autowired
    private ExecutorMethodInvokeHelper invoker;
    @Autowired
    private ExecDataService execDataService;
    @Autowired
    private PlatformInteractionService platformInteractionService;

    @OnTransition(source = "UNEXECUTED", target = "EXECUTING")
    public void startTask(Message<TaskEvent> message) throws DistributorRuntimeException {
        log.info("开始启动任务...");
        Task taskDetail = (Task) message.getHeaders().get("task");
        if(taskDetail == null){
            log.error("目标任务缺失");
            throw new DistributorRuntimeException("状态机更新状态异常，缺失上下文");
        }
        taskDetail.setState(EXECUTING.name());
        taskPersistHelper.insertOrUpdateTaskRecord(taskDetail);
        //发运行通知
        invoker.onTaskStateChange(taskDetail, UNEXECUTED_EXECUTING);
    }

    @OnTransition(source = "EXECUTING", target = "EXECUTING")
    public void updateTask(Message<TaskEvent> message) throws DistributorRuntimeException {
        log.info("开始更新任务...");
        Task taskDetail = (Task) message.getHeaders().get("task");
        if(taskDetail == null){
            log.error("目标任务缺失");
            throw new DistributorRuntimeException("状态机更新状态异常，缺失上下文");
        }
//        taskQueueManager.updateTask(taskDetail);
        taskDetail.setState(EXECUTING.name());
        taskPersistHelper.insertOrUpdateTaskRecord(taskDetail);
    }

    @OnTransition(source = "EXECUTING", target = "EXECUTED")
    public void finishTask(Message<TaskEvent> message) throws DistributorRuntimeException {
        log.info("开始停止任务...");
        Task taskDetail = (Task) message.getHeaders().get("task");
        if(taskDetail == null){
            log.error("目标任务缺失");
            throw new DistributorRuntimeException("状态机更新状态异常，缺失上下文");
        }
        taskDetail.setState(EXECUTED.name());
        taskPersistHelper.insertOrUpdateTaskRecord(taskDetail);
        invoker.onTaskStateChange(taskDetail, EXECUTING_EXECUTED);
//        platformInteractionService.syncPlatformTaskState(taskDetail.getTaskId());
        execDataService.sendKafkaWhenTaskEnded(taskDetail.getTaskId());
    }

}
