package pxds.etmo.lexn.ylyy

import android.Manifest
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Handler
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.isVisible
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.BarUtils
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.AfqrlyBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import pxds.iqiv.ioih.gxph.Fnmj
import pxds.iqiv.ioih.gxph.Vmuc
import pxds.iqiv.ioih.gxph.rrax.Gnup.Companion.initJob
import pxds.lmmd.zmwf.Bpfw
import pxds.lmmd.zmwf.Egac
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.Uawv
import pxds.lmmd.zmwf.jyyf.hwbn.wzxb.Utdc

class Anet : Bpfw<AfqrlyBinding>(), Rwze.onUserPrivacyAggreementListener, Azmb.ProgressListener {

    private var isPause = false

    override val binding: AfqrlyBinding by lazy {
        AfqrlyBinding.inflate(layoutInflater)
    }

    private var mProgressManager: Azmb? = null

    private val TAG = R.string.ދޒޟޖޑ_D.string()

    override fun onProgressMax() {
        Handler().postDelayed({
            Rbpc.jumpNextPage(this)
        }, 500)
    }

    private fun loadAd() {
        Utdc.loadAppOpenAd {
            if (isFinishing || isDestroyed) {
                return@loadAppOpenAd
            } else {
                if (!isPause) {
                    mProgressManager?.pauseProgress()
                    runOnUiThread {
                        Utdc.showAppOpenAd(this@Anet) {
                            Log.d(R.string.ޓތޅހޠ_D.string(), R.string.ކއކޒޥ_D.string())
                            mProgressManager?.maxProgress()
                        }
                    }
                }
            }
        }
        Utdc.loadInterstitialAd(this)
    }

    override fun onAgreePrivacy() {
        initJob()
        MainScope().launch(Dispatchers.IO) {
            Uawv.appList = AppUtils.getAppsInfo().shuffled()
        }
        mProgressManager?.startProgress()
        loadAd()
    }

    private fun cancelNotification() {
        var jumpType = intent.getIntExtra(R.string.ޥޚޅރޤ_D.string(), 0)
        if (jumpType == 0) {
            val uri = intent.data
            val str = (uri?.getQueryParameter(R.string.ޥޚޅރޤ_D.string()) ?: "0")
            jumpType = str.toIntOrNull() ?: 0
        }
        Egac.logDebug(TAG, R.string.ބލޘއޠ_D.string((jumpType).toString()))
        if (jumpType != 0) {
            val cancelIntent = Intent(this, Fnmj::class.java)
            cancelIntent.action = Fnmj.action
            cancelIntent.putExtra(R.string.ޓޅފހޡ_D.string(), Vmuc.notificationId)
            sendBroadcast(cancelIntent)
        }
    }

    private fun initStatusBar() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        if (Build.VERSION.SDK_INT >= 33) {
            registerForActivityResult(ActivityResultContracts.RequestPermission()) {}.launch(
                Manifest.permission.POST_NOTIFICATIONS
            )
        }
    }

    override fun onResume() {
        super.onResume()
        isPause = false
    }

    override fun onPause() {
        super.onPause()
        isPause = true
    }

    override fun onStart() {
        super.onStart()
        isPause = false
    }

    override fun onStop() {
        super.onStop()
        isPause = true
    }

    override fun initView() {
        cancelNotification()
        initStatusBar()
        if (isDestroyed) {
            return
        }

        mProgressManager = Azmb(binding, this)
        if (Uawv.ifAgreePrivacy) {
            binding.zifgydy.isVisible = false
            binding.hilwkxt.isVisible = true
            binding.tkhqynm.isVisible = false
            onAgreePrivacy()
        } else {
            Rwze(binding, this, this)
        }

    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
    }
}