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

import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;
import cn.axzo.workflow.core.deletage.BpmnTaskCalculateDTO;
import cn.axzo.workflow.core.deletage.BpmnTaskDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

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


@Service
public class BpmnDefaultTaskDelegateImpl implements BpmnTaskDelegate {
    private static final Logger log = LoggerFactory.getLogger(BpmnDefaultTaskDelegateImpl.class);

    @Override
    public List<BpmnTaskDelegateAssigner> calculateAssignerAtExecution(BpmnTaskCalculateDTO delegateTask) {
        // 这里的上层应该需要使用 Chain, 类似 Servlet 中的 Filter 模式,有一个有限策略, 例如此处的设置审批人, 第一个策略是优先获取上级直接指定
        // 第二个策略是根据配置规则设置审批人.
        // 而整体为了保证维护性和扩展性,需要内置整个链路有限策略,只要有一个策略已经有人了,则不再执行后续逻辑.
        // 暂时未做此处的设计,早期的模式只有上级直接指定
        log.info("计算审核人,当前的审批任务 DefinitionKey 为: {}", delegateTask.getTaskDefinitionKey());
        List<BpmnTaskDelegateAssigner> assigners = new ArrayList<>();
        Optional.ofNullable(delegateTask.getVariables().get(INTERNAL_SPECIFY_NEXT_APPROVER)).ifPresent(next -> {
            assigners.add((BpmnTaskDelegateAssigner) next);
        });

        if (CollectionUtils.isEmpty(assigners)) {
            if (Objects.equals("NODE1694773129716_0.9426625492049914_1", delegateTask.getTaskDefinitionKey())) {
                // 或签
                BpmnTaskDelegateAssigner assigner1 = new BpmnTaskDelegateAssigner();
                assigner1.setAssignee("1");
                assigner1.setAssigneeType("5");
                assigner1.setAssignerName("或签人1");
                assigner1.setPersonId("100001");
                assigner1.setTenantId("296");
                assigners.add(assigner1);
            } else if (Objects.equals("NODE1695291557658_0.7106808048056956_1", delegateTask.getTaskDefinitionKey())) {
                // 会签节点
                BpmnTaskDelegateAssigner assigner1 = new BpmnTaskDelegateAssigner();
                assigner1.setAssignee("1");
                assigner1.setAssigneeType("5");
                assigner1.setAssignerName("会签人1");
                assigner1.setPersonId("100001");
                assigner1.setTenantId("296");
                assigners.add(assigner1);

                BpmnTaskDelegateAssigner assigner2 = new BpmnTaskDelegateAssigner();
                assigner2.setAssignee("2");
                assigner2.setAssigneeType("5");
                assigner2.setAssignerName("会签人2");
                assigner2.setPersonId("100002");
                assigner2.setTenantId("296");
                assigners.add(assigner2);

                BpmnTaskDelegateAssigner assigner3 = new BpmnTaskDelegateAssigner();
                assigner3.setAssignee("3");
                assigner3.setAssigneeType("5");
                assigner3.setAssignerName("会签人3");
                assigner3.setPersonId("100003");
                assigner3.setTenantId("296");
                assigners.add(assigner3);

                BpmnTaskDelegateAssigner assigner4 = new BpmnTaskDelegateAssigner();
                assigner4.setAssignee("4");
                assigner4.setAssigneeType("5");
                assigner4.setAssignerName("会签人4");
                assigner4.setPersonId("100004");
                assigner4.setTenantId("296");
                assigners.add(assigner4);
            } else if (Objects.equals("NODE1694770290894_0.4227607475273871_1", delegateTask.getTaskDefinitionKey())) {
                // 或签节点
                BpmnTaskDelegateAssigner assigner1 = new BpmnTaskDelegateAssigner();
                assigner1.setAssignee("1");
                assigner1.setAssigneeType("5");
                assigner1.setAssignerName("或签人1");
                assigner1.setPersonId("100001");
                assigner1.setTenantId("296");
                assigners.add(assigner1);

                BpmnTaskDelegateAssigner assigner2 = new BpmnTaskDelegateAssigner();
                assigner2.setAssignee("2");
                assigner2.setAssigneeType("5");
                assigner2.setAssignerName("或签人2");
                assigner2.setPersonId("100001");
                assigner2.setTenantId("296");
                assigners.add(assigner2);
            }
        }

        if (CollectionUtils.isEmpty(assigners) && Objects.equals("69", delegateTask.getTenantId())) {
            if (Objects.equals("NODE1639041346796_0.5413743892974803_0", delegateTask.getTaskDefinitionKey())) {
                BpmnTaskDelegateAssigner assigner1 = new BpmnTaskDelegateAssigner();
                assigner1.setAssignee("1");
                assigner1.setAssigneeType("5");
                assigner1.setAssignerName("或签人1");
                assigner1.setPersonId("100001");
                assigner1.setTenantId("296");
                assigners.add(assigner1);
            }
            if (Objects.equals("NODE1687933854432_0.45762305710855467_1", delegateTask.getTaskDefinitionKey())) {
                BpmnTaskDelegateAssigner assigner1 = new BpmnTaskDelegateAssigner();
                assigner1.setAssignee("1");
                assigner1.setAssigneeType("5");
                assigner1.setAssignerName("或签人1");
                assigner1.setPersonId("100001");
                assigner1.setTenantId("296");
                assigners.add(assigner1);
            }
        }
        return assigners;
    }
}
