package cn.qinys.platform.mobile.util;

import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.utils.Constants;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class QwenImage {

    static {
        // 以下为华北2（北京）地域的URL，各地域的URL不同。
        Constants.baseHttpApiUrl = "https://dashscope.aliyuncs.com/api/v1";
    }

    // 新加坡和北京地域的API Key不同。获取API Key：https://help.aliyun.com/zh/model-studio/get-api-key
    static String apiKey = "sk-a52acbfc944e495ead45b1c54b5659fb";

    /**
     * String msg = """
     * 我的名字叫400块，是一只5岁的英短小猫，我喜欢吃鸡肉冻干
     * 我现在在一个许愿树下许愿，希望我身体健康，天天都有冻干吃，吃多了也不长胖
     * """;
     *
     * @param msg
     * @return
     */
    public static String call(String msg) {
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
                .content(List.of(Collections.singletonMap("text", msg))).build();
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("watermark", false);
        parameters.put("prompt_extend", true);
        parameters.put("negative_prompt", "低分辨率，低画质，肢体畸形，手指畸形，画面过饱和，蜡像感，人脸无细节，过度光滑，画面具有AI感。构图混乱。文字模糊，扭曲。");
        parameters.put("size", "720*720");

        MultiModalConversationParam param = MultiModalConversationParam.builder()
                .apiKey(apiKey)
                .model("qwen-image-2.0-pro")
                .messages(Collections.singletonList(userMessage))
                .parameters(parameters)
                .build();
        try {
            MultiModalConversationResult result = conv.call(param);
            return result.getOutput().getChoices().getFirst().getMessage().getContent().getFirst().get("image").toString();
        } catch (Exception e) {
            log.error("get QwenImage error", e);
        }
        return null;
    }


}