package com.backendsys.modules.sdk.klingai.controller;

import cn.hutool.json.JSONUtil;
import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.common.utils.Result;
import com.backendsys.modules.sdk.klingai.entity.KLGenerationImageParams;
import com.backendsys.modules.sdk.klingai.entity.KLImage2VideoParams;
import com.backendsys.modules.sdk.klingai.entity.KLResponse;
import com.backendsys.modules.sdk.klingai.entity.KLText2VideoParams;
import com.backendsys.modules.sdk.klingai.service.KLingService;
import com.backendsys.modules.sdk.klingai.utils.KLingUtil;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class KLingDemoController {

    @Autowired
    private KLingUtil kLingUtil;
    @Autowired
    private KLingService klingService;

    @Anonymous
    @Operation(summary = "获取接口Token")
    @GetMapping("/api/klingai/getToken")
    public String getToken() {
        return kLingUtil.getToken();
    }

    @Anonymous
    @Operation(summary = "6-1 查询账号下资源包列表及余量")
    @GetMapping("/api/klingai/queryAccountCosts")
    public Result queryAccountCosts() {
        Mono<KLResponse> responseMono = klingService.queryAccountCosts();
        KLResponse response = responseMono.block();
        System.out.println("同步结果: " + response);
        System.out.println("同步结果 (data): " + response.getData());
        return Result.success().put("data", response);
    }

    @Anonymous
    @Operation(summary = "2-1【图像生成】创建任务 (同步)")
    @PostMapping("/api/klingai/generationImage")
    public Result generationImage(@RequestBody KLGenerationImageParams params) {
        Mono<KLResponse> responseMono = klingService.generationImage(params);
        KLResponse response = responseMono.block();
        System.out.println("同步结果: " + response);
        System.out.println("同步结果 (data): " + response.getData());
        return Result.success().put("data", response);
    }

    @Anonymous
    @Operation(summary = "2-2【图像生成】查询任务（单个）(同步)")
    @GetMapping("/api/klingai/queryImageTask")
    public Result queryImageTask(String task_id) {
        Mono<KLResponse> responseMono = klingService.queryImageTask(task_id);
        KLResponse response = responseMono.block();
        System.out.println("同步结果: " + response);
        System.out.println("同步结果 (data): " + response.getData());
        return Result.success().put("data", response);
    }

    @Anonymous
    @Operation(summary = "2-2【图像生成】查询任务（单个）(异步)")
    @GetMapping("/api/klingai/queryImageTaskAsync")
    public Result queryImageTaskAsync(String task_id) {
        Mono<KLResponse> responseMono = klingService.queryImageTask(task_id);
        responseMono.subscribe(response -> {
            System.out.println("异步结果: " + response);
            System.out.println("异步结果 (data): " + response.getData());
        });
        return Result.success();
    }

    @Anonymous
    @Operation(summary = "3-1【文生视频】创建任务 (同步)")
    @PostMapping("/api/klingai/text2Video")
    public Result text2Video(@RequestBody KLText2VideoParams params) {
        Mono<KLResponse> responseMono = klingService.text2Video(params);
        KLResponse response = responseMono.block();
        System.out.println("同步结果: " + response);
        System.out.println("同步结果 (data): " + response.getData());
        return Result.success().put("data", response);
    }

    @Anonymous
    @Operation(summary = "3-2【文生视频】查询任务（单个）")
    @GetMapping("/api/klingai/queryText2VideoTask")
    public Result queryText2VideoTask(String task_id) {
        Mono<KLResponse> responseMono = klingService.queryText2VideoTask(task_id);
        KLResponse response = responseMono.block();
        System.out.println("同步结果: " + response);
        System.out.println("同步结果 (data): " + response.getData());
        return Result.success().put("data", response);
    }

    @Anonymous
    @Operation(summary = "3-4【图生视频】创建任务")
    @PostMapping("/api/klingai/image2Video")
    public Result image2Video(@RequestBody KLImage2VideoParams params) {
        Mono<KLResponse> responseMono = klingService.image2Video(params);
        KLResponse response = responseMono.block();
        System.out.println("同步结果: " + response);
        System.out.println("同步结果 (data): " + response.getData());
        return Result.success().put("data", response);
    }

    @Anonymous
    @Operation(summary = "3-5【图生视频】查询任务（单个）")
    @GetMapping("/api/klingai/queryImage2VideoTask")
    public Result queryImage2VideoTask(String task_id) {
        Mono<KLResponse> responseMono = klingService.queryImage2VideoTask(task_id);
        KLResponse response = responseMono.block();
        System.out.println("同步结果: " + response);
        System.out.println("同步结果 (data): " + response.getData());
        return Result.success().put("data", response);
    }

}
