package rowedto.jockeyedto.sheetedto.atticedto.fadeedto

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Handler
import android.view.KeyEvent
import androidx.activity.result.contract.ActivityResultContracts
import com.base.datarecovery.databinding.YogaedtoBinding
import com.google.android.gms.ads.MobileAds
import com.google.android.ump.ConsentInformation
import com.google.android.ump.ConsentRequestParameters
import com.google.android.ump.UserMessagingPlatform
import rowedto.jockeyedto.sheetedto.Epicalyxedto
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.ifAgreePrivacy
import rowedto.jockeyedto.sheetedto.interactiveedto.twistedto.Easilyedto
import rowedto.jockeyedto.sheetedto.interactiveedto.twistedto.Sectoredto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Slantedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto
import java.util.concurrent.atomic.AtomicBoolean

@SuppressLint("CustomSplashScreen")
class Bottomedto : Crossingedto<YogaedtoBinding>(),
    Sympatheticedto.OnUserPrivacyAgreementListener, Continentaledto.ProgressListener {
    private val TAG = "NewStartActivity"
    private fun initStatusBar() {
        Spareedto.setStatusBarLightMode(this, true)
        Spareedto.setStatusBarColor(this, Color.TRANSPARENT)
        if (Build.VERSION.SDK_INT >= 33) {
            registerForActivityResult(ActivityResultContracts.RequestPermission()) {}.launch(
                Manifest.permission.POST_NOTIFICATIONS
            )
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Slantedto.event("back")
        }
        return super.onKeyDown(keyCode, event)
    }


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

    private var mTaskManager: Continentaledto? = null
    var jumpType = 0

    override fun initView() {
        initStatusBar()
        if (isDestroyed) {
            return
        }
        if (Epicalyxedto.isInterOpenShowing) {
            finish()
            return
        }
        initSdk()
        Sectoredto.stopNotificationHandler()
        jumpType = intent.getIntExtra("actionId", 0)
        closeNotification()
        mTaskManager = Continentaledto(binding, this)
        if (ifAgreePrivacy) {
            onAgreePrivacy()
        } else {
            Sympatheticedto(binding, this, this)
        }

    }

    private fun closeNotification() {
        sendBroadcast(Intent(this, Easilyedto::class.java).apply {
            this.action = Easilyedto.Action
            this.putExtra(Easilyedto.NotificationId, jumpType)
        })
    }
    private lateinit var consentInformation: ConsentInformation
    private var isMobileAdsInitializeCalled = AtomicBoolean(false)
    private fun initSdk(){
        val params = ConsentRequestParameters
            .Builder()
            .build()
        consentInformation = UserMessagingPlatform.getConsentInformation(this)
        consentInformation.requestConsentInfoUpdate(
            this,
            params,
            {
                UserMessagingPlatform.loadAndShowConsentFormIfRequired(
                    this
                ) { loadAndShowError ->
                    if (loadAndShowError != null) {
                    }

                    if (consentInformation.canRequestAds()) {
                        initializeMobileAdsSdk()
                    }
                }
            },
            {

            })
        if (consentInformation.canRequestAds()) {
            initializeMobileAdsSdk()
        }
    }
    private fun initializeMobileAdsSdk(){
        if (isMobileAdsInitializeCalled.getAndSet(true)) {
            return
        }
        MobileAds.initialize(this) {}
    }
    override fun onAgreePrivacy() {
        Slantedto.event("app_start")
        if (jumpType == 0) {
//            startStayNotification()
        }
        Scandaledto.preloadAd(this)
        mTaskManager?.startProgress()
        loadAd()
    }


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

    private fun loadAd() {
        Scandaledto.admobMaxShowOpenAd(this, {
            mTaskManager?.pauseProgress()
        }, {
            mTaskManager?.pauseProgress()
            runOnUiThread {
                mTaskManager?.maxProgress()
            }

        })
    }

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

    private var isPause = false

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

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

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

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


}