package com.ai.da.controller;

import com.ai.da.common.response.Response;
import com.ai.da.model.dto.GenerateLikeDTO;
import com.ai.da.model.dto.GenerateThroughImageTextDTO;
import com.ai.da.model.vo.GenerateCaptionVO;
import com.ai.da.model.vo.GenerateCollectionVO;
import com.ai.da.model.vo.GenerateLikeVO;
import com.ai.da.model.vo.PrepareForGenerateVO;
import com.ai.da.service.GenerateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author XP
 */
@Api(tags = "Generate模块")
@Slf4j
@RestController
@RequestMapping("/api/generate")
public class GenerateController {

    @Resource
    private GenerateService generateService;

    @ApiOperation("自动识别sketch的caption  暂时未上")
    @PostMapping("/caption")
    public Response<GenerateCaptionVO> generateCaption(@RequestParam Long sketchElementId) {
        return Response.success(generateService.generateCaption(sketchElementId));
    }

    @ApiOperation("通过文字、图片生成图片")
    @PostMapping("/sketchAndPrint")
    public Response<GenerateCollectionVO> generateThroughImageText(@Valid @RequestBody GenerateThroughImageTextDTO generateThroughImageTextDTO) {
        return Response.success(generateService.generateThroughImageText(generateThroughImageTextDTO));
    }

    @ApiOperation("喜欢生成的图片")
    @PostMapping("/like")
    public Response<GenerateLikeVO> like(@Valid @RequestBody GenerateLikeDTO generateLikeDTO) {
        return Response.success(generateService.generateLike(generateLikeDTO));
    }

    @ApiOperation(value = "取消喜欢")
    @GetMapping("/dislike")
    public Response<Boolean> dislike(@ApiParam("generateDetailId") @RequestParam Long generateDetailId,
                                     @ApiParam("timeZone") @RequestParam String timeZone) {
        return Response.success(generateService.generateDislike(generateDetailId, timeZone));
    }

    @ApiOperation(value = "发起生成请求，异步获取结果")
    @PostMapping("/prepare")
    public Response<PrepareForGenerateVO> prepareForGenerate(@Valid @RequestBody GenerateThroughImageTextDTO generateThroughImageTextDTO) {
        return Response.success(generateService.prepareForGenerate(generateThroughImageTextDTO));
    }

    @ApiOperation(value = "取消继续生成")
    @GetMapping("/stopWaiting")
    public Response<String> stopWaiting(@RequestParam("userId") Long userId,
                                        @RequestParam("uniqueId") String uniqueId,
                                        @RequestParam("timeZone") String timeZone) {
        generateService.cancelGenerate(userId, uniqueId, timeZone);
        return Response.success("stop waiting successfully");
    }

    @ApiOperation(value = "获取生成结果")
    @GetMapping("/result")
    public Response<GenerateCollectionVO> getGenerateResult(@RequestParam("uniqueId") String uniqueId) {
        GenerateCollectionVO generateResult = generateService.getGenerateResult(uniqueId);
        return Response.success(generateResult);
    }
}
