package com.base.filerecoveryrecyclebin.activity.appprocess

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.ActivityAppProcessAnimationBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

class AppProcessAnimationActivity : BaseActivity<ActivityAppProcessAnimationBinding>() {

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

    override fun initView() {
        playLottie()
    }

    override fun initListener() {
        onBackPressedDispatcher.addCallback {
            Toast.makeText(this@AppProcessAnimationActivity, "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()
        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@AppProcessAnimationActivity) {
                startActivity(Intent(this@AppProcessAnimationActivity, AppProcessActivity::class.java))
                finish()
            }

        }
    }

}