package importantedto.appliededto.surgeonedto.promiseedto.petedto

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.RunneredtoBinding
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 Delayedto : Murderedto<RunneredtoBinding>() {

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

    override fun initView() {
        playLottie()
    }

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

    @SuppressLint("SetTextI18n")
    private fun playLottie() {
        binding.senioredto.imageAssetsFolder = "app_manager/images/"
        binding.senioredto.setAnimation("app_manager/data.json")
        binding.senioredto.playAnimation()
    }

    @SuppressLint("SetTextI18n")
    private fun jumpJob() = lifecycleScope.launch(Dispatchers.Main) {
        delay(Random.nextLong(4000, 6000))
        binding.senioredto.visibility = View.GONE
        binding.eternaledto.visibility = View.VISIBLE
        binding.eternaledto.playAnimation()
        binding.lickedto.text = "Completed!"
        delay(1000)

        Differenceedto.admobMaxShowInterstitialAd(this@Delayedto) {
            startActivity(Intent(this@Delayedto, Negligenceedto::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
    }
}