import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import okio.BufferedSource;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeUnit;


@Slf4j
public class TestRot {


    public static void testRobotRwq() throws IOException {


        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .followRedirects(true)  // 允许自动重定向
                .followSslRedirects(true)
                .build();


/*        JSONObject params = new JSONObject();
        params.put("question", question);*/

        String requestStr = "{\n" +
                "    \"inputs\": {},\n" +
                "    \"query\": \"周杰伦是谁?\",\n" +
                "    \"response_mode\": \"streaming\",\n" +
                "    \"conversation_id\": \"\",\n" +
                "    \"user\": \"abc-123\",\n" +
                "    \"files\": []\n" +
                "}";


        RequestBody requestBody = RequestBody.create(requestStr, MediaType.parse("application/json; charset=utf-8"));

        // 封装请求头
        Headers headers = new Headers.Builder()
                .set("Content-Type", "application/json")
                .set("Accept", "*/*")
                .set("Authorization", "Bearer app-ritPdgkz8bsrfzpcJdSTkUF5")
                .build();

        Request request = new Request.Builder()
                //.url("http://localhost:18006/chat/base")
                // .url("http://robot.scsmile.cn/v1/chat-messages")
                .url("https://agent.wx3.com.cn/v1/chat-messages")
                //.url(".url("https://agent.wx3.com.cn/v1/chat-messages")")
                .headers(headers)

                .post(requestBody)
                .build();


/*        Response response = client.newCall(request).execute();
        log.info(response.code() + "");*/


        Call call = client.newCall(request);
        // 4. 监听回调
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.error("进入 onFailure方法 {}", e.getMessage(), e);
                // sseEmitter.completeWithError(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                log.info(JSON.toJSONString(response));
                if (response.isSuccessful()) {
                    String chunkMessage = "";
                    BufferedSource source = response.body().source();
                    while (!source.exhausted()) {
                        chunkMessage = source.readUtf8Line();
                        if (StringUtils.isBlank(chunkMessage)) {
                            continue;
                        }

                 /*       JSONObject jsonObject = JSONObject.parseObject(chunkMessage);
                        if (null != jsonObject && null != jsonObject.getJSONObject("data")) {
                            String answer = jsonObject.getJSONObject("data").getString("answer");
                            //   sseEmitter.send(answer);
                        }*/


                        log.info("chunkMessage:{}", chunkMessage);

                    }

                } else {
                    // log.error("onResponse 方法请求失败 {}", response.message());
                    // TODO 重新发起请求
                }
            }
        });

    }


    public static void main(String[] args) throws IOException {


        testRobotRwq();

    }

}
