package com.youchain.utils;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
public class HttpPostUtil {
    public static String sendPostReq(String api_url, String request) {
        System.out.println("发送api:"+api_url);
        System.out.println("接收request:"+request);
        InputStream instr = null;
        String str = "";
        try {
            URL url = new URL(api_url);
            URLConnection urlCon = url.openConnection();
            urlCon.setConnectTimeout(3000);
            byte[] xmlData = request.getBytes();
            urlCon.setDoOutput(true);
            urlCon.setDoInput(true);
            urlCon.setUseCaches(false);
            urlCon.setRequestProperty("content-Type", "application/json");
            urlCon.setRequestProperty("charset", "UTF-8");
            urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length));
            DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());
            printout.write(xmlData);
            printout.flush();
            printout.close();
            instr = urlCon.getInputStream();
            byte[] bis = IOUtils.toByteArray(instr);
            String ResponseString = new String(bis, "UTF-8");
            if ((ResponseString == null) || ("".equals(ResponseString.trim()))) {
                System.out.println("返回空");
            }
            str = ResponseString;
            System.out.println("返回报文:"+str);
        } catch (Exception e) {
            log.info("接口异常");
            // throw new Error(e.getMessage());
            /*str = "{\n" +
                " \"data\": null,\n" +
                " \"code\": \"0\",\n" +
                " \"message\": null,\n" +
                " \"success\": true\n" +
                "}";*/
        }
        return str;
    }

    public static String sendPostReq(String api_url, String request, String token) {
        System.out.println("api:"+api_url);
        System.out.println("request:"+request);
        System.out.println("token:"+token);
        InputStream instr = null;
        String str = "";
        int i=1;
        boolean bit=false;
        while (true) {
            try {
                URL url = new URL(api_url);
                URLConnection urlCon = url.openConnection();
                urlCon.setConnectTimeout(3000);
                byte[] xmlData = request.getBytes();
                urlCon.setDoOutput(true);
                urlCon.setDoInput(true);
                urlCon.setUseCaches(false);
                urlCon.setRequestProperty("token", token);
                urlCon.setRequestProperty("content-Type", "application/json");
                urlCon.setRequestProperty("charset", "UTF-8");
                urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length));
                DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());
                printout.write(xmlData);
                printout.flush();
                printout.close();
                instr = urlCon.getInputStream();
                byte[] bis = IOUtils.toByteArray(instr);
                String ResponseString = new String(bis, "UTF-8");
                if ((ResponseString == null) || ("".equals(ResponseString.trim()))) {
                    System.out.println("返回空");
                }else {
                    JSONObject jsonObject=JSONObject.parseObject(ResponseString);
                    if (jsonObject.getString("code").equals("0")){
                        bit=true;
                    }
                }
                str = ResponseString;
                System.out.println("返回报文:"+str);
            } catch (Exception e) {
                log.info(api_url + "接口异常");
            } finally {
                if (i>3||bit==true){
                    break;
                }
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 跳出循环
                i=i+1;
            }
        }
        return str;
    }
}
