package com.xuqm.base.di.module;

import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.xuqm.base.App;
import com.xuqm.base.di.interceptor.HttpLogger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Cookie;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

@Module
public class NetworkModule {
    private String BaseUrl = "https://xuqinmin.com/";
    private final List<Interceptor> interceptor = new ArrayList<>();

    public NetworkModule() {
    }

    public NetworkModule(String baseUrl, Interceptor... interceptor) {
        BaseUrl = baseUrl;
        this.interceptor.clear();
        this.interceptor.addAll(Arrays.asList(interceptor));
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient okHttpClient) {

        return new Retrofit.Builder()
                .baseUrl(BaseUrl)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient(HttpLoggingInterceptor httpLoggingInterceptor, PersistentCookieJar persistentCookieJar) {

        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .pingInterval(5, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS);
//        builder.addNetworkInterceptor(httpLoggingInterceptor);
        if (0 != interceptor.size()) {
            for (Interceptor interceptor1 : this.interceptor) {
                builder.addInterceptor(interceptor1);
            }
        }

        return builder.cookieJar(persistentCookieJar)
                .build();
    }

    @Provides
    @Singleton
    HttpLoggingInterceptor provideHttpLoggingInterceptor() {
        return new HttpLoggingInterceptor(new HttpLogger()).setLevel(HttpLoggingInterceptor.Level.BASIC);

    }

    @Provides
    @Singleton
    PersistentCookieJar providePersistentCookieJar(SharedPrefsCookiePersistor sharedPrefsCookiePersistor) {
        return new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor);
    }

    @Provides
    @Singleton
    SharedPrefsCookiePersistor provideSharedPrefsCookiePersistor() {
        return new SharedPrefsCookiePersistor(App.getInstance());
    }

    @Provides
    @Singleton
    List<Cookie> provideCookies(SharedPrefsCookiePersistor sharedPrefsCookiePersistor) {
        return sharedPrefsCookiePersistor.loadAll();
    }

}
