package com.base.filerecoveryrecyclebin.activity

import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.Color
import android.view.animation.LinearInterpolator
import android.widget.Toast
import androidx.activity.addCallback
import androidx.lifecycle.lifecycleScope
import com.base.filerecoveryrecyclebin.ads.AdmobMaxHelper
import com.base.filerecoveryrecyclebin.databinding.ActivityLayoutXingzuoLoadingBinding
import com.base.filerecoveryrecyclebin.fragment.WebBrowserActivity
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.utils.BarUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.random.Random

class XzLoadingActivity : BaseActivity<ActivityLayoutXingzuoLoadingBinding>() {
    override val binding: ActivityLayoutXingzuoLoadingBinding by lazy {
        ActivityLayoutXingzuoLoadingBinding.inflate(layoutInflater)
    }

    override fun initView() {
        BarUtils.setStatusBarLightMode(this,false)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        AdmobMaxHelper.admobMaxShowNativeAd(this,binding.idFlNative)
        ValueAnimator.ofFloat(0f, 360f).apply {
            duration = 1100
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener { binding.idZhuan.rotation = it.animatedValue as Float }
            start()
        }
    }
    override fun initListener() {
        onBackPressedDispatcher.addCallback {
            Toast.makeText(this@XzLoadingActivity, "wait a moment", Toast.LENGTH_SHORT).show()
        }
    }
    private var isPasue = false
    override fun onPause() {
        super.onPause()
        isPasue = false

    }

    override fun onResume() {
        super.onResume()
        isPasue = true
        playAnimal()
    }
    private fun playAnimal() {
        lifecycleScope.launch(Dispatchers.Main) {
            binding.root.postDelayed({
                animaEnd()
            }, Random.nextLong(3000, 5000))
        }
    }
    private fun animaEnd(){
        if (isPasue) {
            AdmobMaxHelper.admobMaxShowInterstitialAd(this){
                startActivity(Intent(this, WebBrowserActivity::class.java).apply {
                    putExtra("url", "https://www.horoscope.com/us/index.aspx")
                })
                finish()
            }
        }
    }
}
