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

import cn.axzo.maokai.api.client.OrganizationalNodeUserApi;
import cn.axzo.maokai.api.vo.request.FlowTaskAssignerReq;
import cn.axzo.maokai.api.vo.request.FlowTaskAssignerReq.IdentityPair;
import cn.axzo.maokai.api.vo.response.FlowTaskAssigner;
import cn.axzo.workflow.common.enums.ApproverSpecifyEnum;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;
import cn.axzo.workflow.core.deletage.approverscope.ApproverScopeDTO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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

/**
 * 基于"发起人主管"查询审批人
 *
 * @author wangli
 * @since 2023/11/16 11:41
 */
@Slf4j
@Component
public class BasedInitiatorLeaderTaskAssigneeSelector extends AbstractBpmnTaskAssigneeSelector {

    @Autowired
    private OrganizationalNodeUserApi organizationalNodeUserApi;

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

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

    @Override
    public List<BpmnTaskDelegateAssigner> select(FlowElement flowElement, DelegateExecution execution,
                                                 Boolean throwException) {
        return super.select(flowElement, execution, throwException);
    }

    @Override
    protected List<BpmnTaskDelegateAssigner> invokeService(FlowElement flowElement, DelegateExecution execution,
                                                           ApproverScopeDTO scopeDto) {
        // 获取发起人
        BpmnTaskDelegateAssigner initiator =
                BpmnTaskDelegateAssigner.toObjectCompatible(execution.getVariables().get(INTERNAL_INITIATOR));
        if (Objects.isNull(initiator)) {
            return super.invokeService(flowElement, execution, scopeDto);
        }
        FlowTaskAssignerReq req = FlowTaskAssignerReq.builder()
                .orgScopes(ListUtils.emptyIfNull(scopeDto.getOrgScopes()).stream()
                        .map(e -> BeanUtil.copyProperties(e, FlowTaskAssignerReq.OrgScope.class))
                        .collect(Collectors.toList()))
                .sponsor(IdentityPair.builder()
                        .identityId(Long.valueOf(initiator.getAssignee()))
                        .identityType(Integer.valueOf(initiator.getAssigneeType())).build())
                .build();

        List<FlowTaskAssigner> flowTaskAssigners =
                parseApiResult(() -> organizationalNodeUserApi.listFlowTaskAssigner(req),
                        "审批节点: " + flowElement.getId() + ", 通过发起人主管查询审批人",
                        "cn.axzo.maokai.api.client.OrganizationalNodeUserApi.listFlowTaskAssigner", req);

        if (CollUtil.isEmpty(flowTaskAssigners)) {
            return super.invokeService(flowElement, execution, scopeDto);
        }
        return BeanUtil.copyToList(flowTaskAssigners, BpmnTaskDelegateAssigner.class);
    }
}
