package xyz.cottongin.radio247

import android.app.Application
import androidx.room.Room
import xyz.cottongin.radio247.data.db.RadioDatabase
import xyz.cottongin.radio247.data.db.SomaFmSeedData
import xyz.cottongin.radio247.data.logging.NowPlayingHistoryWriter
import xyz.cottongin.radio247.data.prefs.RadioPreferences
import xyz.cottongin.radio247.metadata.AlbumArtResolver
import xyz.cottongin.radio247.service.RadioController
import xyz.cottongin.radio247.service.StreamResolver
import okhttp3.OkHttpClient

class RadioApplication : Application() {
    val database: RadioDatabase by lazy {
        Room.databaseBuilder(this, RadioDatabase::class.java, "radio_database")
            .addCallback(SomaFmSeedData)
            .addMigrations(RadioDatabase.MIGRATION_1_2, RadioDatabase.MIGRATION_2_3, RadioDatabase.MIGRATION_3_4, RadioDatabase.MIGRATION_4_5, RadioDatabase.MIGRATION_5_6)
            .build()
    }

    val preferences: RadioPreferences by lazy {
        RadioPreferences(this)
    }

    val controller: RadioController by lazy {
        RadioController(this)
    }

    val okHttpClient: OkHttpClient by lazy {
        OkHttpClient.Builder().build()
    }

    val albumArtResolver: AlbumArtResolver by lazy {
        AlbumArtResolver(okHttpClient)
    }

    val streamResolver: StreamResolver by lazy {
        StreamResolver(database.stationStreamDao(), preferences, database.stationPreferenceDao())
    }

    val historyWriter: NowPlayingHistoryWriter by lazy {
        NowPlayingHistoryWriter(this, preferences)
    }
}
