package com.ycwl.basic.controller.task;

import com.ycwl.basic.annotation.IgnoreLogReq;
import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.model.pc.task.resp.TaskRespVO;
import com.ycwl.basic.model.pc.template.resp.TemplateRespVO;
import com.ycwl.basic.model.task.req.TaskReqVo;
import com.ycwl.basic.model.task.req.TaskSuccessReqVo;
import com.ycwl.basic.model.task.req.WorkerAuthReqVo;
import com.ycwl.basic.model.task.resp.TaskSyncRespVo;
import com.ycwl.basic.service.task.TaskService;
import com.ycwl.basic.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@IgnoreToken
@RestController
// 渲染端对接接口
@RequestMapping("/task/v1/")
public class TaskTaskController {

    @Autowired
    private TaskService taskService;
    @IgnoreLogReq
    @PostMapping("/sync")
    public ApiResponse<TaskSyncRespVo> sync(@RequestBody TaskReqVo req) {
        TaskSyncRespVo respVo = taskService.handleSyncTask(req);
        if (respVo == null) {
            return ApiResponse.fail("错误");
        }
        return ApiResponse.success(respVo);
    }

    @GetMapping("/{taskId}/info")
    public ApiResponse<TaskRespVO> taskInfo(@PathVariable Long taskId) {
        return ApiResponse.success(taskService.taskInfo(taskId));
    }

    @PostMapping("/template/{templateId}")
    public ApiResponse<TemplateRespVO> getTemplateById(@PathVariable Long templateId, @RequestBody WorkerAuthReqVo req) {
        return ApiResponse.success(taskService.workerGetTemplate(templateId, req));
    }

    @PostMapping("/{taskId}/uploadUrl")
    public ApiResponse<String> getUploadUrl(@PathVariable Long taskId, @RequestBody WorkerAuthReqVo req) {
        String urlForUpload = taskService.getUploadUrl(taskId, req);
        return ApiResponse.success(urlForUpload);
    }

    @PostMapping("/{taskId}/start")
    public ApiResponse taskStart(@PathVariable Long taskId, @RequestBody WorkerAuthReqVo req) {
        taskService.taskStart(taskId, req);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{taskId}/success")
    public ApiResponse taskSuccess(@PathVariable Long taskId, @RequestBody TaskSuccessReqVo req) {
        taskService.taskSuccess(taskId, req);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{taskId}/fail")
    public ApiResponse taskFail(@PathVariable Long taskId, @RequestBody WorkerAuthReqVo req) {
        taskService.taskFail(taskId, req);
        return ApiResponse.success("OK");
    }
}
