package com.base.datarecovery.activity.largefile

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.ActivityLargeFileAnimationBinding
import com.base.datarecovery.help.BaseActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class LargeFileAnimationActivity : BaseActivity<ActivityLargeFileAnimationBinding>() {

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

    private var job: Job? = null

    override fun initView() {
    }

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


    private fun jumpJob() = lifecycleScope.launch(Dispatchers.Main) {
        delay(5000)
        binding.lottie.visibility = View.GONE
        binding.lottie.clearAnimation()
        binding.lottieCompleted.visibility = View.VISIBLE
        binding.lottieCompleted.playAnimation()
        binding.tv.text = "Completed!"
        delay(1000)
        AdmobMaxHelper.admobMaxShowInterstitialAd(this@LargeFileAnimationActivity) {
            startActivity(Intent(this@LargeFileAnimationActivity, LargeFileActivity::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
    }
}