package com.wall.turing.wallpapers.accctivities

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import com.tradplus.ads.base.bean.TPAdInfo
import com.wall.turing.wallpapers.R
import com.wall.turing.wallpapers.ad.AdShowFailed
import com.wall.turing.wallpapers.ad.AdsInsUtil
import com.wall.turing.wallpapers.ad.LoadListener
import com.wall.turing.wallpapers.ad.ShowListener
import com.wall.turing.wallpapers.databinding.TuringSplashBinding

class SplashActivity : BaseActivity() {

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

    private lateinit var binding: TuringSplashBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = TuringSplashBinding.inflate(layoutInflater)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
        windowInsetsController.isAppearanceLightStatusBars = false//状态栏文字颜色
        loadSplashAd()
    }


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

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

        AdsInsUtil.loadAd(
            act = this,
            adID =AdsInsUtil.Placement.TOP_ON_AD_THREE,
            loadListener = object : LoadListener {
                override fun loaded(ad: TPAdInfo) {
                    adHandler.removeCallbacks(adTimeoutRunnable)
                    showSplashAd()
                }

                override fun loadFailed(error: String) {
                    adHandler.removeCallbacks(adTimeoutRunnable)
                    navigateToNext() // 加载失败直接跳转
                }
            })
    }

    private fun showSplashAd() {
        AdsInsUtil.showAd(
            act = this,
            adID = AdsInsUtil.Placement.TOP_ON_AD_THREE,
            listener = object : ShowListener {
                override fun onAdShown(ad: TPAdInfo?) {
                }

                override fun onAdShowFailed(error: AdShowFailed?) {
                    navigateToNext() // 展示失败也直接跳转
                }

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

    private var hasNavigated = false

    //保证只跳转一次
    private fun navigateToNext() {
        if (hasNavigated) return
        hasNavigated = true
        navigateToMainActivity()
    }

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

    }

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

    @SuppressLint("QueryPermissionsNeeded")
    private fun navigateToMainActivity() {
        try {
            val intent = Intent(this, PageMainActivity::class.java)
            startActivity(intent)
        } catch (e: Exception) {
            Toast.makeText(this, "跳转失败", Toast.LENGTH_SHORT).show()
            e.printStackTrace()
            finish()
        }
    }

}