package com.pixelized.biblib.module

import android.content.Context
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.pixelized.biblib.network.client.BibLibClient
import com.pixelized.biblib.network.client.IBibLibClient
import com.pixelized.biblib.network.factory.BIBLIB_API_DATE_FORMAT
import com.pixelized.biblib.repository.connectivity.ConnectivityRepository
import com.pixelized.biblib.repository.connectivity.IConnectivityRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class NetworkModule {

    @Provides
    @Singleton
    fun provideGsonParser(): Gson {
        return GsonBuilder().create()
    }

    @Provides
    @Singleton
    fun provideBibLibClient(
        gson: Gson
    ): IBibLibClient {
        return BibLibClient(
            gson = gson
        )
    }

    @Provides
    @Singleton
    fun provideConnectivityRepository(
        @ApplicationContext context: Context,
    ): IConnectivityRepository {
        return ConnectivityRepository(context)
    }

    @Provides
    fun provideBibLibDateFormatter(): DateFormat {
        return SimpleDateFormat(BIBLIB_API_DATE_FORMAT, Locale.getDefault())
    }
}