package com.base.datarecovery.activity.videomanager
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.ads.AdmobMaxHelper
import com.base.datarecovery.databinding.ActivityAppProcessAnimationBinding
import com.base.datarecovery.help.BaseActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

class VideoCleanAnimationActivity : BaseActivity<ActivityAppProcessAnimationBinding>() {

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

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

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

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

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

        AdmobMaxHelper.admobMaxShowInterstitialAd(this@VideoCleanAnimationActivity) {
            startActivity(Intent(this@VideoCleanAnimationActivity, ManagerActivity::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
    }
}