package com.base.browserwhite

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.base.browserwhite.utils.ActivityManagerUtils
import com.facebook.FacebookSdk
import com.liulishuo.filedownloader.FileDownloader


class MyApplication : Application() {

    companion object {
        lateinit var context: MyApplication
    }

    override fun onCreate() {
        super.onCreate()
        context = this
        FacebookSdk.sdkInitialize(applicationContext)
        initLifeListener()
        initPRDownloader()
    }

    private fun initLifeListener() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            private var count = 0
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                ActivityManagerUtils.getInstance().addActivity(activity)
            }

            override fun onActivityStarted(activity: Activity) {
//                LogEx.logDebug(TAG, "onActivityStarted")

//                count++
//                lastTimeResume = System.currentTimeMillis()
//                if (count == 1 && isHotLaunch()) {
//
//                    val topActivity: Activity? = ActivityManagerUtils.getInstance().topActivity
//
//                    var flag = if (topActivity == null) {
//                        true
//                    } else {
//                        com.base.filerecoveryblack.ConfigHelper.noLoadingActivities.all {
//                            !topActivity.localClassName.contains(it, true)
//                        }
//                    }
//
//                    if (activity.localClassName.contains("AppLovinFullscreenActivity")) {
//                        flag = false
//                    }
//                    LogEx.logDebug(TAG, "flag=$flag" + "   activity:" + activity.localClassName)
//
//                    if (flag && !isInterOpenShowing) {
//                        if (com.base.browserwhite.ads.AdmobMaxHelper.isOpenAdLoaded()) {
//                            var loaded: Boolean = true
//                            com.base.browserwhite.ads.AdmobMaxHelper.admobMaxShowOpenAd(activity, {
//                                loaded = it
//                            }, {
//                                val sp = AppPreferences.getInstance().getString("splashShowInter", "0").toInt()
//                                if (sp == 1 && !loaded) {
//                                    com.base.browserwhite.ads.AdmobMaxHelper.admobMaxShowInterstitialAd(activity)
//                                }
//                            })
//                        } else {
//                            topActivity?.startActivity(
//                                Intent(
//                                    topActivity,
//                                    SplashActivity::class.java
//                                ).apply {
//                                    putExtra("isHotLaunch", true)
//                                    putExtra("type", -1)
//                                })
//                        }
//                    }
//                }
//                lastTimeResume = 0
            }

            override fun onActivityResumed(activity: Activity) {
//                LogEx.logDebug(TAG, "onActivityResumed")
//                PAUSED_VALUE = 1
            }


            override fun onActivityPaused(activity: Activity) {
//                LogEx.logDebug(TAG, "onActivityPaused")
//                PAUSED_VALUE = 2
//                lastTimePause = System.currentTimeMillis()
            }

            override fun onActivityStopped(activity: Activity) {
                count--
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}

            override fun onActivityDestroyed(activity: Activity) {
                ActivityManagerUtils.getInstance().removeActivity(activity)
            }
        })
    }

    private fun initPRDownloader() {

        // 创建 FileDownloader 的配置对象
//        val config = FileDownloader.Config.Builder()
//            .setConnectionComponent(FileDownloadConnection.Impl.create(this).setRetryOnGone(true))
//            .setMinIntervalMillisCallbackProcess(1000) // 设置回调间隔时间
//            .setPath("path/to/your/download/directory", true) // 设置下载文件的存储路径
//            // 其他配置...
//            .build()

        // 初始化 FileDownloader
        FileDownloader.setupOnApplicationOnCreate(this)
    }
}