package cn.stylefeng.workflow.instance.modular.task.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.workflow.api.NodeApi;
import cn.stylefeng.workflow.api.entity.design.Node;
import cn.stylefeng.workflow.api.entity.task.RuTask;
import cn.stylefeng.workflow.api.enums.NodeTypeEnum;
import cn.stylefeng.workflow.api.enums.TaskConditionEnum;
import cn.stylefeng.workflow.api.enums.TaskStatusEnum;
import cn.stylefeng.workflow.api.enums.TaskTypeEnum;
import cn.stylefeng.workflow.api.pojo.InstanceContext;
import cn.stylefeng.workflow.api.pojo.config.node.NodeData;
import cn.stylefeng.workflow.api.pojo.config.node.button.NodeButtonPermission;
import cn.stylefeng.workflow.instance.api.WorkflowStatusApi;
import cn.stylefeng.workflow.instance.core.path.NodePathCalc;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TaskButtonStrategyFactory {

    public static NodeButtonPermission getNodeButtonPermission(TaskConditionEnum taskConditionEnum, RuTask ruTask, Long instanceId) {


        if (TaskConditionEnum.MY_SUBMIT.equals(taskConditionEnum)) {
            return getMySubmitButton(instanceId);
        }

        else if (TaskConditionEnum.ALREADY_DO.equals(taskConditionEnum)) {
            return getAlreadyDoButton(ruTask);
        }

        else if (TaskConditionEnum.WAIT_TO_READ.equals(taskConditionEnum)) {
            NodeButtonPermission nodeButtonPermission = new NodeButtonPermission();
            nodeButtonPermission.setHaveRead(true);
            return nodeButtonPermission;
        }

        else if (TaskConditionEnum.WAIT_TO_DO.equals(taskConditionEnum)) {
            return getWaitToDoNodeButton(ruTask);
        }

        return new NodeButtonPermission();
    }

    private static NodeButtonPermission getMySubmitButton(Long instanceId) {

        if (ObjectUtil.isEmpty(instanceId)) {
            return new NodeButtonPermission();
        }

        WorkflowStatusApi workflowStatusApi = SpringUtil.getBean(WorkflowStatusApi.class);
        InstanceContext instanceContext = workflowStatusApi.buildInstanceContext(instanceId);

        Node applicantNode = new NodePathCalc(instanceContext).getPointNode(NodeTypeEnum.APPLICANT);
        if (applicantNode == null) {
            return new NodeButtonPermission();
        }

        NodeData nodeData = applicantNode.getNodeData();
        if (nodeData == null) {
            return new NodeButtonPermission();
        }

        NodeButtonPermission nodeButtonPermission = nodeData.getNodeButtonPermission();
        if (nodeButtonPermission == null) {
            return new NodeButtonPermission();
        }

        NodeButtonPermission temp = new NodeButtonPermission();
        temp.setRetract(nodeButtonPermission.getRetract());
        temp.setRetractAliasName(nodeButtonPermission.getRetractAliasName());

        temp.setCancel(nodeButtonPermission.getCancel());
        temp.setCancelAliasName(nodeButtonPermission.getCancelAliasName());

        return temp;
    }

    private static NodeButtonPermission getAlreadyDoButton(RuTask ruTask) {

        NodeButtonPermission finalResult = new NodeButtonPermission();

        Long reNodeId = ruTask.getReNodeId();

        NodeApi nodeApi = SpringUtil.getBean(NodeApi.class);
        Node nodeInfo = nodeApi.getNodeInfo(reNodeId);

        if (nodeInfo == null) {
            return finalResult;
        }

        NodeButtonPermission nodeButtonPermission = nodeInfo.getNodeData().getNodeButtonPermission();
        finalResult.setRetract(nodeButtonPermission.getRetract());
        return finalResult;
    }

    private static NodeButtonPermission getWaitToDoNodeButton(RuTask ruTask) {

        NodeButtonPermission finalResult = new NodeButtonPermission();

        Long reNodeId = ruTask.getReNodeId();
        try {

            NodeApi nodeApi = SpringUtil.getBean(NodeApi.class);
            Node nodeInfo = nodeApi.getNodeInfo(reNodeId);
            if (nodeInfo.getNodeType().equals(NodeTypeEnum.APPLICANT.getNodeType())) {
                finalResult.setAgree(true);
                return finalResult;
            }

            NodeData nodeData = nodeInfo.getNodeData();
            if (nodeData != null) {
                NodeButtonPermission tempPermission = nodeData.getNodeButtonPermission();

                tempPermission.setRetract(false);

                if (TaskTypeEnum.HANDLE.getCode().equals(ruTask.getTaskType())
                        && TaskStatusEnum.WAIT_TO_START.getCode().equals(ruTask.getTaskStatus())) {

                    tempPermission.setBegin(true);
                    tempPermission.setBeginAliasName("开始");

                    tempPermission.setAgree(false);
                }

                return tempPermission;
            }
        } catch (Exception e) {
            log.error("获取不到节点配置信息，ReNode节点Id是：" + reNodeId, e);
        }

        return finalResult;
    }

}
