package ca.ksamad.encore

import android.app.Application
import ca.ksamad.encore.playback.ArtImageLoader
import ca.ksamad.encore.playback.MpdConnectionManager
import coil3.ImageLoader
import coil3.PlatformContext
import coil3.SingletonImageLoader

/**
 * Holds the app-scoped [MpdConnectionManager] so the MPD connection outlives any single Activity
 * and can be shared with the foreground [ca.ksamad.encore.playback.PlaybackService].
 *
 * Also the Coil [SingletonImageLoader.Factory], wiring cover-art loading to that same manager so
 * `AsyncImage` calls anywhere in the app fetch (and cache) MPD art.
 */
class EncoreApplication : Application(), SingletonImageLoader.Factory {
    val manager: MpdConnectionManager by lazy { MpdConnectionManager(this) }

    override fun onCreate() {
        super.onCreate()
        // Create eagerly so startup auto-connect (and the media service) kick in
        // as soon as the process starts, not only when the UI first observes it.
        manager
    }

    override fun newImageLoader(context: PlatformContext): ImageLoader =
        ArtImageLoader.create(context, manager)
}
