package com.zxdemo

import android.app.Activity
import android.app.Application
import android.app.KeyguardManager
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.os.PowerManager
import com.zxdemo.admob.AdmobHelper
import com.zxdemo.receiver.AppInstallReceiver
import com.zxdemo.receiver.BatteryStatusReceiver
import com.zxdemo.receiver.UnlockReceiver
import com.zxdemo.service.PermanentNotifyService.Companion.startOmgNotification
import com.zxdemo.utils.ActivityCollector
import com.zxdemo.utils.DeviceUtils
import com.zxdemo.utils.FcmUtils
import com.zxdemo.utils.InstallRefeerUtils

class ZxApplication : Application() {

    companion object {
        lateinit var context: Context
        var APP_STATE = 0
        val packname = "com.testsdktestestababa.aa.com"
        var isDeviceLocked: Boolean = false
        var isAppInForeground: Boolean = false
        var isScreenOn: Boolean = false
        var GID = ""
        val APP_KEY = "f3ea1ff055365453"
        val noLoadingActivities = listOf(
            "full", // 过滤全屏广告
            "adActivity",
            "AppLovinFullscreenActivity",
            // 返回前台时不跳转启动页的 activity
        )
    }

    override fun onCreate() {
        super.onCreate()
        DeviceUtils.getGoogleAdvertiserId()
        context = applicationContext
        sendBroadCast()
        registerActivityLifecycleCallbacks()
        val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
        isScreenOn = powerManager.isInteractive

        InstallRefeerUtils.init()

        startOmgNotification()
        AdmobHelper.init(this, {}, {})

        AppInstallReceiver.registerReceiver(context)
        UnlockReceiver.mIsScreenOn
    }

    fun sendBroadCast() {
        //注册广播
        AppInstallReceiver.registerReceiver(context)
        UnlockReceiver().initBroadcast(context)
        val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        runCatching {
            context.registerReceiver(BatteryStatusReceiver(), filter)
        }
        FcmUtils().initFirebase(context)
        FcmUtils().subscribeToTopic("${packname}_push")

//        var config = SolarEngineConfig.Builder().build()
//        SolarEngineManager.getInstance()
//            .initialize(context, APP_KEY, config)
    }


    private var lastTimePause = 0L
    private var lastTimeResume = 0L
    private fun isHotLaunch(): Boolean {
        if ((lastTimeResume - lastTimePause) > 5000) {
            return true
        }
        return false
    }

    private fun registerActivityLifecycleCallbacks() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            private var count = 0

            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            }

            override fun onActivityStarted(activity: Activity) {
                count++
                lastTimeResume = System.currentTimeMillis()
                if (count == 1 && isHotLaunch()) {
                    val topActivity = ActivityCollector.getTopActivity()
                    if (topActivity == null) {
                        // 跳转启动页
                        return
                    }

                    var flag = noLoadingActivities.all {
                        !topActivity.localClassName.contains(
                            it,
                            true
                        )
                    }
                    if (flag) {
                        // 跳转启动页
                    }
                    lastTimeResume = 0
                }
            }

            override fun onActivityResumed(activity: Activity) {
                APP_STATE = 1
                isAppInForeground = true
            }

            override fun onActivityPaused(activity: Activity) {
                APP_STATE = 2
                lastTimePause = System.currentTimeMillis()
                isAppInForeground = false
            }

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

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

            override fun onActivityDestroyed(activity: Activity) {
            }
        })

        val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
        isDeviceLocked = keyguardManager.isKeyguardLocked
    }

}