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

import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.Color
import android.view.animation.LinearInterpolator
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.ads.nativead.NativeAd
import com.swiftcleaner.chovey.R
import com.swiftcleaner.chovey.business.admob.AdmobManager
import com.swiftcleaner.chovey.business.admob.AdmobManager.isShowAdInter
import com.swiftcleaner.chovey.business.admob.AdmobManager.showInterstitialAd
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() {
        AdmobManager.showNativeAd(this@XzLoadingActivity, { nativeAd: NativeAd? ->
            binding.idFlNative.removeAllViews()
            nativeAd?.let { binding.idFlNative.setNativeAd(it, R.layout.layout_ad_native) }
            null
        }, null, null)

        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 (isShowAdInter()) {
                    showInterstitialAd(
                        this@XzLoadingActivity,
                        {
                            startIntent()
                            null
                        },
                        {
                            null
                        },
                        {
                            startIntent()
                            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()
//            }
//        }
//    }

}