package com.github.mrvilkaman.di.modules;

import android.support.annotation.NonNull;

import com.github.mrvilkaman.presentationlayer.app.CleanBaseSettings;

import java.util.Collections;
import java.util.List;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by Zahar on 31.03.16.
 */
@Module
public class NetworkModule {

	@Provides
	@NonNull
	@Singleton
	public OkHttpClient provideOkHttpClient() {
		final OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
		Interceptor interceptor = provideHttpLoggingInterceptor();
		if (interceptor != null) {
			okHttpBuilder.addInterceptor(interceptor);
		}

		List<Interceptor> networkInterceptors = provideOkHttpNetworkInterceptors();
		for (Interceptor networkInterceptor : networkInterceptors) {
			okHttpBuilder.addNetworkInterceptor(networkInterceptor);
		}

		return okHttpBuilder.build();
	}

	private Interceptor provideHttpLoggingInterceptor() {
		if (CleanBaseSettings.httpLogging()) {
			HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
			httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
			return httpLoggingInterceptor;
		} else {
			return null;
		}
	}


	@SuppressWarnings("unchecked")
	private List<Interceptor> provideOkHttpNetworkInterceptors() {
		return Collections.EMPTY_LIST;
		//		return singletonList(new StethoInterceptor());
	}
}

@@ -78,16 +78,60 @@
 NonNull;
+%0Aimport android.support.annotation.Nullable;
 %0A%0Aimport
@@ -511,16 +511,589 @@
 dule %7B%0A%0A
+%0A%09private final List%3CInterceptor%3E networkInterceptors;%0A%09@Nullable private final BuilderProcessor processor;%0A%0A%09public NetworkModule() %7B%0A%09%09this(Collections.emptyList(), null);%0A%0A%09%7D%0A%0A%09public NetworkModule(List%3CInterceptor%3E networkInterceptors,%0A%09%09%09%09%09%09 @Nullable BuilderProcessor processor) %7B%0A%09%09this.networkInterceptors = networkInterceptors;%0A%09%09this.processor = processor;%0A%09%7D%0A%0A%09public NetworkModule(List%3CInterceptor%3E networkInterceptors) %7B%0A%09%09this(networkInterceptors, null);%0A%09%7D%0A%0A%09public NetworkModule(BuilderProcessor processor) %7B%0A%09%09this(Collections.emptyList(), processor);%0A%09%7D%0A%0A
 %09@Provid
@@ -1235,16 +1235,85 @@
 lder();%0A
+%0A%09%09if (processor != null) %7B%0A%09%09%09processor.handle(okHttpBuilder);%0A%09%09%7D%0A%0A
 %09%09Interc
@@ -1449,86 +1449,8 @@
 %09%7D%0A%0A
-%09%09List%3CInterceptor%3E networkInterceptors = provideOkHttpNetworkInterceptors();%0A
 %09%09fo
@@ -1930,192 +1930,85 @@
 %09%7D%0A%0A
-%0A%09@SuppressWarnings(%22unchecked%22)%0A%09private List%3CInterceptor%3E provideOkHttpNetworkInterceptors() %7B%0A%09%09return Collections.EMPTY_LIST;%0A%09%09//%09%09return singletonList(new StethoInterceptor()
+%09interface BuilderProcessor %7B%0A%09%09void handle(OkHttpClient.Builder builder
 );%0A%09%7D%0A
+%0A
 %7D%0A

update network module