package com.cooleshow.base.data.net;


import android.util.Log;

import com.cooleshow.base.BuildConfig;
import com.cooleshow.base.common.BaseApplication;
import com.cooleshow.base.utils.NetworkUtil;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClientUpFile {

    private static final String API_HOST = BuildConfig.BASE_SERVER_URL;
    private static RetrofitClientUpFile instance;
    private static OkHttpClient okHttpClient;
    private static Retrofit retrofit;

    private RetrofitClientUpFile() {

//        ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(BaseApplication.getApplication()));

        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(600, TimeUnit.SECONDS)
                .readTimeout(600, TimeUnit.SECONDS)
//                .cookieJar(cookieJar)
                .cache(createMCache())
                .retryOnConnectionFailure(true)//错误重连
                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getTrustManager())
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                .addInterceptor(new CommonInterceptor())
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(API_HOST)
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())   // 使用 gson 解析器解析 json
                .client(okHttpClient)
                .build();

    }


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

    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public Retrofit getRetrofit() {
        return retrofit;
    }

    private static Cache createMCache() {
        File cacheFile = new File(String.valueOf(BaseApplication.context.getCacheDir()));
        return new Cache(cacheFile, 1024 * 1024 * 60);
    }

    private Interceptor cacheInterceptor() {
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();

                if (!NetworkUtil.isNetworkAvailable(BaseApplication.context)) {
                    request = request.newBuilder()
                            //强制使用缓存
                            .cacheControl(CacheControl.FORCE_CACHE)
                            .build();
                }

                Response response = chain.proceed(request);

                if (NetworkUtil.isNetworkAvailable(BaseApplication.context)) {
                    //有网的时候读接口上的@Headers里的配置，你可以在这里进行统一的设置
                    String cacheControl = request.cacheControl().toString();
                    Log.i("has network", "cacheControl=" + cacheControl);
                    return response.newBuilder()
                            .header("Cache-Control", cacheControl)
                            .removeHeader("Pragma")
                            .build();
                } else {
                    int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                    Log.i("network error ", "maxStale=" + maxStale);
                    return response.newBuilder()
                            .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                            .removeHeader("Pragma")
                            .build();
                }

            }
        };
    }




}