package com.keyboard.journey

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.applovin.mediation.MaxError
import com.gyf.immersionbar.ktx.immersionBar
import com.keyboard.journey.ad.AdLoadListener
import com.keyboard.journey.ad.AdShowFailed
import com.keyboard.journey.ad.AdShowListener
import com.keyboard.journey.ad.MaxAdsInsUtil
import com.keyboard.journey.databinding.JourneyStartActivityBinding
import org.libpag.PAGFile
import java.util.Timer
import java.util.TimerTask

class JourneyStartActivity : AppCompatActivity() {
    companion object {
        const val SPLASH_TIME_OUT: Long = 15000
    }

    private lateinit var binding: JourneyStartActivityBinding
    private var timer: DelayTimer? = null // 可变变量，初始化为 null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = JourneyStartActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        immersionBar {
            fullScreen(true)
            statusBarDarkFont(false)
        }
        timer = DelayTimer(SPLASH_TIME_OUT, callback = {
            LogUtil.logMsgD("loaded ing")
            if (adCanShow()) {
                timer?.stop()
                showAd()
            }
        }, com = {
            startMain()
        })
        loadAd()
        if (timer != null) {
            timer?.start()
        }
        loadingPlay()
    }

    private fun loadAd() {
        MaxAdsInsUtil.loadMaxAd(
            this,
            MaxAdsInsUtil.Placement.SPLASH_AD_ID, object : AdLoadListener {
                override fun loaded() {
                    LogUtil.logMsgD("loaded")
                }

                override fun loadFailed(error: MaxError?) {
                    LogUtil.logMsgD(error.toString())
                    startMain()
                    timer?.stop()
                }
            })
        MaxAdsInsUtil.loadSplashAllAd(this)
    }

    private fun adCanShow(): Boolean {
        return MaxAdsInsUtil.canShowAd(MaxAdsInsUtil.Placement.SPLASH_AD_ID)
    }

    private fun showAd() {
        MaxAdsInsUtil.showMaxAd(
            this,
            MaxAdsInsUtil.Placement.SPLASH_AD_ID,
            object : AdShowListener {
                override fun onAdShown() {

                }

                override fun onAdShowFailed(error: AdShowFailed?) {
                    startMain()
                }

                override fun onAdClosed() {
                    startMain()
                }
            })
    }

    private fun startMain() {
        val intent = Intent(this, JourneyMActivity::class.java)
        startActivity(intent)
        finish()
    }

    private fun loadingPlay() {
        val pagFile = PAGFile.Load(assets, "loading.pag")
        binding.pagView.composition = pagFile
        binding.pagView.setRepeatCount(0)
        binding.pagView.play()
    }

    private fun loadingClose() {
        if (binding.pagView.isPlaying) {
            binding.pagView.stop()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        loadingClose()
    }
}

class DelayTimer(
    private val totalDelayInMillis: Long,
    private val callback: () -> Unit,
    private val com: () -> Unit
) {

    private val timer = Timer()
    private var elapsedTime: Long = 0

    fun start() {
        timer.scheduleAtFixedRate(object : TimerTask() {
            override fun run() {
                if (elapsedTime < totalDelayInMillis) {
                    callback.invoke()
                    elapsedTime += 1000 // 每秒钟递增
                } else {
                    com.invoke()
                    stop()
                }
            }
        }, 0, 1000) // 每秒钟执行一次
    }

    fun stop() {
        timer.cancel()
    }
}