package rallyedto.finishedto.sheepedto.extraterrestrialedto
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.InfrastructureedtoBinding
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 Pickedto : Lemonedto<InfrastructureedtoBinding>() {

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

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

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

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

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

        admobMaxShowInterstitialAd {
            startActivity(Intent(this@Pickedto, Discussedto::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
    }
}