package com.a2i.forwarder

import android.app.Application
import com.a2i.forwarder.core.CarrierBalanceQuery
import com.a2i.forwarder.core.CarrierDetector
import com.a2i.forwarder.core.ConnectivityMonitor
import com.a2i.forwarder.core.LogStore
import com.a2i.forwarder.core.PhoneCallMonitor
import com.a2i.forwarder.core.SmsFallbackForwarder
import com.a2i.forwarder.core.UpdateChecker
import com.a2i.forwarder.store.AppRulesStore
import com.a2i.forwarder.store.SettingsStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob

class A2iApp : Application() {
    val appScope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)

    lateinit var settings: SettingsStore
        private set
    lateinit var appRules: AppRulesStore
        private set
    lateinit var logStore: LogStore
        private set
    lateinit var phoneCallMonitor: PhoneCallMonitor
        private set
    lateinit var connectivityMonitor: ConnectivityMonitor
        private set
    lateinit var carrierBalanceQuery: CarrierBalanceQuery
        private set
    lateinit var smsForwarder: SmsFallbackForwarder
        private set
    lateinit var updateChecker: UpdateChecker
        private set

    override fun onCreate() {
        super.onCreate()
        instance = this
        settings = SettingsStore(this, appScope)
        appRules = AppRulesStore(this, appScope)
        logStore = LogStore(this, appScope)
        phoneCallMonitor = PhoneCallMonitor(this)
        phoneCallMonitor.start()
        connectivityMonitor = ConnectivityMonitor(this)
        connectivityMonitor.start()
        carrierBalanceQuery = CarrierBalanceQuery(this)
        smsForwarder = SmsFallbackForwarder(this)
        updateChecker = UpdateChecker(this)
        updateChecker.start()
    }

    companion object {
        lateinit var instance: A2iApp
            private set
    }
}
