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

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
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.instance.api.pojo.TaskQueryParam;
import cn.stylefeng.workflow.instance.api.pojo.request.RuntimeInstanceRequest;

public class TaskQueryConditionFactory {

    public static TaskQueryParam createTaskQueryParam(RuntimeInstanceRequest runtimeInstanceRequest) {

        TaskQueryParam taskQueryParam = new TaskQueryParam();

        Long currentUserId = LoginContext.me().getLoginUser().getUserId();

        TaskConditionEnum taskCondition = runtimeInstanceRequest.getTaskCondition();

        taskQueryParam.setSearchText(runtimeInstanceRequest.getSearchText());
        taskQueryParam.setApplicant(runtimeInstanceRequest.getApplicant());
        taskQueryParam.setApplicantOrgId(runtimeInstanceRequest.getApplicantOrgId());
        taskQueryParam.setInstanceStatusList(runtimeInstanceRequest.getInstanceStatusList());
        taskQueryParam.setSearchBeginTime(runtimeInstanceRequest.getSearchBeginTime());
        taskQueryParam.setFlowModelIdList(runtimeInstanceRequest.getFlowModelIdList());
        taskQueryParam.setTagNameList(runtimeInstanceRequest.getTagNameList());

        if (ObjectUtil.isNotEmpty(runtimeInstanceRequest.getTaskId())) {
            taskQueryParam.setTaskId(runtimeInstanceRequest.getTaskId());
        }

        if (TaskConditionEnum.MY_SUBMIT.equals(taskCondition)) {
            taskQueryParam.setApplicant(ListUtil.list(false, currentUserId));
        }

        else {

            taskQueryParam.setApprover(LoginContext.me().getLoginUser().getUserId());


            if (taskCondition.equals(TaskConditionEnum.WAIT_TO_DO)) {
                taskQueryParam.setTaskTypeList(ListUtil.of(TaskTypeEnum.PROCESSING.getCode(), TaskTypeEnum.HANDLE.getCode()));
                taskQueryParam.setTaskStatusCollection(ListUtil.list(false, TaskStatusEnum.WAIT_TO_START.getCode(), TaskStatusEnum.PROCESSING.getCode()));
            }

            else if (taskCondition.equals(TaskConditionEnum.WAIT_TO_READ)) {
                taskQueryParam.setTaskTypeList(ListUtil.of(TaskTypeEnum.COPY.getCode()));
                taskQueryParam.setReadFlag(YesOrNotEnum.N.getCode());
            }

            else if (taskCondition.equals(TaskConditionEnum.ALREADY_DO)) {
                taskQueryParam.setTaskTypeList(ListUtil.of(TaskTypeEnum.PROCESSING.getCode()));
                taskQueryParam.setTaskStatusCollection(
                        ListUtil.list(false, TaskStatusEnum.PROCESSED.getCode(), TaskStatusEnum.REJECT.getCode(), TaskStatusEnum.BACK.getCode(), TaskStatusEnum.CALL_BACK.getCode(),
                                TaskStatusEnum.TRANSFERRED.getCode(), TaskStatusEnum.CLOSE.getCode()));
            }

            else if (taskCondition.equals(TaskConditionEnum.ALREADY_READ)) {
                taskQueryParam.setTaskTypeList(ListUtil.of(TaskTypeEnum.COPY.getCode()));
                taskQueryParam.setReadFlag(YesOrNotEnum.Y.getCode());
            }
        }

        return taskQueryParam;
    }

}
