package cn.dankal.basiclib.api;


import java.util.LinkedHashMap;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import cn.dankal.basiclib.domain.HttpLoggingInterceptor;
import cn.dankal.basiclib.domain.TokenInterceptor;
import cn.dankal.basiclib.util.Logger;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.fastjson.FastJsonConverterFactory;

import static cn.dankal.basiclib.DankalApplication.isDev;

/**
 * api地址
 *
 * @author vane
 * @since 2018/3/24
 */

public class BaseApi {
    public static final String BASE_URL = isDev ? "http://tp.lzdu.com/api/"
            : "http://tp.lzdu.com/api/";

    public static final String OTHER_BASE_URL="http://gd21262648.imwork.net:30110/";

    private static LinkedHashMap<String, Retrofit> linkedHashMap = new LinkedHashMap<>(3);


    private BaseApi() {

    }

    public static Retrofit getRetrofit() {
        if (linkedHashMap.isEmpty() || !linkedHashMap.containsKey(BASE_URL)) {
            synchronized (BaseApi.class) {
                if (linkedHashMap.isEmpty() || !linkedHashMap.containsKey(BASE_URL)) {
                    linkedHashMap.put(BASE_URL, new Retrofit.Builder().addConverterFactory(FastJsonConverterFactory.create())
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .client(getOkhttpInstance())
                            .baseUrl(BASE_URL)
                            .build());
                }
            }
        }
        return linkedHashMap.get(BASE_URL);
    }

    public static Retrofit getRetrofit(String baseurl) {
        if (linkedHashMap.isEmpty() || !linkedHashMap.containsKey(baseurl)) {
            synchronized (BaseApi.class) {
                if (linkedHashMap.isEmpty() || !linkedHashMap.containsKey(baseurl)) {
                    linkedHashMap.put(baseurl, new Retrofit.Builder()
                            //.addConverterFactory(FastJsonConverterFactory.create())
//                .addConverterFactory(GsonConverterFactory.create(getGson()))
                            .addConverterFactory(FastJsonConverterFactory.create())
                            .client(getOkhttpInstance())
                            .baseUrl(baseurl)
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .build());
                }
            }
        }
        return linkedHashMap.get(baseurl);
    }

    public static OkHttpClient getOkhttpInstance() {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Logger.e(message);
            }
        });
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient
                okHttpClient = new OkHttpClient.Builder().readTimeout(7000, TimeUnit.MILLISECONDS)
                .connectTimeout(7000, TimeUnit.MILLISECONDS)
                .addInterceptor(new TokenInterceptor())
                .addInterceptor(httpLoggingInterceptor)
                .hostnameVerifier(new HostnameVerifier() {
                    /**
                     * Verify that the host name is an acceptable match with
                     * the server's authentication scheme.
                     *
                     * @param hostname the host name
                     * @param session  SSLSession used on the connection to host
                     * @return true if the host name is acceptable
                     */
                    //忽略证书
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                })
                .build();
        return okHttpClient;
    }

}
