package com.gem.media.splash.base.crash;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Base64;

import com.or.ange.dot.utils.LLog;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


public class Http {

    private static final String TAG = "DotHttp";

    private Http() {
        throw new UnsupportedOperationException();
    }

    private static void setHttpsFactory(HttpURLConnection conn) {
        if (conn instanceof HttpsURLConnection) {
            HttpsURLConnection httpsConn = (HttpsURLConnection)conn;
            httpsConn.setHostnameVerifier(new SkipHostnameVerifier());

            try {
                httpsConn.setSSLSocketFactory(createSslSocketFactory());
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }
    }

    public static HttpURLConnection post(String uri, Map<String, String> heads, Map<String, String> params, String body) throws IOException {
        URL url;
        if (params != null && params.size() > 0) {
            url = new URL(uri + "?" + convertRequestParamsToString(params));
        } else {
            url = new URL(uri);
        }

        OutputStream outputStream = null;

        while(true) {
            try {
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setAllowUserInteraction(false);
                conn.setConnectTimeout(20000);
                conn.setReadTimeout(20000);
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
                conn.setRequestProperty("Content-Type", "application/octet-stream");
                conn.setRequestProperty("e", "5");
                setHttpsFactory(conn);
                if (!TextUtils.isEmpty(body)) {
                    String encryptBody = Base64.encodeToString(AESEncryptUtil.encrypt2(body), Base64.NO_WRAP);
                    outputStream = conn.getOutputStream();
                    outputStream.write((encryptBody).getBytes("UTF-8"));
                    outputStream.flush();
                }

                int responseCode = conn.getResponseCode();
                LLog.d(TAG, "responseCode  "+responseCode);
                switch (responseCode) {
                    case 200:
                    case 206:
                        HttpURLConnection var25 = conn;
                        return var25;
                    case 301:
                    case 302:
                    case 303:
                    case 307:
                        String location = conn.getHeaderField("Location");
                        url = new URL(location);
                        break;
                    case 416:
                        throw new StopRequestException(416);
                    default:
                        return  null;
                }
            } catch (Exception var21) {
                return null;
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException var20) {
                    }
                }

            }
        }
    }

    public static String convertRequestParamsToString(Map<String, String> params) throws IOException {
        StringBuilder result = new StringBuilder();
        boolean first = true;

        for (String key : params.keySet()) {
            String value = params.get(key);
            if (!TextUtils.isEmpty(value)) {
                if (first) {
                    first = false;
                } else {
                    result.append("&");
                }

                result.append(URLEncoder.encode(key, "UTF-8"));
                result.append("=");
                result.append(URLEncoder.encode(value, "UTF-8"));
            }
        }

        return result.toString();
    }

    private static SSLSocketFactory createSslSocketFactory() throws Exception {
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[]{new SkipX509TrustManager()}, new SecureRandom());
        return context.getSocketFactory();
    }

    public static class StopRequestException extends RuntimeException {
        private final int code;

        public StopRequestException(int code) {
            this.code = code;
        }

        public int getCode() {
            return this.code;
        }
    }

    private static class SkipHostnameVerifier implements HostnameVerifier {
        private SkipHostnameVerifier() {
        }

        @SuppressLint({"BadHostnameVerifier"})
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
    }

    private static class SkipX509TrustManager implements X509TrustManager {
        private SkipX509TrustManager() {
        }

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

        @SuppressLint({"TrustAllX509TrustManager"})
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        @SuppressLint({"TrustAllX509TrustManager"})
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }
    }
}
