package org.dromara.resource.junziqian.openai;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

public class ChatMessage
{
    public JSONArray buildMessage(final String systemContent, final String userPreContent, final String assistantPreContent, final String imageUrl, final String... userContent) {
        final JSONArray message = new JSONArray();
        final JSONObject system = new JSONObject();
        system.set("role", (Object)"system");
        system.set("content", (Object)systemContent);
        message.add((Object)system);
        if (StrUtil.isNotBlank((CharSequence)userPreContent)) {
            final JSONObject preUser = new JSONObject();
            preUser.set("role", (Object)"user");
            preUser.set("content", (Object)userPreContent);
            message.add((Object)preUser);
        }
        if (StrUtil.isNotBlank((CharSequence)assistantPreContent)) {
            final JSONObject assistant = new JSONObject();
            assistant.set("role", (Object)"assistant");
            assistant.set("content", (Object)assistantPreContent);
            message.add((Object)assistant);
        }
        if (StrUtil.isBlank((CharSequence)imageUrl)) {
            final JSONObject user = new JSONObject();
            final JSONArray contents = new JSONArray();
            user.set("role", (Object)"user");
            for (final String s : userContent) {
                final JSONObject content = new JSONObject();
                content.set("type", (Object)"text");
                content.set("text", (Object)s);
                contents.add((Object)content);
            }
            user.set("content", (Object)contents);
            message.add((Object)user);
        }
        else {
            final JSONObject image = new JSONObject();
            image.set("role", (Object)"user");
            final JSONArray imageContentArray = new JSONArray();
            final JSONObject content2 = new JSONObject();
            content2.set("type", (Object)"image_url");
            content2.set("image_url", (Object)imageUrl);
            imageContentArray.add((Object)content2);
            for (final String s2 : userContent) {
                final JSONObject textContent = new JSONObject();
                textContent.set("type", (Object)"text");
                textContent.set("text", (Object)s2);
                imageContentArray.add((Object)textContent);
            }
            image.set("content", (Object)imageContentArray);
            message.add((Object)image);
        }
        return message;
    }
}
