package com.ruoyi.utils;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.back.domain.PdProcessData;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpUtil {
    private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);



//    public static String postBody(String postUrl, String body, String uSign, String accessToken) {
    public static String postBody(PdProcessData data) {
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(data.getCompressed());
//            String dataBinary = JSONObject.toJSONString(data.getDataBinary());
            httpPost.setEntity(new StringEntity(data.getDataBinary(), ContentType.APPLICATION_JSON));
            httpPost.setHeader("Youzy-Signature", data.getYouzySignature());
            httpPost.setHeader("sign", data.getSign());
            httpPost.setHeader("eagle-token", data.getEagleToken());
            httpPost.setHeader("u-dfv", data.getuDfv());
            httpPost.setHeader("x-token", data.getxToken());
            httpPost.setHeader("u-token", data.getuToken());
            httpPost.setHeader("u-dfs", data.getuDfs());
            httpPost.setHeader("wToken", data.getwToken());
            httpPost.setHeader("Content-Type", data.getContentType());

            httpPost.setHeader("Host", data.getHost());
            httpPost.setHeader("User-Agent", data.getUserAgent());
            CloseableHttpResponse response = httpClient.execute(httpPost);
            String result = "";
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200 && response != null) {
                HttpEntity httpEntity = response.getEntity();
                // 如果返回的内容不为空
                if (httpEntity != null) {
                    result = EntityUtils.toString(httpEntity);
                }
            }else {
                log.error("result is error ");
            }

            return result;
        } catch (Exception e) {
            log.error("postUrl is {}", data.getCompressed());
            log.error(e.getMessage(), e);
        }
        return "";
    }
}
