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.Build
import android.os.Bundle
import android.os.PowerManager
import androidx.annotation.RequiresApi
import com.zxdemo.admob.AdmobHelper
//import com.reyun.solar.engine.SolarEngineConfig
//import com.reyun.solar.engine.SolarEngineManager
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
import com.zxdemo.utils.SpUtils
import java.time.LocalTime

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
        )
    }

    @RequiresApi(Build.VERSION_CODES.O)
    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, {}, {})

    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun sendBroadCast() {
        //注册广播
        AppInstallReceiver.registerReceiver(context)
        UnlockReceiver().initBroadcast(context)
        val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        if (context != null) {
            context.registerReceiver(BatteryStatusReceiver(), filter)
        }
        var minute = SpUtils.getInstance().getInt("MINUTE", -1)
        if (minute < 0) {
            minute = LocalTime.now().minute
        } else {
            SpUtils.getInstance().putInt("MINUTE", minute)
        }
        FcmUtils().initFirebase(context)
        FcmUtils().subscribeToTopic("${packname}_push_$minute")

//        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

    }

}