package info.mschmitt.githubbrowser.network.internal.dagger;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import info.mschmitt.githubbrowser.network.GitHubService;
import info.mschmitt.githubbrowser.network.internal.GitHubServiceInterceptor;
import info.mschmitt.githubbrowser.network.internal.utils.AsyncRxJavaCallAdapterFactory;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author Matthias Schmitt
 */
@Module
public class RetrofitNetworkModule {
    private final boolean mDebug;
    private String mBaseUrl;

    public RetrofitNetworkModule(String baseUrl, boolean debug) {
        mBaseUrl = baseUrl;
        mDebug = debug;
    }

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient() {
        OkHttpClient.Builder builder =
                new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)
                        .readTimeout(10, TimeUnit.SECONDS)
                        .addInterceptor(new GitHubServiceInterceptor());
        if (mDebug) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addInterceptor(loggingInterceptor);
        }
        return builder.build();
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient client, Gson gson) {
        Retrofit.Builder builder =
                new Retrofit.Builder().addCallAdapterFactory(AsyncRxJavaCallAdapterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create(gson)).client(client)
                        .baseUrl(mBaseUrl);
        return builder.build();
    }

    @Provides
    @Singleton
    Gson provideGson() {
        return new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .create();
    }

    @Provides
    @Singleton
    GitHubService provideGitHubService(Retrofit retrofit) {
        return retrofit.create(GitHubService.class);
    }
}
