package kr.co.vividnext.sodalive.common

import android.content.Context
import coil.ImageLoader
import okhttp3.Cache
import okhttp3.OkHttpClient
import java.io.File

object ImageLoaderProvider {
    lateinit var imageLoader: ImageLoader
        private set

    val isInitialized: Boolean
        get() = ::imageLoader.isInitialized
    fun init(context: Context) {
        val cacheSize = 250L * 1024L * 1024L // 250 MB
        val cacheDirectory = File(
            context.cacheDir,
            "image_cache"
        ).apply { mkdirs() }

        val cache = Cache(cacheDirectory, cacheSize)

        imageLoader = ImageLoader.Builder(context)
            .okHttpClient {
                OkHttpClient().newBuilder()
                    .cache(cache)
                    .build()
            }
            .build()
    }
}
