package rallyedto.finishedto.pollutionedto

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

class Opaledto : Lemonedto<DeclarationedtoBinding>() {

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

    private var job: Job? = null

    override fun initView() {
        Togetheredto.showNativeAd(this,binding.jeweledto, isShowAdNative = Togetheredto.isShowAdNativeBottom())
    }

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

//    fun jumpJob() = lifecycleScope.launch {
//        delay(Random.nextLong(3000, 4500))
//        AdmobMaxHelper.admobMaxShowInterstitialAd(this@Opaledto) {
//            startActivity(Intent(this@Opaledto, Carefullyedto::class.java))
//            finish()
//        }
//    }
//
//    override fun onResume() {
//        super.onResume()
//        if (job?.isActive == false || job == null) {
//            job = jumpJob()
//        }
//    }
//
//    override fun onPause() {
//        super.onPause()
//        job?.cancel()
//        job = null
//    }

    private var isPasue = false
    override fun onPause() {
        super.onPause()
        isPasue = false

    }

    override fun onResume() {
        super.onResume()
        isPasue = true
        playAnimal()
    }
    private fun playAnimal() {
        lifecycleScope.launch(Dispatchers.Main) {
            binding.root.postDelayed({
                Lampedto.getImageFiles {
                    animaEnd()
                }

            }, Random.nextLong(500, 1000))
        }
    }
    private fun animaEnd(){
        if (isPasue) {
            admobMaxShowInterstitialAd{
                startActivity(Intent(this, Breatheedto::class.java))
                finish()
            }
        }
    }
}