package com.atmob.voiceai.di;

import com.atmob.network.okhttp.AtmobOkHttpClient;
import com.atmob.voiceai.BuildConfig;
import com.atmob.voiceai.data.api.AtmobApi;
import com.atmob.voiceai.data.api.timeout.TimeoutInterceptor;
import com.atmob.voiceai.data.consts.Constants;
import com.google.gson.Gson;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import atmob.okhttp3.OkHttpClient;
import atmob.retrofit2.Retrofit;
import atmob.retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import atmob.retrofit2.converter.gson.GsonConverterFactory;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.components.SingletonComponent;

@Module
@InstallIn(SingletonComponent.class)
public class NetworkModule {
    private static final String ATMOB_TAG = "AtmobApi";

    @Singleton
    @Provides
    public static AtmobApi provideAtmobApi(Gson gson) {
        OkHttpClient okHttpClient = AtmobOkHttpClient.newInstance(ATMOB_TAG, BuildConfig.DEBUG)
                .newBuilder()
                .addInterceptor(new TimeoutInterceptor())
                .build();
        return new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .baseUrl(Constants.Atmob_Server_Base_URL)
                .build()
                .create(AtmobApi.class);
    }


    @Singleton
    @Provides
    public static OkHttpClient downloadFileHttpClient() {
        return new OkHttpClient().newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                .writeTimeout(120, TimeUnit.SECONDS)
                .build();
    }

}