package com.datarecovery.master.di;

import com.atmob.network.interceptor.crypto.AtmobCryptoInterceptor;
import com.atmob.network.okhttp.AtmobOkHttpClient;
import com.datarecovery.master.BuildConfig;
import com.datarecovery.master.data.api.AtmobApi;
import com.datarecovery.master.data.api.V8Api;
import com.datarecovery.master.data.consts.Constants;
import com.google.gson.Gson;

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 AtmobCryptoInterceptor.Builder()
                        .appKey(BuildConfig.ATMOB_CENTRAL_KEY)
                        .setCryptoVersion(AtmobCryptoInterceptor.Version.V1)
                        .build())
                .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 V8Api provideV8Api(Gson gson) {
        OkHttpClient okHttpClient = AtmobOkHttpClient.newInstance("V8Api", BuildConfig.DEBUG)
                .newBuilder()
                .build();
        return new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .baseUrl("https://i90okxj.v8dashen.com:4695")
                .build()
                .create(V8Api.class);
    }
}