package org.jeecg.common.util.shumai;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpUtils {
    private static final String DEFAULT_CHARSET = "UTF-8";

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class);

    private static PoolingHttpClientConnectionManager connectionManager;

    private static HttpClient client;

    private HttpUtils() {
    }

    static {
        try {
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
            SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(sslContext,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());
            Registry<ConnectionSocketFactory> socketFactoryRegistry =
                    RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslSf).register("http",
                            PlainConnectionSocketFactory.getSocketFactory()).build();
            connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            connectionManager.setMaxTotal(50);
            connectionManager.setDefaultMaxPerRoute(25);
            client = getSSLHttpClient();
        } catch(NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
            LOGGER.error("HttpUtils class initialize error|message={}", e.getMessage(), e);
        }
    }

    public static CloseableHttpClient getHttpClient() {
        return HttpClients.custom().setConnectionManager(connectionManager).build();
    }

    public static CloseableHttpClient getSSLHttpClient() {
        // 使用TrustSelfSignedStrategy容许Self Signed的证书
        SSLContext sslContext = null;
        try {
            sslContext = SSLContextBuilder.create().loadTrustMaterial(new TrustSelfSignedStrategy()).build();
        } catch (Exception e) {
            LOGGER.warn("method getSSLHttpClient error,message={}", e.getMessage(), e);
        }
        // 不做服务器名验证，这是可选的, 如果需要对服务器名做验证，可以去掉该配置
        HostnameVerifier allowAllHosts = new NoopHostnameVerifier();
        // 使用SSLContext创建SSL Socket Factory
        SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, allowAllHosts);
        // 使用HttpClient Factory创建HttpClient
        return HttpClients.custom().setSSLSocketFactory(connectionFactory).build();
    }

    /**
     * get 请求
     */
    public static String get(String uri, Map<String, String> headers, Map<String, Object> data) {
        HttpClient client = getHttpClient();
        List<NameValuePair> params = new ArrayList<>();
        if (data != null) {
            for(Map.Entry entry : data.entrySet()) {
                params.add(new BasicNameValuePair(entry.getKey() + "", entry.getValue() + ""));
            }
        }
        try {
            String str = EntityUtils.toString(new UrlEncodedFormEntity(params, DEFAULT_CHARSET));
            HttpGet get = new HttpGet(uri + "?" + str);
            if(headers != null) {
                for(Map.Entry<String, String> entry : headers.entrySet()) {
                    get.addHeader(entry.getKey(), entry.getValue());
                }
            }
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8").trim();
        } catch (Exception e) {
            LOGGER.error("method get error|message={}", e.getMessage(), e);
            return null;
        }
    }

    /**
     * post 发送表单请求
     */
    public static String postForm(String uri, Map<String, String> headers, Map<String, Object> data) {
        HttpClient client = getHttpClient();
        HttpPost post = new HttpPost(uri);
        List<NameValuePair> form = new ArrayList<>();
        for(Map.Entry entry : data.entrySet()) {
            form.add(new BasicNameValuePair(entry.getKey() + "", entry.getValue() + ""));
        }
        if(headers != null) {
            for(Map.Entry<String, String> entry : headers.entrySet()) {
                post.addHeader(entry.getKey(), entry.getValue());
            }
        }
        try {
            post.setEntity(new UrlEncodedFormEntity(form, DEFAULT_CHARSET));
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8").trim();
        } catch (Exception e) {
            LOGGER.error("method postForm error|message={}", e.getMessage(), e);
            return null;
        }
    }

    /**
     * post 发送json报文请求
     * content 请求参数json字符串
     */
    public static String postData(String uri, Map<String, String> headers, String content) {
        HttpClient client = getHttpClient();
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity(content, DEFAULT_CHARSET));
        if(headers != null) {
            for(Map.Entry<String, String> entry : headers.entrySet()) {
                post.addHeader(entry.getKey(), entry.getValue());
            }
        }
        return postCommon(client, post);
    }



    /**
     * TLS/SSL 加密方式发送json报文
     * @param uri
     * @param headers
     * @param content
     * @return
     */
    public static String postSSLData(String uri, Map<String, String> headers, String content) {
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity(content, DEFAULT_CHARSET));
        if(headers != null) {
            for(Map.Entry<String, String> entry : headers.entrySet()) {
                post.addHeader(entry.getKey(), entry.getValue());
            }
        }
        return postCommon(client, post);
    }

    private static String postCommon(HttpClient client, HttpPost post) {
        try {
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8").trim();
        } catch(Exception e) {
            LOGGER.error("method postData error|message={}", e.getMessage(), e);
            return null;
        }
    }
}
