package com.phapps.elitedangerous.companion

import android.app.Activity
import android.app.Application
import com.phapps.elitedangerous.companion.di.AppInjector
import com.phapps.elitedangerous.edsm.EdsmClient
import com.squareup.leakcanary.LeakCanary
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasActivityInjector
import timber.log.Timber
import javax.inject.Inject

class EdacApp : Application(), HasActivityInjector {
    @Inject
    lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

    override fun onCreate() {
        super.onCreate()
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return
        }

        Timber.plant(Timber.DebugTree())
        Timber.tag("EdacApp")

        AppInjector.init(this)

        instance = this

        LeakCanary.install(this)
        EdsmClient.init(this)
        EdsmClient.getInstance().setLoggingEnabled(BuildConfig.DEBUG)
        EdsmClient.getInstance().setServer(EdsmClient.Server.Production)

        // FIXME: Should be converted to a bg service like the sync service.
        // newer Android SDK versions do not allow the job to be created if the app is in the bg.
        //JobManager.create(this).addJobCreator(EdsmJobCreator(commanderRepo))
    }

    override fun activityInjector(): AndroidInjector<Activity> {
        return dispatchingAndroidInjector
    }

    companion object {
        lateinit var instance: EdacApp
    }
}
