package net.northking.cctp.scriptcase.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * 通过http直接调用系统接口的工具类
 *
 * @author kf0841 lwz
 * @date 2023/09/01
 */
public class HttpClientUtils {

    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);

    public static String doPost(String pathUrl, String query, String ak) {
        return doPost(pathUrl, query, ak, null);
    }

    private static String doPost(String pathUrl, String query, String ak, String token) {

        OutputStreamWriter out = null;
        BufferedReader br = null;
        String result = "";
        try {

            URL url = new URL(pathUrl);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("POST");
            conn.setConnectTimeout(40000);
            conn.setReadTimeout(40000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);


            conn.setRequestProperty("accept", "application/json, text/plain, */*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "application/json");

            //开发环境、测试环境和生产环境、ak不一样
            /**
             * 开发环境：
             * ak：dWktdG8tYXV0b3Rlc3QtZGV2
             * Ip：158.1.81.42:8000
             * 测试环境：
             * ak：dWktdG8tYXV0b3Rlc3Qtc3Fh
             * Ip：158.1.81.43:8000
             * 验证环境：
             * ak：dWktdG8tYXV0b3Rlc3QtdWF0
             * Ip：158.1.83.161:8000
             */
            conn.setRequestProperty("Ak", ak);

            System.out.println("ak--->" + ak);

            conn.connect();

            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");

            out.write(query);
            out.flush();

            InputStream inputStream = conn.getInputStream();
            br = new BufferedReader(new InputStreamReader(inputStream));
            String str = "";

            while ((str = br.readLine()) != null) {
                result += str;
            }
            inputStream.close();
            conn.disconnect();

        } catch (Exception e) {
            logger.error("",e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                logger.error("",e);
            }
        }
        return result;
    }

    /**
     * doget 以get方式调用对方接口方法
     *
     * @param pathUrl
     */
    public static String doGet(String pathUrl, String ak) {
        String result = null;
        BufferedReader in = null;
        try {
            URL url = new URL(pathUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("GET");
            connection.setReadTimeout(30000);
            connection.setConnectTimeout(30000);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            connection.setRequestProperty("accept", "/");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "application/json");

            connection.setRequestProperty("Ak", ak);

            connection.connect();
            Map<String, List<String>> map = connection.getHeaderFields();
            for (String key :
                    map.keySet()) {
                System.out.println(key + "------>" + map.get(key));
            }

            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }


        } catch (Exception e) {
            logger.error("",e);
            System.out.println("发送GET请求出现异常：" + e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                logger.error("",e);
            }
        }
        return result;
    }
}
