package pxds

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.google.firebase.FirebaseApp
import com.test.basd.supercleanermax.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import pxds.etmo.lexn.ylyy.Anet
import pxds.iqiv.ioih.gxph.Zmze.Companion.initBroadcast
import pxds.iqiv.ioih.gxph.rrax.Mqmn
import pxds.lmmd.zmwf.Cisv
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.Lwfm
import pxds.lmmd.zmwf.Uawv
import pxds.lmmd.zmwf.Uloz.requestCfg
import pxds.lmmd.zmwf.jyyf.hwbn.wzxb.Utdc

class Tmuv : Lwfm() {

    override fun init() {
        initApp()
    }

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

            override fun onActivityStarted(activity: Activity) {

                count++
                if (count == 1 && !AppUtils.isAppForeground()) {
                    val topActivity = ActivityUtils.getTopActivity()
                    if (Uawv.noLoadingActivities.all {
                            !topActivity.localClassName.contains(
                                it, true
                            )
                        }) {
                        if (Utdc.isOpenAdLoaded()) {
                            Utdc.showAppOpenAd(activity)
                        } else {
                            topActivity.startActivity(
                                Intent(
                                    topActivity, Anet::class.java
                                ).apply {
                                    putExtra(R.string.ދޢޘޏއ_D.string(), true)
                                })
                        }
                    }
                }
            }

            override fun onActivityResumed(activity: Activity) {
                PAUSED_VALUE = 1
            }

            override fun onActivityPaused(activity: Activity) {
                PAUSED_VALUE = 2
            }

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

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

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

    fun initApp() {
        if (Uawv.ifAgreePrivacy) {
            MainScope().launch(Dispatchers.IO) {
                try {
                    Uawv.gid = AdvertisingIdClient.getAdvertisingIdInfo(context).id ?: ""
                } catch (_: Exception) {
                }
            }

            initNotificationWork()
            context.initBroadcast()
            MainScope().launch {
                requestCfg()
                Cisv.init()
            }
        }
        MobileAds.initialize(this) { initializationStatus ->
            val statusMap = initializationStatus.adapterStatusMap
            for (adapterClass in statusMap.keys) {
                val status = statusMap[adapterClass]
                Log.d(
                    R.string.ގޖޟއޅ_D.string(), String.format(
                        R.string.މޒސޛގ_D.string(), adapterClass, status!!.description, status.latency
                    )
                )
            }
        }
        initLifeListener()
    }

    private fun initNotificationWork() {
        FirebaseApp.initializeApp(this)
        Mqmn.getToken()
        Mqmn.subscribeToTopic()
    }

    companion object {
        var PAUSED_VALUE = 0
    }
}