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

import com.backendsys.modules.sdk.volcengine.entity.VolcengineImage2VideoParams;
import com.backendsys.modules.sdk.volcengine.service.VolcengineImage2VideoService;
import com.volcengine.ark.runtime.model.content.generation.*;
import com.volcengine.ark.runtime.service.ArkService;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class VolcengineImage2VideoServiceImpl implements VolcengineImage2VideoService {


    static String API_KEY = "ea19ba93-41fa-4bd9-90ec-6b203be84e33";
    static ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
    static Dispatcher dispatcher = new Dispatcher();
    static ArkService service = ArkService.builder()
            .dispatcher(dispatcher)
            .connectionPool(connectionPool)
            .apiKey(API_KEY)
            .build();

    @Override
    public CreateContentGenerationTaskResult image2Video(VolcengineImage2VideoParams params) {
//        String model = "doubao-seedance-1-0-pro-250528";

        List<CreateContentGenerationTaskRequest.Content> contents = new ArrayList<>();

        // 图生视频功能
        // 文本提示词与参数组合
        if (StringUtils.isBlank(params.getText())) {
            params.setText("");
        }

        StringBuilder text = new StringBuilder(params.getText());
        if(StringUtils.isNotBlank(params.getResolution())) {
            text.append(" --rs ").append(params.getResolution());
        }
        if(params.getDuration() != null) {
            text.append(" --dur ").append(params.getDuration());
        }
        if(params.getCamerafixed() != null) {
            if (params.getCamerafixed() == 1) {
                text.append(" --cf true");
            } else {
                text.append(" --cf false");
            }
        }

        contents.add(CreateContentGenerationTaskRequest.Content.builder()
                .type("text")
                .text(text.toString())
                .build());


        // 首帧图片 (若仅需使用文本生成视频功能，可将此部分内容进行注释处理。)
        contents.add(CreateContentGenerationTaskRequest.Content.builder()
                .type("image_url")
                .imageUrl(CreateContentGenerationTaskRequest.ImageUrl.builder()
                        .url(params.getImageUrl())
                        .build())
                .build());
        // 创建视频生成任务
        CreateContentGenerationTaskRequest createRequest = CreateContentGenerationTaskRequest.builder()
                .model(params.getModel())
                .content(contents)
                .build();

        return service.createContentGenerationTask(createRequest);

    }

    @Override
    public GetContentGenerationTaskResponse queryImage2VideoTask(String taskId) {

        GetContentGenerationTaskRequest getRequest = GetContentGenerationTaskRequest.builder()
                .taskId(taskId)
                .build();

        return service.getContentGenerationTask(getRequest);

    }

    @Override
    public ListContentGenerationTasksResponse queryImage2VideoTaskList(String taskIds) {
        List<String> taskIdList = Arrays.asList(taskIds.split( ","));
        ListContentGenerationTasksRequest req = ListContentGenerationTasksRequest.builder()
                .taskIds(taskIdList)
                .pageNum(1)
                .pageSize(taskIdList.size())
                .build();

        return service.listContentGenerationTasks(req);
    }

}
