package rallyedto.finishedto.conventionedto

import android.content.Intent
import android.view.View
import android.widget.Toast
import androidx.activity.addCallback
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.databinding.BuffetedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.sunedto.tumouredto.majorityedto.loseedto.Togetheredto
import kotlin.random.Random

class Stooledto : Lemonedto<BuffetedtoBinding>() {

    override val binding: BuffetedtoBinding by lazy {
        BuffetedtoBinding.inflate(layoutInflater)
    }
            ;
    private var job: Job? = null
    override fun initView() {

        Togetheredto.showNativeAd(this,binding.jeweledto, isShowAdNative = Togetheredto.isShowAdNativeBottom())
    }

    private fun jumpJob() = lifecycleScope.launch(Dispatchers.Main) {
        delay(Random.nextLong(4000, 4500))
        binding.correctedto.visibility = View.INVISIBLE
        binding.silenceedto.visibility = View.VISIBLE
        binding.silenceedto.playAnimation()
        delay(1500)

        val tittle = this@Stooledto.intent?.extras?.getString("tittle")
        admobMaxShowInterstitialAd {
            startActivity(Intent(this@Stooledto, Biologyedto::class.java).apply {
                tittle?.let { putExtra("tittle", it) }
            })
            finish()
        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            Toast.makeText(this@Stooledto, "wait a moment", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onResume() {
        super.onResume()
        if (job?.isActive == false || job == null) {
            job = jumpJob()
        }
    }

    override fun onPause() {
        super.onPause()
        job?.cancel()
        job = null
    }

}