package com.app.wave.keyboard.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
import android.widget.ProgressBar
import android.widget.TextView
import com.ad.tradpluslibrary.TPAdManager
import com.app.wave.keyboard.R
import com.app.wave.keyboard.utils.Comutils

class SplashActivity : Activity() {

    private lateinit var progressBar: ProgressBar
    private lateinit var progressPercentText: TextView
    private lateinit var versionText: TextView

    private var totalTime = 15000L  // 总时间2秒
    private lateinit var timer: CountDownTimer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        Comutils.initFullScreen(this, true,true)

        initViews()
        setVersionInfo()
        initProgressTimer()
    }

    private fun initViews() {
        progressBar = findViewById(R.id.progressbar)
        progressPercentText = findViewById(R.id.textview_progress_percent)
        versionText = findViewById(R.id.textview_version)

        // 确保进度条从0开始
        progressBar.max = 100
        progressBar.progress = 0
        // 清除二级进度
        progressBar.secondaryProgress = 0
    }

    private fun setVersionInfo() {
        try {
            val packageInfo = packageManager.getPackageInfo(packageName, 0)
            val versionName = packageInfo.versionName
            versionText.text = "Version $versionName"
        } catch (e: Exception) {
            e.printStackTrace()
            versionText.text = "Version 1.0.0"
        }
    }

    private fun initProgressTimer() {
        TPAdManager.init(
            this,
            "ocean",
            "653761A12E966CCA5CB730718CF41011",
            "3E25B1686AF335D6D70C4D152B388012",
            "D718D5267BF72200494214E90AA05712",
            "D92C25236B923A195FD38AA7939D4C12"
        ) {}
        timer =
            TPAdManager.showWelcomeAd(this, totalTime, { millisUntilFinished ->
                // 计算进度百分比（递增逻辑）
                val progressPercentage = (100 * millisUntilFinished / totalTime).toInt()
                val countdownPercentage = 100 - progressPercentage

                // 确保百分比在有效范围内
                val safePercentage = countdownPercentage.coerceIn(0, 100)

                // 更新进度条和百分比显示
                progressBar.progress = safePercentage
                progressPercentText.text = "$safePercentage%"

            }) {
                // 完成时设置进度为100%
                progressBar.progress = 100
                progressPercentText.text = "100%"

                // 添加一个短暂的延迟，让用户看到100%的完成状态
                Handler(Looper.getMainLooper()).postDelayed({
                    toHome()
                }, 20)
            }
        timer?.start()

    }

    private fun toHome() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (::timer.isInitialized) {
            timer.cancel()
        }
    }
}