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

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.workflow.api.entity.task.RuTask;
import cn.stylefeng.workflow.api.enums.TaskStatusEnum;
import cn.stylefeng.workflow.api.enums.TaskTypeEnum;
import cn.stylefeng.workflow.instance.modular.task.pojo.TotalTaskNumberVo;

import java.util.List;
import java.util.Set;

public class TaskNumberFactory {

    public static TotalTaskNumberVo createTotalTaskNumber(List<RuTask> totalTaskSummary, Set<Long> currentUserInstanceIdList) {
        TotalTaskNumberVo totalTaskNumberVo = new TotalTaskNumberVo();

        if (ObjectUtil.isEmpty(totalTaskSummary)) {
            return totalTaskNumberVo;
        }

        for (RuTask ruTask : totalTaskSummary) {

            if (ruTask.getTaskStatus().equals(TaskStatusEnum.PROCESSING.getCode())
                    && ruTask.getTaskType().equals(TaskTypeEnum.PROCESSING.getCode())) {
                totalTaskNumberVo.setToDoTaskNumber(totalTaskNumberVo.getToDoTaskNumber() + 1);
            }

            if (ruTask.getTaskStatus().equals(TaskStatusEnum.PROCESSED.getCode())
                    && ruTask.getTaskType().equals(TaskTypeEnum.PROCESSING.getCode())) {
                totalTaskNumberVo.setAlreadyDoneTaskNumber(totalTaskNumberVo.getAlreadyDoneTaskNumber() + 1);
            }

            if (ruTask.getTaskStatus().equals(TaskStatusEnum.PROCESSING.getCode())
                    && ruTask.getTaskType().equals(TaskTypeEnum.COPY.getCode())
                    && ruTask.getReadFlag().equals(YesOrNotEnum.N.getCode())) {
                totalTaskNumberVo.setToReadTaskNumber(totalTaskNumberVo.getToReadTaskNumber() + 1);
            }

            if (ruTask.getTaskStatus().equals(TaskStatusEnum.PROCESSED.getCode())
                    && ruTask.getTaskType().equals(TaskTypeEnum.COPY.getCode())) {
                totalTaskNumberVo.setAlreadyReadTaskNumber(totalTaskNumberVo.getAlreadyReadTaskNumber() + 1);
            }
        }

        if (ObjectUtil.isNotEmpty(currentUserInstanceIdList)) {
            totalTaskNumberVo.setMyStartTaskNumber(currentUserInstanceIdList.size());
        }

        return totalTaskNumberVo;
    }

}
