package com.all.pdfreader.pdf.reader.ui.act

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.activity.OnBackPressedCallback
import androidx.core.view.doOnPreDraw
import com.all.pdfreader.pdf.reader.R
import com.all.pdfreader.pdf.reader.ad.AdsInsUtil
import com.all.pdfreader.pdf.reader.ad.LoadListener
import com.all.pdfreader.pdf.reader.ad.ShowListener
import com.all.pdfreader.pdf.reader.databinding.ActivitySplashBinding
import com.all.pdfreader.pdf.reader.util.AnalyticsUtils
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar

@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseActivity() {

    override val TAG: String = "SplashActivity"

    private lateinit var binding: ActivitySplashBinding

    companion object {
        private const val AD_TIMEOUT = 15000L // 广告加载超时时间 15 秒
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySplashBinding.inflate(layoutInflater)
        setupImmersionBar {
            fullScreen(true)
            statusBarDarkFont(true)
            hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
        }
        // 设置启动页布局
        setContentView(binding.root)
        AnalyticsUtils.logEvent(AnalyticsUtils.Event.SPLASH_VISIBLE)
        loadSplashAd()

    }

    private val adHandler = Handler(Looper.getMainLooper())
    private val adTimeoutRunnable = Runnable {
        navigateToNext() // 超时直接跳转
    }

    private fun loadSplashAd() {
        adHandler.postDelayed(adTimeoutRunnable, AD_TIMEOUT)

        AdsInsUtil.loadAd(
            act = this,
            adPlacement = AdsInsUtil.AdPlacement.SPL_AND_INTO_HOME,
            loadListener = object : LoadListener {
                override fun loaded(ad: InterstitialAd) {
                    logDebug("loaded 启动页广告加载成功")
                    adHandler.removeCallbacks(adTimeoutRunnable)
                    showSplashAd()
                }

                override fun loadFailed(string: String) {
                    logDebug("loadFailed->$string")
                    adHandler.removeCallbacks(adTimeoutRunnable)
                    navigateToNext() // 加载失败直接跳转
                }
            })
    }

    private fun showSplashAd() {
        AdsInsUtil.showAd(
            act = this,
            adPlacement = AdsInsUtil.AdPlacement.SPL_AND_INTO_HOME,
            listener = object : ShowListener {
                override fun onAdShown() {
                    // 可以记录日志
                }

                override fun onAdShowFailed(string: String) {
                    navigateToNext() // 展示失败也直接跳转
                }

                override fun onAdClosed() {
                    navigateToNext() // 广告关闭后跳转
                }
            })
    }

    private var hasNavigated = false

    //保证只跳转一次
    private fun navigateToNext() {
        if (hasNavigated) return
        hasNavigated = true
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    override fun shouldInterceptBackPress(): Boolean = true
    override fun onInterceptBackPressed() {

    }

    override fun onDestroy() {
        super.onDestroy()
        adHandler.removeCallbacks(adTimeoutRunnable)
    }
}