package com.ai.da.controller;

import com.ai.da.common.enums.CreditsEventsEnum;
import com.ai.da.common.response.Response;
import com.ai.da.mapper.primary.entity.CollectionSort;
import com.ai.da.model.dto.*;
import com.ai.da.model.vo.*;
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.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @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 void generateThroughImageText(@Valid @RequestBody GenerateThroughImageTextDTO generateThroughImageTextDTO) {
//        return Response.success(generateService.generateThroughImageText(generateThroughImageTextDTO));
        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 ResponseEntity<Response<PrepareForGenerateVO>> prepareForGenerate(@Valid @RequestBody GenerateThroughImageTextDTO generateThroughImageTextDTO) {
        PrepareForGenerateVO prepareForGenerateVO = generateService.prepareForGenerate(generateThroughImageTextDTO);
        if (prepareForGenerateVO.getStatus().equals(200)){
            prepareForGenerateVO.setStatus(0);
            return ResponseEntity.ok(Response.success(prepareForGenerateVO));
        } else {
            return ResponseEntity.status(prepareForGenerateVO.getStatus()).body(Response.fail("The rate limit has been exceeded. Please wait 1 minute before retrying."));
        }
    }

    @ApiOperation(value = "取消继续生成")
    @GetMapping("/stopWaiting")
    public Response<String> stopWaiting(@RequestParam("userId") Long userId,
                                        @RequestParam("uniqueId") List<String> uniqueId,
                                        @RequestParam("timeZone") String timeZone,
                                        @RequestParam("type") String type) {
        generateService.cancelGenerate(userId, uniqueId, timeZone, type);
        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);
    }*/

    @ApiOperation(value = "获取生成结果")
    @PostMapping("/result")
    public Response<List<GenerateResultVO>> getGenerateResults(@Valid @RequestBody List<String> taskIdList) {
        List<GenerateResultVO> generateResult = generateService.getGenerateResultList(taskIdList);
        return Response.success(generateResult);
    }

    @ApiOperation(value = "imageToSketch")
    @PostMapping("/imageToSketch")
    public Response<String> imageToSketch(@Valid @RequestBody ImageToSketchDTO imageToSketchDTO) {
        return Response.success(generateService.imageToSketchAsync(imageToSketchDTO, null, null));
//        return Response.success(generateService.imageToSketch(imageToSketchDTO, null, null));
    }

    // modifySketch
    @ApiOperation(value = "modifySketch")
    @PostMapping("/modifySketch")
    public Response<GenerateResultVO> modifySketch(@Valid @RequestBody GenerateModifyDTO generateModifyDTO) {
        return Response.success(generateService.modifySketch(generateModifyDTO));
    }

    @ApiOperation(value = "请求进行姿势变换")
    @PostMapping("/poseTransform")
    public Response<ToProductImageResultVO> poseTransform(@Valid @RequestBody PoseTransformDTO poseTransformDTO) {
        return Response.success(generateService.poseTransform(poseTransformDTO));
    }

    @ApiOperation(value = "获取姿势变换生成结果")
    @PostMapping("/poseTransformResult")
    public Response<List<PoseTransformationVO>> getPoseTransformationResults(@Valid @RequestBody List<String> taskIdList) {
        List<PoseTransformationVO> generateResult = generateService.getPoseTransformationResult(taskIdList, null, null);
        return Response.success(generateResult);
    }

    @ApiOperation("喜欢或取消喜欢姿势变换生成的图片")
    @PostMapping("/likeOrDislike")
    public Response<CollectionSort> likeOrDislike(@ApiParam("id") @RequestParam Long transformedId, @ApiParam("like || dislike") @RequestParam String likeOrDislike, @RequestParam("projectId") Long projectId, @RequestParam(value = "collectionSortParentId", required = false) Long collectionSortParentId) {
        return Response.success(generateService.disOrLikePose(transformedId, likeOrDislike, projectId, collectionSortParentId));
    }

    @ApiOperation(value = "修改模特比例")
    @PostMapping("/modifyProportion")
    public Response<String> modifyModelProportion(@Valid @RequestBody ModifyModelProportionDTO proportionDTO){
        String path = generateService.modifyModelProportion(proportionDTO);
        return Response.success(path);
    }

    @ApiOperation(value = "拼贴图生成线稿")
    @PostMapping("/genSketchRecon")
    public Response<GenerateResultVO> sketchReconstructionGenerate(@Valid @RequestBody SketchReconstructionDTO sketchReconstructionDTO){
        GenerateResultVO generateResultVO = generateService.sketchReconstructionGenerate(sketchReconstructionDTO);
        return Response.success(generateResultVO);
    }

    @ApiOperation(value = "获取拼贴图最后一次生成结果")
    @GetMapping("/getReconLastResult")
    public Response<SketchReconstructionVO> getSketchReconstruction(@RequestParam("projectId") Long projectId){
        SketchReconstructionVO sketchReconstruction = generateService.getSketchReconstruction(projectId);
        return Response.success(sketchReconstruction);
    }

    @ApiOperation(value = "获取pose transfer的所有pose")
    @GetMapping("/getAllPose")
    public Response<List<Map<String, String>>> getAllPose(){
        return Response.success(generateService.getAllPose());
    }

    @ApiOperation(value = "删除pose transfer的结果")
    @GetMapping("/deleteResult")
    public Response<String> deleteToProductRelightResult(@RequestParam("projectId") @NotNull Long projectId,
                                                         @RequestParam("id") @NotNull Long id){
        try{
            generateService.deleteGeneratedPose(projectId, id);
            return Response.success();
        }catch (Exception e){
            return Response.fail(e.getMessage());
        }
    }

    /*@ApiOperation(value = "万象 t2i 创建异步任务")
    @GetMapping("/createAsyncTask")
    public Response<String> createAsyncTask(@RequestParam("prompt") String prompt){
        return Response.success(generateService.createAsyncTask(87L, prompt, ""));
    }

    @ApiOperation(value = "万象 t2i 获取异步任务结果")
    @GetMapping("/waitAsyncTask")
    public Response<GenerateResultVO> waitAsyncTask(@RequestParam("taskId") String taskId){
        return Response.success(generateService.getAsyncTaskResult(taskId));
    }

    @ApiOperation(value = "万象 图生动图")
    @GetMapping("/animateAnyone")
    public Response<String> animateAnyone(@Valid @RequestBody PoseTransformDTO poseTransformDTO){
        return Response.success(generateService.animateAnyone(poseTransformDTO, null));
    }

    @ApiOperation(value = "万象 获取动图模板id")
    @GetMapping("/getVideoTemplateId")
    public Response<String> getVideoTemplateId(@RequestParam("videoPath") String videoPath){
        return Response.success(generateService.getVideoTemplateId(videoPath));
    }

    @ApiOperation(value = "万象 获取动图结果")
    @GetMapping("/getAnimateResult")
    public Response<PoseTransformationVO> getAnimateResult(@RequestParam("taskId") String taskId){
        return Response.success(generateService.getAnimateResult(taskId));
    }

    @ApiOperation(value = "freepik toProductImage")
    @GetMapping("/reimagineFreePik")
    public Response<String> reimagineFreePik(@RequestParam("path") String path,
                                             @RequestParam("prompt") String prompt,
                                             @RequestParam("style") String style) throws IOException {
        return Response.success(generateService.reimagineFreePik(path, prompt, style));
    }

    @ApiOperation(value = "获取图片描述")
    @GetMapping("/getImageDescription")
    public Response<String> getImageDescription(@RequestParam("path") String path) {
        return Response.success(generateService.getImageDescription(path));
    }*/

//    @ApiOperation(value = "试用flux")
//    @GetMapping("/flux")
    public Response<String> flux(@RequestParam(value = "path", required = false) String path,
                                 @RequestParam("type") int type,
                                 @RequestParam(value = "prompt", required = false) String prompt){
        CreditsEventsEnum typeEnum = CreditsEventsEnum.RELIGHT;
        switch (type){
            case 1:
                typeEnum = CreditsEventsEnum.TO_PRODUCT_IMAGE;
                break;
            case 2:
                typeEnum = CreditsEventsEnum.IMAGE_TO_SKETCH;
                break;
            case 3:
                typeEnum = CreditsEventsEnum.PATTERN;
                break;
        }
        return Response.success(generateService.flux(typeEnum, prompt, path, false));
    }

//    @ApiOperation(value = "获取flux结果")
//    @GetMapping("/fluxResult")
    public Response<String> fluxResult(@RequestParam("taskId") String taskId){
        return Response.success(generateService.getFluxResult(taskId, "87/" + taskId + ".png"));
    }



}
