package com.hh.hhomc.http;

import android.content.Context;


import com.hh.hhomc.common.Constants;

import java.security.cert.CertificateException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Date: 2022/5/17 0017 11:08
 * Description:
 */
public class HttpManager {

    private static volatile HttpManager instance;
    private Retrofit retrofit;
    Context context;
    private static volatile  OkHttpClient mOkHttpClient;
//    public HttpManager(Context context) {
//        this.context = context;
//    }

    public static HttpManager getInstance(Context context) {
        if (instance == null) {
            synchronized (HttpManager.class) {
                instance = new HttpManager(context);
            }
        }
        return instance;
    }

    public <T> T create(Class<T> service) {
        return retrofit.create(service);
    }


    public HttpManager(Context context) {
        this.context = context;



        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.Url.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(getmOkHttpClient()).build();

    }

    public  OkHttpClient getmOkHttpClient() {
        if(mOkHttpClient==null){
            synchronized (HttpManager.class){
                if(mOkHttpClient==null){
                    //log用拦截器
                    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                    //开发模式记录整个body,否则只记录基本信息如返回200，http协议版本等
                    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                    //https
                    try {
                        // Create a trust manager that does not validate certificate chains
                        final TrustManager[] trustAllCerts = new TrustManager[]{
                                new X509TrustManager() {
                                    @Override
                                    public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                                    }

                                    @Override
                                    public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                                    }

                                    @Override
                                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                        return new java.security.cert.X509Certificate[]{};
                                    }
                                }
                        };

                        // Install the all-trusting trust manager
                        final SSLContext sslContext = SSLContext.getInstance("SSL");
                        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
                        // Create an ssl socket factory with our all-trusting manager
                        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

                        OkHttpClient.Builder builder = new OkHttpClient.Builder();
                        builder.sslSocketFactory(sslSocketFactory);
                        builder.hostnameVerifier(new HttpHostnameVerifier());
                        mOkHttpClient = builder
                                // 服务器debug 网络差 出现几率比较高，导致服务器数据重复,故设置false
                                .retryOnConnectionFailure(true)
                                .connectTimeout(10, TimeUnit.MILLISECONDS)
                                //添加token
//                    .addInterceptor(new CommonParamsInterceptor(context))
                                .addInterceptor(new CommonAddAndHandleInterceptor(context))
                                .addInterceptor(loggingInterceptor)
                                //连接超时时间设置
                                .connectTimeout(20, TimeUnit.SECONDS)
                                //读取超时时间设置
                                .readTimeout(20, TimeUnit.SECONDS)

                                .build();


                    } catch (Exception e) {

                    }
                }
            }
        }
        return mOkHttpClient;
    }

    public OkHttpClient getOkHttpClient() {

        //log用拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();

        //开发模式记录整个body,否则只记录基本信息如返回200，http协议版本等
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        return new OkHttpClient.Builder()
                //添加token
//                .addInterceptor(new CommonParamsInterceptor(context))
                .addInterceptor(new CommonAddAndHandleInterceptor(context))
                .addInterceptor(loggingInterceptor)
                //连接超时时间设置
                .connectTimeout(20, TimeUnit.SECONDS)
                //读取超时时间设置
                .readTimeout(20, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
    }


    public OkHttpClient getOkHttpClientNoInterceptor() {

        //log用拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();

        //开发模式记录整个body,否则只记录基本信息如返回200，http协议版本等
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        return new OkHttpClient.Builder()
                //添加token
//                .addInterceptor(new CommonParamsInterceptor(context))
                .addInterceptor(new CommonAddAndHandleInterceptor(context))
                .addInterceptor(loggingInterceptor)
                //连接超时时间设置
                .connectTimeout(10, TimeUnit.SECONDS)
                //读取超时时间设置
                .readTimeout(10, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
    }


    public Retrofit getRetrofit(OkHttpClient okHttpClient) {
        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(Constants.Url.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(okHttpClient);
        return builder.build();
    }

}
