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

import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.file.api.pojo.AntdvFileInfo;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.team.project.api.pojo.BusinessRenderDetailVo;
import cn.stylefeng.workflow.api.WorkflowApi;
import cn.stylefeng.workflow.api.entity.task.RuTask;
import cn.stylefeng.workflow.api.pojo.config.node.button.NodeButtonPermission;
import cn.stylefeng.workflow.instance.api.pojo.request.RuntimeInstanceRequest;
import cn.stylefeng.workflow.instance.api.pojo.task.CommonTaskItem;
import cn.stylefeng.workflow.instance.api.pojo.task.RuTaskRequest;
import cn.stylefeng.workflow.instance.modular.task.pojo.ApprovalLog;
import cn.stylefeng.workflow.instance.modular.task.pojo.TaskDetailVo;
import cn.stylefeng.workflow.instance.modular.task.pojo.ToDoNumberVo;
import cn.stylefeng.workflow.instance.modular.task.pojo.TotalTaskNumberVo;
import cn.stylefeng.workflow.instance.modular.task.pojo.request.TaskButtonPermissionRequest;
import cn.stylefeng.workflow.instance.modular.task.pojo.request.TaskFormDataRequest;
import cn.stylefeng.workflow.instance.modular.task.pojo.request.TaskFormFileRequest;
import cn.stylefeng.workflow.instance.modular.task.service.ApprovalLogService;
import cn.stylefeng.workflow.instance.modular.task.service.FormFileService;
import cn.stylefeng.workflow.instance.modular.task.service.RuTaskService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@ApiResource(name = "任务控制器")
public class TaskController {

    @Resource
    private RuTaskService ruTaskService;

    @Resource
    private ApprovalLogService approvalsLogService;

    @Resource
    private FormFileService formFileService;

    @Resource
    private WorkflowApi workflowApi;

    @GetResource(name = "获取用户待办和待阅任务数量", path = "/ruTask/getUserWaitToTaskNum")
    public ResponseData<ToDoNumberVo> getUserWaitToTaskNum(RuTaskRequest ruTaskRequest) {
        return new SuccessResponseData<>(ruTaskService.getUserWaitToTaskNum(ruTaskRequest));
    }

    @PostResource(name = "获取用户待办任务", path = "/ruTask/getUserTasks")
    public ResponseData<PageResult<CommonTaskItem>> getUserTasks(@RequestBody @Validated(RuntimeInstanceRequest.getTasks.class) RuntimeInstanceRequest runtimeInstanceRequest) {
        PageResult<CommonTaskItem> pageResult = ruTaskService.getUserTasks(runtimeInstanceRequest);
        return new SuccessResponseData<>(pageResult);
    }

    @GetResource(name = "获取流程的审批日志列表", path = "/ruTask/getInstanceApprovalLog")
    public ResponseData<List<ApprovalLog>> getInstanceApprovalLog(
            @Validated(RuTaskRequest.getApprovalLog.class) RuTaskRequest ruTaskRequest) {
        List<ApprovalLog> approvalLogs = approvalsLogService.getSummaryApprovalLog(ruTaskRequest);
        return new SuccessResponseData<>(approvalLogs);
    }

    @GetResource(name = "获取待办任务、已办任务等界面的任务详情可操作的按钮", path = "/ruTask/getTaskButtonPermission")
    public ResponseData<NodeButtonPermission> getTaskButtonPermission(@Validated TaskButtonPermissionRequest taskButtonPermissionRequest) {
        NodeButtonPermission nodeButtonPermission = ruTaskService.getTaskButtonPermission(taskButtonPermissionRequest);
        return new SuccessResponseData<>(nodeButtonPermission);
    }

    @GetResource(name = "获取待办任务、已办任务等界面的表单数据", path = "/ruTask/getTaskFormData")
    public ResponseData<BusinessRenderDetailVo> getTaskFormDesignAndData(@Validated TaskFormDataRequest taskFormDataRequest) {
        BusinessRenderDetailVo taskFormDataVo = ruTaskService.getTaskFormData(taskFormDataRequest);
        return new SuccessResponseData<>(taskFormDataVo);
    }

    @GetResource(name = "获取待办、待阅（抄送我的）、已办、我发起的任务数量", path = "/ruTask/getUserTotalTaskNum")
    public ResponseData<TotalTaskNumberVo> getUserTotalTaskNum(RuTaskRequest ruTaskRequest) {
        TotalTaskNumberVo totalTaskNumberVo = ruTaskService.getUserTotalTaskNum(ruTaskRequest);
        return new SuccessResponseData<>(totalTaskNumberVo);
    }

    @GetResource(name = "获取任务详情", path = "/ruTask/getTaskDetail")
    public ResponseData<TaskDetailVo> getTaskDetail(RuTaskRequest ruTaskRequest) {
        TaskDetailVo taskDetail = ruTaskService.getTaskDetail(ruTaskRequest);
        return new SuccessResponseData<>(taskDetail);
    }

    @GetResource(name = "获取流程实例的文件列表", path = "/ruTask/getInstanceFileList")
    public ResponseData<List<AntdvFileInfo>> getInstanceFileList(@Validated TaskFormFileRequest taskFormFileRequest) {
        List<AntdvFileInfo> instanceFileList = formFileService.getInstanceFileList(taskFormFileRequest);
        return new SuccessResponseData<>(instanceFileList);
    }

    @GetResource(name = "获取当前用户针对指定流程实例，正在审批中的任务", path = "/ruTask/getUserInstanceTask")
    public ResponseData<RuTask> getUserInstanceTask(@Validated(BaseRequest.detail.class) RuTaskRequest ruTaskRequest) {
        return new SuccessResponseData<>(workflowApi.getUserInstanceTask(ruTaskRequest.getInstanceId()));
    }

}
