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

import cn.hutool.core.date.DateUtil;
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.instance.RuntimeNode;
import cn.stylefeng.workflow.api.entity.task.RuTask;
import cn.stylefeng.workflow.api.enums.TaskReadFlagEnum;
import cn.stylefeng.workflow.api.enums.TaskStatusEnum;
import cn.stylefeng.workflow.api.enums.TaskTypeEnum;
import cn.stylefeng.workflow.instance.modular.task.pojo.ApprovalLog;

import java.util.ArrayList;
import java.util.List;

public class ApprovalLogFactory {

    public static ApprovalLog createStartApprovalLog(RuntimeNode startRuntimeNode, Long applicant) {

        ApprovalLog approvalLog = new ApprovalLog();

        if (ObjectUtil.isNotEmpty(startRuntimeNode)) {
            approvalLog.setNodeName(startRuntimeNode.getNodeName());
        } else {
            approvalLog.setNodeName("开始节点");
        }

        approvalLog.setProcessUserId(applicant);

        approvalLog.setTaskType(TaskTypeEnum.START.getCode());

        approvalLog.setTaskStatus(TaskStatusEnum.START.getCode());

        approvalLog.setReadFlag(TaskReadFlagEnum.UN_READ.getCode());

        if (ObjectUtil.isNotEmpty(startRuntimeNode)) {
            approvalLog.setBeginTime(startRuntimeNode.getCreateTime());
            approvalLog.setCreateTime(startRuntimeNode.getCreateTime());
        }

        if (ObjectUtil.isNotEmpty(startRuntimeNode)) {
            approvalLog.setApproveTime(startRuntimeNode.getCreateTime());
        }

        approvalLog.setDurationFormat("0秒");

        approvalLog.setSuggestContent(null);

        return approvalLog;
    }

    public static List<ApprovalLog> createRuTaskApprovalLog(List<RuTask> ruTaskList) {

        ArrayList<ApprovalLog> approvalLogs = new ArrayList<>();

        NodeApi nodeApi = SpringUtil.getBean(NodeApi.class);

        for (RuTask ruTask : ruTaskList) {
            ApprovalLog approvalLog = createApprovalLog(ruTask, nodeApi);
            approvalLogs.add(approvalLog);
        }

        return approvalLogs;
    }

    public static ApprovalLog createApprovalLog(RuTask ruTask, NodeApi nodeApi) {

        Node nodeInfo = nodeApi.getNodeInfo(ruTask.getReNodeId());

        ApprovalLog approvalLog = new ApprovalLog();

        approvalLog.setNodeId(nodeInfo.getNodeId());

        approvalLog.setNodeName(nodeInfo.getNodeName());

        approvalLog.setProcessUserId(ruTask.getApprover());

        approvalLog.setTaskType(ruTask.getTaskType());

        approvalLog.setTaskStatus(ruTask.getTaskStatus());

        approvalLog.setReadFlag(ruTask.getReadFlag());

        approvalLog.setCreateTime(ruTask.getCreateTime());

        approvalLog.setBeginTime(ruTask.getBeginTime());

        approvalLog.setApproveTime(ruTask.getSuggestTime());

        if (ObjectUtil.isNotEmpty(ruTask.getCreateTime()) && ObjectUtil.isNotEmpty(ruTask.getSuggestTime())) {
            String durationFormat = DateUtil.formatBetween(ruTask.getCreateTime(), ruTask.getSuggestTime());
            approvalLog.setDurationFormat(durationFormat);
        }

        approvalLog.setSuggestContent(ruTask.getSuggestContent());

        return approvalLog;
    }

}
