package com.swiftcleaner.chovey.view.function.constellation

import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.Color
import android.util.Log
import android.view.animation.LinearInterpolator
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.lifecycle.lifecycleScope
import com.clean.file.b.business.newAdmob.AdmobHelps
import com.clean.file.b.business.newAdmob.AdmobHelps.isShowAdNativeBottom
import com.clean.file.b.business.newAdmob.AdmobHelps.showInterstitialAd
import com.clean.file.b.business.newAdmob.AdmobHelps.showNativeAd
import com.swiftcleaner.chovey.R
import com.swiftcleaner.chovey.databinding.ActivityXzLoadingBinding
import com.swiftcleaner.chovey.view.BaseActivityKotlin
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.random.Random

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

    override fun initView() {
        showNativeAd(
            this,
            binding.flAd,
            R.layout.layout_ad_native_small,
            isShowAdNativeBottom(),
            false
        )

        BarUtils.setStatusBarLightMode(this, false)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)

        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(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                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({
                if (AdmobHelps.isShowAdInter()) {
                    showInterstitialAd(
                        this@XzLoadingActivity,
                        {
                            startIntent()
                            // 当广告被关闭时的逻辑
                            Log.d("AdmobHelps", "Interstitial Ad was dismissed.")
                            null
                        },
                        {
                            // 当广告展示完成时的逻辑
                            Log.d("AdmobHelps", "Interstitial Ad was shown.")
                            null
                        },
                        {
                            startIntent()
                            // 当广告加载失败时的逻辑
                            Log.d("AdmobHelps", "Interstitial Ad failed to load.")
                            null
                        },
                        false
                    )
                } else {
                    startIntent()
                }
            }, Random.nextLong(3000, 5000))
        }
    }

    private fun startIntent() {
        val intent = Intent(this@XzLoadingActivity, WebBrowserActivity::class.java)
        intent.putExtra("url", "https://www.horoscope.com/us/index.aspx")
        startActivity(intent)
        finish()
    }
//    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()
//            }
//        }
//    }

}