package cn.stylefeng.workflow.instance.modular.task.strategy.emptyprocess;

import cn.hutool.core.collection.ListUtil;
import cn.stylefeng.workflow.api.entity.design.Node;
import cn.stylefeng.workflow.api.entity.instance.RuntimeNode;
import cn.stylefeng.workflow.api.enums.NodeEventTypeEnum;
import cn.stylefeng.workflow.api.enums.strategy.EmptyApproverStrategyEnum;
import cn.stylefeng.workflow.api.pojo.InstanceContext;
import cn.stylefeng.workflow.event.api.EventPublishApi;
import cn.stylefeng.workflow.instance.api.WorkflowControlApi;
import cn.stylefeng.workflow.instance.api.pojo.request.WorkflowFinishNodeRequest;
import cn.stylefeng.workflow.instance.modular.task.strategy.emptyprocess.base.EmptyApproverRule;
import cn.stylefeng.workflow.log.api.WorkflowLogApi;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class EmptyApproverJumpRule implements EmptyApproverRule {

    @Resource
    private EventPublishApi eventPublishApi;

    @Resource
    private WorkflowControlApi workflowControlApi;

    @Resource
    private WorkflowLogApi workflowLogApi;

    @Override
    public EmptyApproverStrategyEnum getStrategyType() {
        return EmptyApproverStrategyEnum.JUMP;
    }

    @Override
    public void processEmptyApprover(Node currentNode, InstanceContext instanceContext, RuntimeNode runtimeNode) {

        eventPublishApi.publishNodeEvent(NodeEventTypeEnum.APPROVAL_PASSED, currentNode.getNodeId(), instanceContext);

        WorkflowFinishNodeRequest workflowFinishNodeRequest = new WorkflowFinishNodeRequest(instanceContext.getRuntimeInstance().getInstanceId(), ListUtil.toList(runtimeNode.getRuNodeId()));
        workflowControlApi.finishNode(workflowFinishNodeRequest);

        workflowLogApi.logSuccess(instanceContext, currentNode.getNodeId(), "执行审批策略-审批人为空时，自动跳过");
    }

}
