package com.hzya.frame.plugin.lets.util.pushData;

import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.hzya.frame.plugin.lets.constant.OverallConstant;
import com.hzya.frame.plugin.lets.constant.ProfilesActiveConstant;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

/**
 * 通过中台推送U8C推送工具类.
 */
@Component
public class PushU8CByApiCode {

//    private static String appId = "800037";
//    private static String publicKey = "ZJYAWb7lhAUTYqekPkU+uHJv1/ObJxb7dT7sD8HPRDGAgyhCe7eDIk+3zDUT+v578prj";//用安中台
//    private static String secretKey = "fviZnLBsQUAGF8w8FSOdJi7XlIm/XAZclMxRagDLfTyJFlvnIBF3w66Hrpfzs8cYj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=";//用安中台

    /**
     * 发起post请求，传递json类型
     *
     * @param apicode 地址
     * @param data    请求参数
     * @return 返回消息
     */
    public ZTResult pushByPK(String apicode, String data) throws Exception {
        Assert.notNull(apicode, "请求地址不能为空");
        Assert.notNull(data, "请求参数不能为空(需要JSON格式)");

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(OverallConstant.getOverAllValue("u8c_url"));
        httpPost.setHeader("Content-Type", "application/json");
        //u8c
        httpPost.setHeader("usercode", OverallConstant.getOverAllValue("u8cApiUsercodePK"));
        httpPost.setHeader("password", OverallConstant.getOverAllValue("u8cApiPasswordPK"));
        httpPost.setHeader("trantype", OverallConstant.getOverAllValue("u8cApiTrantypePK"));
        httpPost.setHeader("system", OverallConstant.getOverAllValue("u8cApiSystemPK"));
        httpPost.setHeader("needStackTrace", OverallConstant.getOverAllValue("u8cApiNeedStackTracePK"));
        //中台
        httpPost.setHeader("publicKey", OverallConstant.getOverAllValue("publicKey"));
        httpPost.setHeader("secretKey", OverallConstant.getOverAllValue("secretKey"));
        //app
        httpPost.setHeader("appId", OverallConstant.getOverAllValue("appId"));
        httpPost.setHeader("apicode", apicode);
        //data
        httpPost.setEntity(new StringEntity(data, "UTF-8"));
        // 配置请求的超时设置
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(600000)  // 连接超时时间: 600秒
                .setConnectionRequestTimeout(600000) // 请求超时时间: 600秒
                .setSocketTimeout(600000)  // 数据读取超时时间: 600秒
                .build();
        httpPost.setConfig(requestConfig);
        CloseableHttpResponse response = httpclient.execute(httpPost);

        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
        ZTResult ztResult = JSON.parseObject(result).toJavaObject(ZTResult.class);

        return ztResult;
    }

    /**
     * 发起post请求，传递json类型
     * <p>
     * u8c code模式。
     *
     * @param apicode
     * @param data    请求参数
     * @return 返回消息
     */
    public ZTResult pushByCode(String apicode, String data) throws Exception {
        Assert.notNull(apicode, "请求地址不能为空");
        Assert.notNull(data, "请求参数不能为空(需要JSON格式)");

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(OverallConstant.getOverAllValue("u8c_url"));
        httpPost.setHeader("Content-Type", "application/json");
        //u8c
        httpPost.setHeader("usercode", OverallConstant.getOverAllValue("u8cApiUsercodeCode"));
        httpPost.setHeader("password", OverallConstant.getOverAllValue("u8cApiPasswordCode"));
        httpPost.setHeader("trantype", OverallConstant.getOverAllValue("u8cApiTrantypeCode"));
        httpPost.setHeader("system", OverallConstant.getOverAllValue("u8cApiSystemCode"));
        httpPost.setHeader("needStackTrace", OverallConstant.getOverAllValue("u8cApiNeedStackTraceCode"));
        //中台
        httpPost.setHeader("publicKey", OverallConstant.getOverAllValue("publicKey"));
        httpPost.setHeader("secretKey", OverallConstant.getOverAllValue("secretKey"));
        //app
        httpPost.setHeader("appId", OverallConstant.getOverAllValue("appId"));
        httpPost.setHeader("apicode", apicode);
        //data
        httpPost.setEntity(new StringEntity(data, "UTF-8"));

        // 配置请求的超时设置
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(600000)  // 连接超时时间: 600秒
                .setConnectionRequestTimeout(600000) // 请求超时时间: 600秒
                .setSocketTimeout(600000)  // 数据读取超时时间: 600秒
                .build();
        httpPost.setConfig(requestConfig);
        CloseableHttpResponse response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
        ZTResult ztResult = JSON.parseObject(result).toJavaObject(ZTResult.class);

        return ztResult;
    }
}
