package com.base.filerecoveryrecyclebin.activity.repeat

import android.content.Intent
import android.widget.Toast
import androidx.activity.addCallback
import androidx.lifecycle.lifecycleScope
import com.base.filerecoveryrecyclebin.ads.AdmobMaxHelper
import com.base.filerecoveryrecyclebin.databinding.ActivityRepeatAnimationBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

class RepeatAnimationActivity : BaseActivity<ActivityRepeatAnimationBinding>() {

    override val binding: ActivityRepeatAnimationBinding by lazy {
        ActivityRepeatAnimationBinding.inflate(layoutInflater)
    }

    private var job: Job? = null

    override fun initView() {
        AdmobMaxHelper.admobMaxShowNativeAd(this,binding.idFlNative)
    }

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

    private fun jumpJob() = lifecycleScope.launch {
        delay(Random.nextLong(3000, 4500))
        AdmobMaxHelper.admobMaxShowInterstitialAd(this@RepeatAnimationActivity) {
            startActivity(Intent(this@RepeatAnimationActivity, RepeatActivity::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
    }


}