package cn.axzo.workflow.server.controller.web.bpmn;

import cn.axzo.workflow.client.feign.bpmn.ProcessTaskApi;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskAssigneeDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskAttachmentDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskAuditDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskCommentDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskCountersignDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskPageSearchDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskRemindDTO;
import cn.axzo.workflow.common.model.response.BpmPageResult;
import cn.axzo.workflow.common.model.response.bpmn.task.BpmnHistoricTaskInstanceGroupVO;
import cn.axzo.workflow.common.model.response.bpmn.task.BpmnHistoricTaskInstanceVO;
import cn.axzo.workflow.common.model.response.bpmn.task.BpmnTaskDonePageItemVO;
import cn.axzo.workflow.common.model.response.bpmn.task.BpmnTaskInstanceVO;
import cn.axzo.workflow.common.model.response.bpmn.task.BpmnTaskTodoPageItemVO;
import cn.axzo.workflow.common.valid.group.ValidGroup;
import cn.axzo.workflow.core.service.BpmnProcessTaskService;
import cn.azxo.framework.common.model.CommonResponse;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.flowable.form.api.FormInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

import static cn.azxo.framework.common.model.CommonResponse.success;

@Slf4j
@RequestMapping({"/web/v1/api/process/task", "/api/process/task"})
@RestController
@Validated
public class BpmnProcessTaskController implements ProcessTaskApi {

    @Resource
    private BpmnProcessTaskService bpmnProcessTaskService;


    /**
     * 待审核列表
     */
    @GetMapping("/page/todo")
    @Override
    public CommonResponse<BpmPageResult<BpmnTaskTodoPageItemVO>> getTodoTaskPage(@Validated @RequestBody BpmnTaskPageSearchDTO dto) {
        log.info("待审核列表 getTodoTaskPage===>>>参数：{}", dto);
        return success(bpmnProcessTaskService.getTodoTaskPage(dto));
    }

    /**
     * 已完成的审批列表
     */
    @GetMapping("/page/done")
    @Override
    public CommonResponse<BpmPageResult<BpmnTaskDonePageItemVO>> getDoneTaskPage(@Validated @RequestBody BpmnTaskPageSearchDTO dto) {
        log.info("已完成的审批列表 getDoneTaskPage===>>>参数：{}", dto);
        return success(bpmnProcessTaskService.getDoneTaskPage(dto));
    }


    /**
     * 同意
     */
    @PutMapping("/approve")
    @Override
    public CommonResponse<Boolean> approveTask(@Validated(ValidGroup.Insert.class) @RequestBody BpmnTaskAuditDTO dto) {
        log.info("同意 approveTask===>>>参数：{}", dto);
        bpmnProcessTaskService.approveTask(dto);
        return success(true);
    }

    /**
     * 拒绝
     */
    @PutMapping("/reject")
    @Override
    public CommonResponse<Boolean> rejectTask(@Validated(ValidGroup.Update.class) @RequestBody BpmnTaskAuditDTO dto) {
        log.info("拒绝 rejectTask===>>>参数：{}", dto);
        bpmnProcessTaskService.rejectTask(dto);
        return success(true);
    }

    /**
     * 转交
     */
    @Operation(summary = "直接修改审批任务的审批人")
    @PutMapping("/assignee")
    @Override
    public CommonResponse<Boolean> assigneeTask(@Validated @RequestBody BpmnTaskAssigneeDTO dto) {
        bpmnProcessTaskService.assigneeTask(dto);
        return success(true);
    }

    /**
     * 评论
     */
    @Override
    public CommonResponse<Boolean> commentTask(BpmnTaskCommentDTO commentDTO) {
        bpmnProcessTaskService.commentTask(commentDTO);
        return success(true);
    }


    /**
     * 加签
     */
    @Override
    @PutMapping("/countersign")
    public CommonResponse<Boolean> countersignTask(BpmnTaskCountersignDTO countersignDTO) {
        bpmnProcessTaskService.countersignTask(countersignDTO);
        return success(true);
    }

    @Override
    public CommonResponse<Boolean> remindTask(BpmnTaskRemindDTO dto) {
        bpmnProcessTaskService.remindTask(dto);
        return success(true);
    }

    //    @Override
//    public CommonResponse<Boolean> copyTask(BpmnTaskAssigneeDTO dto) {
//        return null;
//    }
//
//    @Override
//    public CommonResponse<Boolean> rollbackTask(BpmnTaskAssigneeDTO dto) {
//        return null;
//    }
//
//    @Override
//    public CommonResponse<Boolean> revocationTask(BpmnTaskAssigneeDTO dto) {
//        return null;
//    }
//
//    @Override
//    public CommonResponse<Boolean> remindTask(BpmnTaskAssigneeDTO dto) {
//        return null;
//    }


    /**
     * 获取指定流程实例的审批过程信息
     * <p>
     * 同一层级结构
     */
    @GetMapping("/list/flat")
    @Override
    public CommonResponse<List<BpmnHistoricTaskInstanceVO>> getTaskListFlatByProcessInstanceId(
            @NotBlank(message = "流程实例 ID 不能为空") @RequestParam String processInstanceId,
            @Nullable @RequestParam(required = false) String tenantId) {
        log.info("获取历史已审批的列表详情 getTaskListFlatByProcessInstanceId===>>>参数：{}", processInstanceId);
        return success(bpmnProcessTaskService.getHistoricTaskListByProcessInstanceId(processInstanceId, tenantId));
    }

    /**
     * 获取指定流程实例的审批过程信息
     * <p>
     * 分组结构
     */
    @GetMapping("/list/group")
    @Override
    public CommonResponse<List<BpmnHistoricTaskInstanceGroupVO>> getTaskListGroupByProcessInstanceId(
            @NotBlank(message = "流程实例 ID 不能为空") @RequestParam String processInstanceId,
            @Nullable @RequestParam(required = false) String tenantId) {
        log.info("获取历史已审批的列表详情 getTaskListGroupByProcessInstanceId===>>>参数：{}", processInstanceId);
        return success(bpmnProcessTaskService.getHistoricTaskListGroupByProcessInstanceId(processInstanceId, tenantId));
    }

    /**
     * 获取实例正在审核的人列表
     */
    @GetMapping("/active/list")
    @Override
    public CommonResponse<List<BpmnTaskInstanceVO>> getActiveTasksByProcessInstanceId(
            @NotBlank(message = "流程实例 ID 不能为空") @RequestParam String processInstanceId,
            @NotBlank(message = "租户不能为空") @RequestParam String tenantId) {
        log.info(" 获取实例正在审核的人列表 getActiveTasksByProcessInstanceId===>>>参数：{}", processInstanceId);
        return success(bpmnProcessTaskService.getActiveTasksByProcessInstanceId(processInstanceId, tenantId));
    }

    @GetMapping("/form")
    public CommonResponse<FormInfo> getTaskForm(@NotBlank(message = "任务 ID 不能为空") @RequestParam String taskId,
                                                @NotBlank(message = "租户不能为空") @RequestParam String tenantId) {
        return success(bpmnProcessTaskService.getTaskFormModel(taskId, tenantId));
    }

    @PostMapping("/attachment/add")
    public CommonResponse<Void> addAttachment(@Validated @RequestBody BpmnTaskAttachmentDTO dto) {
        bpmnProcessTaskService.attachmentTask(dto);
        return success();
    }

}
