package importantedto.appliededto.surgeonedto.promiseedto.obtainedto.museumedto.outletedto

import android.annotation.SuppressLint
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.DogedtoBinding
import importantedto.admireedto.sweptedto.Differenceedto
import importantedto.afteredto.constitutionaledto.Murderedto
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

class Probablyedto : Murderedto<DogedtoBinding>() {

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

    override fun initView() {
        resumeLottie()
    }

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


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

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

    }

    private fun resumeLottie() {
        binding.senioredto.imageAssetsFolder = "battery_scan/images/"
        binding.senioredto.setAnimation("battery_scan/data.json")
        binding.senioredto.playAnimation()
    }

    @SuppressLint("SetTextI18n")
    private fun waitJob() = lifecycleScope.launch(Dispatchers.Main) {
        delay(Random.nextLong(3000, 4500))
        binding.senioredto.visibility = View.GONE
        binding.eternaledto.visibility = View.VISIBLE
        binding.eternaledto.playAnimation()
        binding.lickedto.text = "Completed!"
        delay(1000)
        Differenceedto.admobMaxShowInterstitialAd(this@Probablyedto) {
            binding.senioredto.clearAnimation()
            startActivity(Intent(this@Probablyedto, Hardshipedto::class.java))
            finish()
        }
    }

}