package com.cslg.ppa.service.commom;

import com.alibaba.fastjson.JSONObject;
import com.cslg.ppa.common.utils.DataUtils;
import com.cslg.ppa.dto.OAMessageDTO;
import com.google.gson.Gson;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
@Slf4j
@Service
public class DifyService {
    @Value("${DIFY.url}")
    private String url;
    @Value("${DIFY.getAbstractKey}")
    private String getAbstractKey;
    @Value("${DIFY.articleClassifyKey}")
    private String articleClassifyKey;

    public String getCondensedAbstract(String text) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("content", text);
        OAMessageDTO oaMessageDTO = new OAMessageDTO();
        oaMessageDTO.setInputs(map);
        oaMessageDTO.setResponseMode("blocking");
        oaMessageDTO.setUser("1");

        String param = new Gson().toJson(oaMessageDTO);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(600, TimeUnit.SECONDS)
                .writeTimeout(600, TimeUnit.SECONDS)
                .readTimeout(600, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(url + "workflows/run")
                .addHeader("Authorization", "Bearer " + getAbstractKey)
                .post(requestBody)
                .build();
        String res = Objects.requireNonNull(client.newCall(request).execute().body()).string();
        JSONObject jsonObject = JSONObject.parseObject(res);
        String dataStr = jsonObject.get("data").toString();
        JSONObject dataObject = JSONObject.parseObject(dataStr);
        String outPuts = dataObject.get("outputs").toString();
        JSONObject jsonObject1 = JSONObject.parseObject(outPuts);
        String content = jsonObject1.get("text").toString();
        return DataUtils.unicodeDecode(content);
    }

    public String getArticleClassify(String text) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("content", text);
        OAMessageDTO oaMessageDTO = new OAMessageDTO();
        oaMessageDTO.setInputs(map);
        oaMessageDTO.setResponseMode("blocking");
        oaMessageDTO.setUser("1");

        String param = new Gson().toJson(oaMessageDTO);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(600, TimeUnit.SECONDS)
                .writeTimeout(600, TimeUnit.SECONDS)
                .readTimeout(600, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(url + "workflows/run")
                .addHeader("Authorization", "Bearer " + articleClassifyKey)
                .post(requestBody)
                .build();
        String res = Objects.requireNonNull(client.newCall(request).execute().body()).string();
        JSONObject jsonObject = JSONObject.parseObject(res);
        String dataStr = jsonObject.get("data").toString();
        JSONObject dataObject = JSONObject.parseObject(dataStr);
        String outPuts = dataObject.get("outputs").toString();
        JSONObject jsonObject1 = JSONObject.parseObject(outPuts);
        String content = jsonObject1.get("text").toString();
        return DataUtils.unicodeDecode(content);
    }
}
