package com.tenlionsoft.baselib.core.retrofit_net;

import com.tenlionsoft.baselib.core.retrofit_net.conver.AreaInterceptor;
import com.tenlionsoft.baselib.core.retrofit_net.conver.BaseUrlInterceptor;

import java.net.ProxySelector;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.internal.proxy.NullProxySelector;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 作者 : Adam on 2018/12/5.
 * 邮箱 : itgaojian@163.com
 * 描述 : Retrofit工具类
 */
public class RetrofitManager {
    //项目Base路径
    /**
     * 超时时间
     */
    public static final int TIMEOUT = 1;
    private static volatile RetrofitManager mInstance;
    private Retrofit mRetrofit;

    public static RetrofitManager getInstance() {
        if (mInstance == null) {
            synchronized (RetrofitManager.class) {
                if (mInstance == null) {
                    mInstance = new RetrofitManager();
                }
            }
        }
        return mInstance;
    }


    private RetrofitManager() {
        initRetrofit();
    }

    /**
     * 初始化Retrofit
     */
    private void initRetrofit() {
        OkHttpClient.Builder builder = getOkBuilder();
        OkHttpClient client = builder.build();
        mRetrofit = new Retrofit.Builder()
            .baseUrl(BaseUrlApi.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
            .client(client)
            .build();
    }

    private OkHttpClient.Builder getOkBuilder() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//        setLocalProxy();
        // 设置超时
        builder.connectTimeout(TIMEOUT, TimeUnit.MINUTES);
        builder.readTimeout(TIMEOUT, TimeUnit.MINUTES);
        builder.writeTimeout(TIMEOUT, TimeUnit.MINUTES);
        //封装公共参数
//        builder.addInterceptor(new AreaInterceptor());
        //builder.addInterceptor(new CommInterceptor());
        //多BaseUrl连接器
        builder.addInterceptor(new BaseUrlInterceptor());
        return builder;
    }

    /*设置本地代理*/
    private void setLocalProxy() {
        Properties prop = System.getProperties();
        prop.setProperty("http.proxyHost", "192.168.0.36");//设置http网络请求的代理
        prop.setProperty("http.proxyPort", "8888");//设置http网络请求的代理
        prop.setProperty("http.nonProxyHosts", "http://1.24.238.149:37210/case/*");//设置http不需要代理的

        prop.setProperty("https.proxyHost", "192.168.0.36");//设置http网络请求的代理
        prop.setProperty("https.proxyPort", "8888");//设置http网络请求的代理

    }

    /*清除本地代理*/
    private void removeLocalProxy() {
        Properties prop = System.getProperties();
        prop.remove("http.proxyHost");//设置http网络请求的代理
        prop.remove("http.proxyPort");//设置http网络请求的代理
        prop.remove("http.nonProxyHosts");//设置http不需要代理的
        prop.remove("https.proxyHost");//设置http网络请求的代理
        prop.remove("https.proxyPort");//设置http网络请求的代理
    }

    /**
     * 创建API
     */
    public <T> T create(Class<T> clazz) {
        return mRetrofit.create(clazz);
    }

}
