package com.aletheia.app

import android.app.Application
import com.aletheia.app.data.AppDatabaseHelper
import com.aletheia.app.data.BookParserService
import com.aletheia.app.data.BookRepository
import com.aletheia.app.data.CatalogCache
import com.aletheia.app.data.QBooksService
import com.aletheia.app.data.SettingsRepository
import com.aletheia.app.diagnostics.DiagnosticsReporter
import com.aletheia.app.ui.reader.ReaderStateCoordinator
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.sync.Mutex
import xyz.kusoft.argusupdater.ArgusUpdateManager

class AletheiaApplication : Application() {
    val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
    val readerPersistenceMutex = Mutex()
    val readerStateCoordinator = ReaderStateCoordinator()

    lateinit var diagnosticsReporter: DiagnosticsReporter
        private set

    lateinit var databaseHelper: AppDatabaseHelper
        private set

    lateinit var settingsRepository: SettingsRepository
        private set

    lateinit var bookParserService: BookParserService
        private set

    lateinit var bookRepository: BookRepository
        private set

    lateinit var qBooksService: QBooksService
        private set

    lateinit var catalogCache: CatalogCache
        private set

    lateinit var appUpdateManager: ArgusUpdateManager
        private set

    override fun onCreate() {
        super.onCreate()

        diagnosticsReporter = DiagnosticsReporter(this)
        diagnosticsReporter.install()
        databaseHelper = AppDatabaseHelper(this)
        settingsRepository = SettingsRepository(this, databaseHelper)
        bookParserService = BookParserService(this)
        bookRepository = BookRepository(databaseHelper, bookParserService, settingsRepository)
        catalogCache = CatalogCache(this)
        qBooksService = QBooksService(settingsRepository, catalogCache)
        appUpdateManager = ArgusUpdateManager(this)
    }
}
