package cn.stylefeng.workflow.instance.core.actions;


import cn.stylefeng.workflow.api.entity.design.Node;
import cn.stylefeng.workflow.api.entity.instance.RuntimeNode;
import cn.stylefeng.workflow.api.enums.NodeStatusEnum;
import cn.stylefeng.workflow.api.enums.NodeTypeEnum;
import cn.stylefeng.workflow.api.pojo.InstanceContext;
import cn.stylefeng.workflow.instance.api.node.NodeAction;
import cn.stylefeng.workflow.instance.api.pojo.NodeActionResult;
import cn.stylefeng.workflow.log.api.WorkflowLogApi;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MessageListenerNodeAction extends NodeAction {

    @Resource
    private WorkflowLogApi workflowLogApi;

    @Override
    public NodeTypeEnum getNodeType() {
        return NodeTypeEnum.MESSAGE_LISTENER;
    }

    @Override
    public NodeActionResult doAction(Node nodeInfo, InstanceContext instanceContext) {

        RuntimeNode runtimeNode = super.createRuntimeNode(nodeInfo, instanceContext);

        runtimeNode.setNodeStatus(NodeStatusEnum.RUNNING.getStatus());
        runtimeNode = super.saveRuntimeNode(runtimeNode);

        workflowLogApi.logSuccess(instanceContext, nodeInfo.getNodeId(), "消息监听节点进行中：创建运行时节点");

        return new NodeActionResult(runtimeNode, null, instanceContext.getInstanceVariable());
    }

}
