package com.base.filerecoveryrecyclebin.activity.appmanager

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

class AppManagerAnimationActivity : BaseActivity<ActivityAppProcessAnimationBinding>() {

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

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

    override fun initListener() {
        onBackPressedDispatcher.addCallback {
            Toast.makeText(this@AppManagerAnimationActivity, "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@AppManagerAnimationActivity) {
            startActivity(Intent(this@AppManagerAnimationActivity, AppManagerActivity::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
    }
}