package com.syaroful.agrilinkvocpro.growth_recipe_feature.di

import com.syaroful.agrilinkvocpro.growth_recipe_feature.data.network.ClusteringService
import com.syaroful.agrilinkvocpro.growth_recipe_feature.data.network.GrowthRecipeService
import okhttp3.OkHttpClient
import org.koin.core.qualifier.named
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

val CLUSTER_CLIENT = named("CLUSTER_CLIENT")
val CLUSTER_RETROFIT = named("CLUSTER_RETROFIT")

val RECIPE_CLIENT = named("RECIPE_CLIENT")
val RECIPE_RETROFIT = named("RECIPE_RETROFIT")

val networkModule = module {

    // Cluster client and retrofit
    single(CLUSTER_CLIENT) {
        OkHttpClient.Builder()
            .connectTimeout(5, TimeUnit.SECONDS)
            .build()
    }

    single(CLUSTER_RETROFIT) {
        Retrofit.Builder()
            .baseUrl("http://labai.polinema.ac.id:5050/")
            .client(get(CLUSTER_CLIENT))
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    single<ClusteringService> {
        get<Retrofit>(CLUSTER_RETROFIT).create(ClusteringService::class.java)
    }

    // Growth Recipe client and retrofit
    single(RECIPE_CLIENT) {
        OkHttpClient.Builder()
            .connectTimeout(2, TimeUnit.SECONDS)
            .build()
    }

    single(RECIPE_RETROFIT) {
        Retrofit.Builder()
            .baseUrl("http://labai.polinema.ac.id:3042/")
            .client(get(RECIPE_CLIENT))
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    single<GrowthRecipeService> {
        get<Retrofit>(RECIPE_RETROFIT).create(GrowthRecipeService::class.java)
    }
}