package com.base.filerecoveryrecyclebin.activity.screenshot

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.ActivityScreenShotAnimationBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.utils.LogEx
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

class ScreenShotAnimationActivity : BaseActivity<ActivityScreenShotAnimationBinding>() {

    private val TAG = "ScreenShotAnimationActivity"

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

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

    private fun jumpJob() = lifecycleScope.launch {
        delay(Random.nextLong(3000, 4500))
        AdmobMaxHelper.admobMaxShowInterstitialAd(this@ScreenShotAnimationActivity) {
            startActivity(Intent(this@ScreenShotAnimationActivity, ScreenShotActivity::class.java))
            finish()
        }
    }

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

    override fun onResume() {
        super.onResume()
        LogEx.logDebug(TAG, "onResume")
        if (job?.isActive == false || job == null) {
            job = jumpJob()
        }
    }

    override fun onPause() {
        super.onPause()
        LogEx.logDebug(TAG, "onPause")
        job?.cancel()
        job = null
    }

}