package importantedto.appliededto.surgeonedto.promiseedto.dialectedto

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.KeepedtoBinding
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 importantedto.Exhibitionedto
import importantedto.admireedto.sweptedto.Differenceedto
import importantedto.afteredto.constitutionaledto.Murderedto
import importantedto.concreteedto.cameedto.Hateedto.ifAgreePrivacy
import importantedto.gallonedto.Rideredto
import importantedto.gallonedto.Transcendedto
import importantedto.growedto.umbrellaedto.Appleedto
import importantedto.growedto.umbrellaedto.Busyedto
import java.util.concurrent.atomic.AtomicBoolean

@SuppressLint("CustomSplashScreen")
class Styleedto : Murderedto<KeepedtoBinding>(),
    Policyedto.OnUserPrivacyAgreementListener, Breatheedto.ProgressListener {
    private val TAG = "NewStartActivity"
    private fun initStatusBar() {
        Transcendedto.setStatusBarLightMode(this, true)
        Transcendedto.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) {
            Rideredto.event("back")
        }
        return super.onKeyDown(keyCode, event)
    }


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

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

    override fun initView() {
        initStatusBar()
        if (isDestroyed) {
            return
        }
        if (Exhibitionedto.isInterOpenShowing) {
            finish()
            return
        }
        initSdk()
        Appleedto.stopNotificationHandler()
        jumpType = intent.getIntExtra("actionId", 0)
        closeNotification()
        mTaskManager = Breatheedto(binding, this)
        if (ifAgreePrivacy) {
            onAgreePrivacy()
        } else {
            Policyedto(binding, this, this)
        }

    }

    private fun closeNotification() {
        sendBroadcast(Intent(this, Busyedto::class.java).apply {
            this.action = Busyedto.Action
            this.putExtra(Busyedto.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() {
        Rideredto.event("app_start")
        if (jumpType == 0) {
//            startStayNotification()
        }
        Differenceedto.preloadAd(this)
        mTaskManager?.startProgress()
        loadAd()
    }


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

    private fun loadAd() {
        Differenceedto.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
        Rideredto.event("onPause")
    }

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

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


}