import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.SecureRandom;

import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class OkHttpUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(OkHttpUtils.class);
    private static final OkHttpClient okHttpClient;


    private OkHttpUtils() {
    }


    /**
     * 初始化okHttpClient,并且允许https访问
     */
    static {
        TrustManager[] trustManagers = buildTrustManagers();
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .sslSocketFactory(createSSLSocketFactory(trustManagers), (X509TrustManager) trustManagers[0])
                .hostnameVerifier((hostName, session) -> true)
                .retryOnConnectionFailure(true)
                .build();
    }


    /**
     * 表单请求
     *
     * @param paramMapObj 参数
     * @return string
     */
    public static String postForm(String url, Map<String, String> paramMapObj) {
        FormBody.Builder formBody = new FormBody.Builder();
        if (paramMapObj != null) {
            paramMapObj.forEach(formBody::add);
        }
        RequestBody requestBody = formBody.build();
        Request.Builder request = new Request.Builder()
                .addHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8")
                .post(requestBody).url(url);
        try (Response response = okHttpClient.newCall(request.build()).execute()) {
            boolean successful = response.isSuccessful();
            Assert.isTrue(successful, "请求返回失败");
            assert response.body() != null;
            return response.body().string();
        } catch (Exception e) {
            LOGGER.info("请求异常{},详情{}", url, e.getStackTrace());
            throw new RuntimeException(e);
            //  return "请求失败:" + e.getMessage();
        }

    }

    /**
     * json参数请求
     *
     * @param paramJson 参数值
     * @return
     */
    public static String postJson(String url, String paramJson) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), paramJson);
        Request.Builder request = new Request.Builder().post(requestBody).url(url);
        try (Response response = okHttpClient.newCall(request.build()).execute()) {
            boolean successful = response.isSuccessful();
            assert response.body() != null;
            return response.body().string();
        } catch (Exception e) {
            LOGGER.info("请求异常{},详情{}", e.getMessage(), e.getStackTrace());
            return "请求失败:" + e.getMessage();
        }
    }

    /**
     * 异步json请求
     *
     * @param url
     * @param paramJson
     * @param callBack
     */
    public static void postJsonAsync(String url, String paramJson, ICallBack callBack) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), paramJson);
        Request.Builder request = new Request.Builder().post(requestBody).url(url);
        okHttpClient.newCall(request.build()).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.onFailure(call, e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                assert response.body() != null;
                callBack.onSuccessful(call, response.body().string());
            }
        });

    }


    /**
     * 生成安全套接字工厂,用于https请求的证书跳过
     *
     * @return
     */
    private static SSLSocketFactory createSSLSocketFactory(TrustManager[] trustAllCerts) {
        SSLSocketFactory ssfFactory = null;
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
            ssfFactory = sc.getSocketFactory();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ssfFactory;
    }

    private static TrustManager[] buildTrustManagers() {
        return new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }
                }
        };
    }

    /**
     * 自定义一个接口回调
     */
    public interface ICallBack {

        void onSuccessful(Call call, String data);

        void onFailure(Call call, String errorMsg);

    }