package app.voltplan.cable

import android.app.Application
import app.voltplan.cable.analytics.Analytics
import app.voltplan.cable.data.CableRepository
import app.voltplan.cable.data.UnitSystemSettings
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

/** Owns the singletons (repository, settings, analytics) shared across the app. */
class CableApplication : Application() {
    lateinit var repository: CableRepository
        private set
    lateinit var settings: UnitSystemSettings
        private set

    override fun onCreate() {
        super.onCreate()
        Analytics.init(this)
        repository = CableRepository(this)
        settings = UnitSystemSettings(this)

        // Mirrors AppDelegate.application(_:didFinishLaunchingWithOptions:).
        CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {
            if (settings.consumeFirstLaunch()) {
                Analytics.log("First Launch")
            }
            Analytics.log("App Launched")
        }
    }
}
