package com.backendsys.modules.sdk.volcengine.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.backendsys.exception.CustException;
import com.backendsys.modules.sdk.volcengine.entity.VisualFaceFusionMovie;
import com.backendsys.modules.sdk.volcengine.entity.VisualFaceSwapV2;
import com.backendsys.modules.sdk.volcengine.service.VolcengineService;
import com.volcengine.service.visual.IVisualService;
import com.volcengine.service.visual.impl.VisualServiceImpl;
import com.volcengine.service.visual.model.request.VisualFaceFusionMovieGetResultRequest;
import com.volcengine.service.visual.model.request.VisualFaceFusionMovieSubmitTaskRequest;
import com.volcengine.service.visual.model.request.VisualFaceFusionMovieSyncRequest;
import com.volcengine.service.visual.model.request.VisualFaceSwapV2Request;
import com.volcengine.service.visual.model.response.VisualFaceFusionMovieGetResultResponse;
import com.volcengine.service.visual.model.response.VisualFaceFusionMovieSubmitTaskResponse;
import com.volcengine.service.visual.model.response.VisualFaceFusionMovieSyncResponse;
import com.volcengine.service.visual.model.response.VisualFaceSwapV2Response;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 通用返回字段及错误码
 * https://www.volcengine.com/docs/6444/69728
 */
@Service
public class VolcengineServiceImpl implements VolcengineService {

    private String ACCESS_KEY = "AKLTNzVlZWQwNzYxYTc2NDNmY2JlYzRiMzU5NDkzOGE0ZmQ";
    private String SECRET_KEY = "TVdJMU5XWTRNV1ZpWTJFMk5ETmtNV0pqTWpGaU5Ua3hOelF6TWpjNU0yVQ==";


    /**
     * 人像融合3.6(最新版)
     * 文档：https://www.volcengine.com/docs/6791/1337909
     * 代码：https://github.com/volcengine/volc-sdk-java/blob/main/example/src/main/java/com/volcengine/example/visual/FaceSwapV2Demo.java
     */
    @Override
    public VisualFaceSwapV2Response faceSwapV2(VisualFaceSwapV2 visualFaceSwapV2) {

        IVisualService visualService = VisualServiceImpl.getInstance();
        visualService.setAccessKey(ACCESS_KEY);
        visualService.setSecretKey(SECRET_KEY);

        VisualFaceSwapV2Request req = new VisualFaceSwapV2Request();

        req.setReqKey(visualFaceSwapV2.getReq_key());
        req.setImageUrls(visualFaceSwapV2.getImage_urls());
        req.setFaceType(visualFaceSwapV2.getFace_type());

        ArrayList<VisualFaceSwapV2Request.MergeInfos> mergeInfosList= new ArrayList<>();
        VisualFaceSwapV2Request.MergeInfos mergeInfos=new VisualFaceSwapV2Request.MergeInfos();
        mergeInfos.setLocation(1);
        mergeInfos.setTemplate_location(1);
        mergeInfosList.add(mergeInfos);
        req.setMergeInfos(mergeInfosList);
        req.setDoRisk(false);
        req.setSourceSimilarity("1");
        try {
            VisualFaceSwapV2Response response = visualService.faceSwapV2(req);
            System.out.println(JSON.toJSONString(response));
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            throw new CustException(e.getMessage());
        }
    }


    /**
     * 视频-人脸融合-同步
     * 文档：https://www.volcengine.com/docs/6792/145429
     * 代码：https://github.com/volcengine/volc-sdk-java/blob/main/example/src/main/java/com/volcengine/example/visual/FaceFusionMovieSyncDemo.java
     */
    @Override
    public VisualFaceFusionMovieSyncResponse faceFusionMovieSync(VisualFaceFusionMovie visualFaceFusionMovie) {

        IVisualService visualService = VisualServiceImpl.getInstance();
        visualService.setAccessKey(ACCESS_KEY);
        visualService.setSecretKey(SECRET_KEY);

        // 视频人脸融合同步接口
        VisualFaceFusionMovieSyncRequest req = new VisualFaceFusionMovieSyncRequest();

        /*
            按量计费-标准版 facefusionmovie_usage
            按量计费-高级版 3.4版本取固定值 facefusionmovie_standard_v2_usage
            按量计费-高级版 3.6版本取固定值 facefusionmovie_standard_v3_usage
            QPS计费-高级版 3.4版本取固定值 facefusionmovie_standard_v2
            QPS计费-高级版 3.6版本取固定值 facefusionmovie_standard_v3
            QPS计费2.X facefusionmovie_standard
         */
        req.setReqKey("facefusionmovie_usage");

        // -- 入参 -------------------------------------------------------------------
        req.setVideo_url(visualFaceFusionMovie.getVideo_url());
        System.out.println("video_url = " + visualFaceFusionMovie.getVideo_url());

        String image_urls = StrUtil.join(",", visualFaceFusionMovie.getImage_urls());
        System.out.println("image_urls = " + image_urls);
        req.setImage_url(image_urls);

        if (visualFaceFusionMovie.getRef_img_urls() != null && !visualFaceFusionMovie.getRef_img_urls().isEmpty()) {
            String ref_img_urls = StrUtil.join(",", visualFaceFusionMovie.getRef_img_urls());
            System.out.println("ref_img_urls = " + ref_img_urls);
            req.setRefImgUrl(ref_img_urls);
        }
        // --------------------------------------------------------------------------

        req.setEnableFaceBeautify(true);

        try {
            VisualFaceFusionMovieSyncResponse response = visualService.faceFusionMovieSync(req);
            System.out.println(JSON.toJSONString(response));

            return response;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            throw new CustException(e.getMessage());
        }
    };

    /**
     * 视频-人脸融合-异步
     * 代码：https://github.com/volcengine/volc-sdk-java/blob/main/example/src/main/java/com/volcengine/example/visual/FaceFusionMovieDemo.java
     */
    @Override
    public VisualFaceFusionMovieSubmitTaskResponse faceFusionMovie(VisualFaceFusionMovie visualFaceFusionMovie) {

        IVisualService visualService = VisualServiceImpl.getInstance();
        visualService.setAccessKey(ACCESS_KEY);
        visualService.setSecretKey(SECRET_KEY);

        //提交任务
        VisualFaceFusionMovieSubmitTaskRequest req = new VisualFaceFusionMovieSubmitTaskRequest();

        /*
            按量计费-标准版 facefusionmovie_usage
            按量计费-高级版 3.4版本取固定值 facefusionmovie_standard_v2_usage
            按量计费-高级版 3.6版本取固定值 facefusionmovie_standard_v3_usage
            QPS计费-高级版 3.4版本取固定值 facefusionmovie_standard_v2
            QPS计费-高级版 3.6版本取固定值 facefusionmovie_standard_v3
            QPS计费2.X facefusionmovie_standard
         */
//        req.setReqKey("facefusionmovie_usage");
        req.setReqKey("facefusionmovie_standard_v3_usage");

        // -- 入参 -------------------------------------------------------------------
        req.setVideoUrl(visualFaceFusionMovie.getVideo_url());
        System.out.println("video_url = " + visualFaceFusionMovie.getVideo_url());

        String image_urls = StrUtil.join(",", visualFaceFusionMovie.getImage_urls());
        System.out.println("image_urls = " + image_urls);
        req.setImageUrl(image_urls);

        if (visualFaceFusionMovie.getRef_img_urls() != null && !visualFaceFusionMovie.getRef_img_urls().isEmpty()) {
            String ref_img_urls = StrUtil.join(",", visualFaceFusionMovie.getRef_img_urls());
            System.out.println("ref_img_urls = " + ref_img_urls);
            req.setRefImgUrl(ref_img_urls);
        }
        // --------------------------------------------------------------------------

//        req.setSourceSimilarity("");

        //水印信息
//        VisualFaceFusionMovieSubmitTaskRequest.LogoInfo  logoInfo= new VisualFaceFusionMovieSubmitTaskRequest.LogoInfo();
//        logoInfo.setAddlogo(true);
//        logoInfo.setPosition(2);
//        logoInfo.setLanguage(0);
//        logoInfo.setOpacity(0.9);
//        req.setLogoInfo(logoInfo);
        //3.x版本参数
//        req.setEnableFaceBeautify(false);
//        req.setSharpening(0);
//        req.setWhitening(0);
//        req.setDermabrasion(0);
//        req.setGpen(1);

        try {
            VisualFaceFusionMovieSubmitTaskResponse response = visualService.faceFusionMovieSubmitTask(req);
            System.out.println(JSON.toJSONString(response));
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            throw new CustException(e.getMessage());
        }


    }

    // 视频-人脸融合-异步-查询
    @Override
    public VisualFaceFusionMovieGetResultResponse faceFusionMovieGetResult(String task_id) {

        IVisualService visualService = VisualServiceImpl.getInstance();
        visualService.setAccessKey(ACCESS_KEY);
        visualService.setSecretKey(SECRET_KEY);

        // 查询任务
        VisualFaceFusionMovieGetResultRequest req = new VisualFaceFusionMovieGetResultRequest();

        /*
            按量计费-标准版 facefusionmovie_usage
            按量计费-高级版 3.4版本取固定值 facefusionmovie_standard_v2_usage
            按量计费-高级版 3.6版本取固定值 facefusionmovie_standard_v3_usage
            QPS计费-高级版 3.4版本取固定值 facefusionmovie_standard_v2
            QPS计费-高级版 3.6版本取固定值 facefusionmovie_standard_v3
            QPS计费2.X facefusionmovie_standard
         */
        req.setReqKey("facefusionmovie_usage");

        req.setTaskId(task_id);

        try {
            VisualFaceFusionMovieGetResultResponse response = visualService.faceFusionMovieGetResult(req);
            System.out.println(JSON.toJSONString(response));
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            throw new CustException(e.getMessage());
        }
    }

}
