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

import cn.axzo.karma.client.feign.FlowSupportApi;
import cn.axzo.karma.client.model.request.ListFlowTaskAssignerReq;
import cn.axzo.karma.client.model.response.FlowTaskAssignerResp;
import cn.axzo.workflow.common.enums.ApproverEmptyHandleTypeEnum;
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 org.springframework.util.CollectionUtils;

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

/**
 * 基于"转交给管理员"查询审批人
 *
 * @author wangli
 * @since 2023/11/16 11:44
 */
@Slf4j
@Component
public class TransferToAdminTaskAssigneeSelector extends AbstractBpmnTaskAssigneeSelector {

    @Autowired
    private FlowSupportApi flowSupportApi;

    @Override
    public String getType() {
        return ApproverEmptyHandleTypeEnum.transferToAdmin.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) {
        ListFlowTaskAssignerReq.ListFlowTaskAssignerReqBuilder builder = ListFlowTaskAssignerReq.builder();
        if (!CollectionUtils.isEmpty(scopeDto.getOrgScopes())) {
            builder.orgScopes(ListUtils.emptyIfNull(scopeDto.getOrgScopes()).stream()
                    .map(e -> BeanUtil.copyProperties(e, ListFlowTaskAssignerReq.OrgScope.class))
                    .collect(Collectors.toList()));
        }
        if (!CollectionUtils.isEmpty(scopeDto.getWorkerTeamScopes())) {
            builder.workerTeamScopes(ListUtils.emptyIfNull(scopeDto.getOrgScopes()).stream()
                    .map(w -> BeanUtil.copyProperties(w, ListFlowTaskAssignerReq.OrgScope.class))
                    .collect(Collectors.toList()));
        }
        ListFlowTaskAssignerReq req = builder.build();
        List<FlowTaskAssignerResp> flowTaskAssigners =
                parseApiResult(() -> flowSupportApi.listTaskAssignerAdmin(req),
                        "审批节点: " + flowElement.getId() + ", 通过管理员查询审批人",
                        "cn.axzo.karma.client.feign.FlowSupportApi.listTaskAssignerAdmin", req);

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