package cn.axzo.workflow.server.controller.delegate;

import cn.axzo.workflow.common.enums.ApproverSpecifyEnum;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

import java.util.List;

import static cn.axzo.workflow.common.constant.BpmnConstants.INTERNAL_SPECIFY_NEXT_APPROVER;

/**
 * 上级节点指定审批人
 *
 * @author wangli
 * @since 2023/11/29 17:43
 */
@Slf4j
@Component
@AllArgsConstructor
public class PreNodeSpecifiedTaskAssigneeSelector extends AbstractBpmnTaskAssigneeSelector {

    private final RuntimeService runtimeService;

    @Override
    public String getType() {
        return ApproverSpecifyEnum.preNodeSpecified.getType();
    }

    @Override
    public boolean support(String param) {
        return getType().equals(param);
    }

    @Override
    public List<BpmnTaskDelegateAssigner> select(FlowElement flowElement, DelegateExecution execution,
                                                 Boolean throwException) {
        BpmnTaskDelegateAssigner assigner = runtimeService.getVariable(execution.getProcessInstanceId(),
                INTERNAL_SPECIFY_NEXT_APPROVER, BpmnTaskDelegateAssigner.class);
        return Lists.newArrayList(assigner);
    }
}
