package com.gh.gamecenter.common

import android.app.Application
import android.content.Context
import android.content.res.Configuration
import com.gh.gamecenter.common.base.AppUncaughtHandler
import com.gh.gamecenter.common.base.GlobalActivityLifecycleObserver
import com.gh.gamecenter.common.utils.doOnMainProcessOnly
import com.gh.gamecenter.core.iinterface.IApplication
import com.google.auto.service.AutoService

@AutoService(IApplication::class)
class HaloApp : IApplication {

    override fun attachBaseContext(base: Context) {

    }

    override fun onCreate(application: Application) {
        mApp = application
        doOnMainProcessOnly {
            mApp.registerActivityLifecycleCallbacks(GlobalActivityLifecycleObserver())
            initAppUncaughtHandler()
        }
    }

    private fun initAppUncaughtHandler() {
        if (!BuildConfig.DEBUG) {
            val appUncaughtHandler = AppUncaughtHandler(mApp)
            Thread.setDefaultUncaughtExceptionHandler(appUncaughtHandler)
        }
    }


    override fun onLowMemory() {

    }

    override fun onTerminate() {

    }

    override fun onTrimMemory(level: Int) {

    }

    override fun onConfigurationChanged(newConfig: Configuration) {

    }

    companion object {
        private lateinit var mApp: Application

        fun getInstance(): Application {
            return mApp
        }
    }
}