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 kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

class WhatsAppCleanerAnimationActivity : BaseActivity<ActivityWhatsAppCleanerAnimationBinding>() {

    private val animationPath = "whupscan/data.json"
    private val imagesPath = "whupscan/images/"
    private val nextActivityClass = WhatsAppCleanerActivity::class.java

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

    override fun initView() {
        playLottie()
    }

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

    @SuppressLint("SetTextI18n")
    private fun playLottie() {
        binding.lottie.imageAssetsFolder = imagesPath
        binding.lottie.setAnimation(animationPath)
        binding.lottie.playAnimation()
        lifecycleScope.launch {
            delay(Random.nextLong(3500, 4500))
            binding.lottie.cancelAnimation()
            binding.lottie.visibility = View.GONE
            showCompletionAnimation()
        }
    }

    @SuppressLint("SetTextI18n")
    private suspend fun showCompletionAnimation() {
        binding.lottieCompleted.visibility = View.VISIBLE
        binding.lottieCompleted.playAnimation()
        binding.tv.text = "Completed!"
        delay(1000)
        showAdAndNavigateToNextActivity()
    }

    private fun showAdAndNavigateToNextActivity() {
        AdmobMaxHelper.admobMaxShowInterstitialAd(this) {
            startActivity(Intent(this, nextActivityClass))
            finish()
        }
    }
}