package com.kfzs.libs.di.http;


import com.kfzs.libs.convert.StringConverterFactory;
import com.kfzs.libs.convert.TConverterFactory;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by guoxin on 2018/3/29.
 */
@Module
public class HttpModule {

//    public static final String BASE_URL = "http://10.8.240.165:7011/";
//    public static final String BASE_URL = "http://10.8.230.17:7011/";

    public static final String BASE_URL = "http://credit.kfzs.com/";


    public static final int CONNECT_TIMEOUT = 15;
    public static final int READ_TIMEOUT = 15;

    @Provides
    @Singleton
    public OkHttpClient provideOkHttpClient(){
//        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//        loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
        OkHttpClient okhttpClient = new OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIMEOUT,TimeUnit.SECONDS)
//                .addInterceptor(new AddPuplicParameIntercept())
//                .addInterceptor(loggingInterceptor)
                .build();
        return okhttpClient;
    }

    @Provides
    @Singleton
    public Retrofit provideRetrofit(OkHttpClient okHttpClient){
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(BASE_URL)
                .addConverterFactory(StringConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
//                .addConverterFactory(TConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit;
    }

    @Provides
    @Singleton
    public HttpService provideHttpService(Retrofit retrofit){
        return retrofit.create(HttpService.class);
    }

}
