package pl.firmatpp.kierowca

import android.app.Application
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import pl.firmatpp.kierowca.data.upload.OfflineOutboxManager
import pl.firmatpp.kierowca.diagnostics.AppDiagnostics

class DriverApplication : Application() {
    private val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

    override fun onCreate() {
        super.onCreate()
        AppDiagnostics.installFirebaseCrashlytics()
        AppDiagnostics.log("app_started")
        applicationScope.launch {
            val outboxManager = OfflineOutboxManager(this@DriverApplication)
            runCatching { outboxManager.repairOperationalQueues() }
                .onFailure { AppDiagnostics.reportNonFatal(it, "repair_operational_queues") }
            runCatching { outboxManager.repairGpsQueue() }
                .onFailure { AppDiagnostics.reportNonFatal(it, "repair_gps_queue") }
        }
    }
}
