package rallyedto.finishedto.illnessedto

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.CrouchedtoBinding
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

class Sweptedto : Lemonedto<CrouchedtoBinding>() {

    override val binding: CrouchedtoBinding by lazy {
        CrouchedtoBinding.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@Sweptedto, "wait a moment", Toast.LENGTH_SHORT).show()
        }
    }


    private fun jumpJob() = lifecycleScope.launch(Dispatchers.Main) {
        delay(5000)
        binding.correctedto.visibility = View.GONE
        binding.correctedto.clearAnimation()
        binding.silenceedto.visibility = View.VISIBLE
        binding.silenceedto.playAnimation()
        binding.promoteedto.text = "Completed!"
        delay(1000)
        admobMaxShowInterstitialAd {
            startActivity(Intent(this@Sweptedto, Bounceedto::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
    }
}