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

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.workflow.api.entity.instance.RuntimeNode;
import cn.stylefeng.workflow.instance.api.pojo.task.RuTaskRequest;
import cn.stylefeng.workflow.instance.modular.task.holder.EventBackTaskParamHolder;
import cn.stylefeng.workflow.instance.modular.task.holder.EventRecallParamHolder;
import cn.stylefeng.workflow.instance.modular.task.holder.pojo.RecallParam;
import cn.stylefeng.workflow.instance.modular.task.pojo.operate.*;
import cn.stylefeng.workflow.instance.modular.task.service.RuTaskService;
import cn.stylefeng.workflow.instance.modular.task.taskopt.*;
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 TaskOperateController {

    @Resource
    private RuTaskService ruTaskService;

    @Resource
    private TaskAgreeService taskAgreeService;

    @Resource
    private TaskDelegateService taskDelegateService;

    @Resource
    private TaskBackService taskBackService;

    @Resource
    private TaskRejectService taskRejectService;

    @Resource
    private TaskAddSignService taskAddSignService;

    @Resource
    private TaskReadService taskReadService;

    @Resource
    private TaskReCallService taskReCallService;

    @Resource
    private TaskCopyToService taskCopyToService;

    @Resource
    private TaskCancelService taskCancelService;

    @Resource
    private TaskBeginHandleService taskBeginHandleService;

    @Resource
    private TaskReworkService taskReworkService;

    @PostResource(name = "同意审批单个任务", path = "/ruTask/singleAgree")
    public ResponseData<?> agree(@RequestBody @Validated AgreeTaskRequest agreeTaskRequest) {
        taskAgreeService.agree(agreeTaskRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "退回某个任务", path = "/ruTask/back")
    public ResponseData<?> back(@RequestBody @Validated BackTaskRequest backTaskRequest) {

        try {
            EventBackTaskParamHolder.setParam(backTaskRequest);
            taskBackService.backTask(backTaskRequest);
        } finally {
            EventBackTaskParamHolder.clearParam();
        }

        return new SuccessResponseData<>();
    }

    @PostResource(name = "拒绝某个待办", path = "/ruTask/reject")
    public ResponseData<?> reject(@RequestBody @Validated RejectTaskRequest rejectTaskRequest) {
        taskRejectService.rejectTask(rejectTaskRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "设置某个任务为已阅", path = "/ruTask/read")
    public ResponseData<?> read(@RequestBody @Validated ReadTaskRequest readTaskRequest) {
        taskReadService.taskRead(readTaskRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "批量已阅", path = "/ruTask/batchRead")
    public ResponseData<?> batchRead(@RequestBody @Validated BatchReadTaskRequest batchReadTaskRequest) {
        taskReadService.batchRead(batchReadTaskRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "转交", path = "/ruTask/delegate")
    public ResponseData<?> delegate(@RequestBody @Validated DelegateTaskRequest delegateTaskRequest) {
        taskDelegateService.delegate(delegateTaskRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "转交任务，通过instanceId转交", path = "/ruTask/delegateByInstanceId")
    public ResponseData<?> delegateByInstanceId(@RequestBody @Validated DelegateTaskByInstanceIdRequest delegateTaskByInstanceIdRequest) {
        taskDelegateService.delegateByInstanceId(delegateTaskByInstanceIdRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "加签", path = "/ruTask/addSignTask")
    public ResponseData<?> addSignTask(@RequestBody @Validated AddSignRequest addSignRequest) {
        taskAddSignService.addSign(addSignRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "撤回", path = "/ruTask/recall")
    public ResponseData<?> recall(@RequestBody RecallRequest recallRequest) {

        RecallParam recallParam = new RecallParam();
        if (recallRequest.getInstanceId() != null) {
            recallParam.setStarterRecall(true);
        } else if (recallRequest.getTaskId() != null) {
            recallParam.setApproverRecall(true);
        }
        try {
            EventRecallParamHolder.setParam(recallParam);
            taskReCallService.recallTask(recallRequest);
        } finally {
            EventRecallParamHolder.clearParam();
        }
        return new SuccessResponseData<>();
    }

    @PostResource(name = "抄送", path = "/ruTask/copyTo")
    public ResponseData<?> copyTo(@RequestBody CopyToRequest copyToRequest) {
        taskCopyToService.copyTo(copyToRequest);
        return new SuccessResponseData<>();
    }

    @GetResource(name = "获取当前任务可以退回的节点列表", path = "/ruTask/getBackReNodeList")
    public ResponseData<List<RuntimeNode>> getBackReNodeList(@Validated(RuTaskRequest.getBackNodeList.class) RuTaskRequest ruTaskRequest) {
        List<RuntimeNode> backReNodeList = ruTaskService.getBackReNodeList(ruTaskRequest);
        return new SuccessResponseData<>(backReNodeList);
    }

    @GetResource(name = "获取当前任务对应节点的审批的类型", path = "/ruTask/getTaskNodeApproverNodeType")
    public ResponseData<Integer> getTaskNodeApproverNodeType(@Validated(BaseRequest.detail.class) RuTaskRequest ruTaskRequest) {
        Integer nodeApproverType = this.ruTaskService.getTaskNodeApproverNodeType(ruTaskRequest);
        return new SuccessResponseData<>(nodeApproverType);
    }

    @PostResource(name = "撤销", path = "/ruTask/cancel")
    public ResponseData<?> cancel(@Validated @RequestBody CancelRequest cancelRequest) {
        taskCancelService.cancel(cancelRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "开始办理某个任务", path = "/ruTask/beginHandle")
    public ResponseData<?> beginHandle(@Validated @RequestBody BeginHandleRequest beginHandleRequest) {
        taskBeginHandleService.beginHandleTask(beginHandleRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "重新办理", path = "/ruTask/reworkTask")
    public ResponseData<?> reworkTask(@Validated @RequestBody ReworkRequest reworkRequest) {
        taskReworkService.reworkTask(reworkRequest);
        return new SuccessResponseData<>();
    }

}
