package com.abbidot.tracker

import android.app.ActivityManager
import android.app.Application
import android.os.Build
import android.os.Process
import androidx.appcompat.app.AppCompatDelegate
import coil.Coil
import coil.ImageLoader
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.util.CrashHandler
import com.hjq.toast.Toaster
import com.qmuiteam.qmui.arch.QMUISwipeBackActivityManager
import dagger.hilt.android.HiltAndroidApp


/**
 *Created  by .yzq on 2021/11/2/002.
 * @link
 * @description:Application
 */
@HiltAndroidApp
class MyApplication : Application() {

    companion object {
        lateinit var instance: Application
    }

    override fun onCreate() {
        super.onCreate()
        //强制浅色模式,使用 AppCompatDelegate 或 MaterialComponents 动态切换主题
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        initInMainProcess {
            instance = this
            AppUtils.syncIsDebug(applicationContext)
            QMUISwipeBackActivityManager.init(this)
            MMKVUtil.init(this)

            // 初始化 Toast 框架
            Toaster.init(this)
            Toaster.setView(R.layout.toast_my_layout)

//            JPushInterface.setDebugMode(true)
//            JPushInterface.init(this)

//            if (!AppUtils.isDebug()) {
//                //腾讯bugly初始化
//                CrashReport.initCrashReport(applicationContext, "06fc0a9cd1", AppUtils.isDebug())
//            }

//            //同意协议
//            SDKInitializer.setAgreePrivacy(this, true)
////             LocationClient.setAgreePrivacy(true);
//            //在使用SDK各组件之前初始化context信息，传入ApplicationContext
//            SDKInitializer.initialize(this)
//            //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
//            //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
//            SDKInitializer.setCoordType(CoordType.GCJ02)

//            if (!AppUtils.isChina()) {
//                MapboxSearchSdk.initialize(application = this,
//                        accessToken = getString(R.string.mapbox_access_token),
//                        locationEngine = LocationEngineProvider.getBestLocationEngine(this))
//            }

            //设置支持加载gif
            val imageLoader = ImageLoader.Builder(this).componentRegistry {
                if (Build.VERSION.SDK_INT >= 28) {
                    add(ImageDecoderDecoder(this@MyApplication))
                } else {
                    add(GifDecoder())
                }
            }.build()
            //设置全局唯一实例
            Coil.setImageLoader(imageLoader)
        }

        CrashHandler().init(this)
    }

    /**
     * 主进程初始化
     */
    private fun initInMainProcess(block: () -> Unit) {
        val activityManager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
        val myPId = Process.myPid()
        val mainProcessName = packageName
        activityManager.runningAppProcesses.forEach {
            if (it.pid == myPId && it.processName == mainProcessName) {
                block()
            }
        }
    }
}