package com.base.filerecoveryrecyclebin.activity.whatsapp

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.filerecoveryrecyclebin.ads.AdmobMaxHelper
import com.base.filerecoveryrecyclebin.databinding.ActivityWhatsAppCleanerAnimationBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.utils.LogEx
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class WhatsAppCleanerAnimationActivity : BaseActivity<ActivityWhatsAppCleanerAnimationBinding>() {

    private val TAG = "WhatsAppCleanerAnimationActivity"

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

    private var job: Job? = null
    override fun initView() {
        playLottie()
        AdmobMaxHelper.admobMaxShowNativeAd(this,binding.idFlNative)
    }

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

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

    @SuppressLint("SetTextI18n")
    fun jumpJob() = lifecycleScope.launch(Dispatchers.Main) {
        delay(5000)
        LogEx.logDebug(TAG, "delay over")
        binding.lottie.cancelAnimation()
        binding.lottie.visibility = View.GONE
        binding.lottieCompleted.visibility = View.VISIBLE
        binding.lottieCompleted.playAnimation()
        binding.tv.text = "Completed!"
        delay(1000)
        AdmobMaxHelper.admobMaxShowInterstitialAd(this@WhatsAppCleanerAnimationActivity) {
            startActivity(Intent(this@WhatsAppCleanerAnimationActivity, WhatsAppCleanerActivity::class.java))
            finish()
        }
    }

    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
    }

}