package cn.stylefeng.workflow.api.util;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.workflow.api.entity.task.RuTask;
import cn.stylefeng.workflow.api.enums.TaskStatusEnum;
import cn.stylefeng.workflow.api.enums.TaskTypeEnum;

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

public class WorkFlowStatusUtil {

    public static boolean haveRunningTaskOrHandling(List<RuTask> ruTaskList) {
        if (ObjectUtil.isEmpty(ruTaskList)) {
            return false;
        }

        List<RuTask> collect = ruTaskList.stream().filter(taskItem -> {

            if (TaskTypeEnum.PROCESSING.getCode().equals(taskItem.getTaskType())) {
                return TaskStatusEnum.PROCESSING.getCode().equals(taskItem.getTaskStatus());
            }

            else if (TaskTypeEnum.HANDLE.getCode().equals(taskItem.getTaskType())) {
                return TaskStatusEnum.PROCESSING.getCode().equals(taskItem.getTaskStatus())
                        || TaskStatusEnum.WAIT_TO_START.getCode().equals(taskItem.getTaskStatus());
            }

            return false;
        }).collect(Collectors.toList());

        return !collect.isEmpty();
    }

    public static void initTaskStatus(RuTask ruTask, TaskTypeEnum taskTypeEnum) {

        if (TaskTypeEnum.HANDLE.equals(taskTypeEnum)) {
            ruTask.setTaskStatus(TaskStatusEnum.WAIT_TO_START.getCode());
        }

        else {
            ruTask.setTaskStatus(TaskStatusEnum.PROCESSING.getCode());
        }
    }

    public static void initTaskStatus(RuTask ruTask, Integer taskType) {

        if (TaskTypeEnum.HANDLE.getCode().equals(taskType)) {
            ruTask.setTaskStatus(TaskStatusEnum.WAIT_TO_START.getCode());
        }

        else {
            ruTask.setTaskStatus(TaskStatusEnum.PROCESSING.getCode());
        }
    }

}
