package com.base.datarecovery.activity.newfunction

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.datarecovery.ads.AdmobMaxHelper
import com.base.datarecovery.databinding.ActivityLayoutXingzuoLoadingBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.utils.BarUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.random.Random

class XingzuoLoadingActivity : 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,1)
        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@XingzuoLoadingActivity, "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()
            }
        }
    }
}
