package com.proyek.akhir.kodel.utils

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class ApiClient {

    private fun getInterceptor(): OkHttpClient {
        val httpLoggingInterceptor = HttpLoggingInterceptor()
        httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY

        return OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor)
            .connectTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .build()
    }

    private fun getRetrofitInstanceDel(): Retrofit {
        return Retrofit.Builder()
            .baseUrl(Constans.API_GET_DATA_DEL)
            .client(getInterceptor())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    private fun getRetrofitInstanceKode(): Retrofit {
        return Retrofit.Builder()
            .baseUrl(Constans.API_GET_DATA_KODE)
            .client(getInterceptor())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    private fun postRetrofitInstance(): Retrofit {
        return Retrofit.Builder()
            .baseUrl(Constans.API_POST_DATA)
            .client(getInterceptor())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    fun getServiceDel(): GetService {
        return getRetrofitInstanceDel().create(GetService::class.java)
    }

    fun getServiceKode(): GetService {
        return getRetrofitInstanceKode().create(GetService::class.java)
    }

    fun postServicePeople(): GetService {
        return postRetrofitInstance().create(GetService::class.java)
    }
}