package rallyedto.finishedto.climateedto.depressionedto.dustedto

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.RaceedtoBinding
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 Treeedto : Lemonedto<RaceedtoBinding>() {

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

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

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


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

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

    }

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

    @SuppressLint("SetTextI18n")
    private fun waitJob() = lifecycleScope.launch(Dispatchers.Main) {
        delay(Random.nextLong(3000, 4500))
        binding.correctedto.visibility = View.GONE
        binding.silenceedto.visibility = View.VISIBLE
        binding.silenceedto.playAnimation()
        binding.promoteedto.text = "Completed!"
        delay(1000)
        admobMaxShowInterstitialAd {
            binding.correctedto.clearAnimation()
            startActivity(Intent(this@Treeedto, Workedto::class.java))
            finish()
        }
    }

}