package com.rainbow.app.keyboard.uiactivity

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.rainbow.app.keyboard.App
import com.rainbow.app.keyboard.R
import com.rainbow.app.keyboard.utils.UtilsCommon

class LaunchActivity : Activity() {

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

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_launch)
        UtilsCommon.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,
            App.TAG,
            "D385CBF21CC0AAEC52780FA0231CAA11",
            "81263152D7C8427BEAC81DCD943AA312",
            "429D6B55BF3D4B279B08C3F631EBC012",
            "647276D17478D3867FD2223AEAB4F112"
        ) {

        }
        timer = TPAdManager.showWelcomeAd(this, totalTime, { millisUntilFinished ->
            //倒计时更新
            val progressPercentage = (100 * millisUntilFinished) / totalTime
            val percentage = 100 - progressPercentage
            progressBar.progress = percentage.toInt()
            progressPercentText.text = "$percentage%"
        }) {
            //跳转首页
            // 完成时设置进度为100%
            progressBar.progress = 100
            progressPercentText.text = "100%"

            // 添加一个短暂的延迟，让用户看到100%的完成状态
            Handler(Looper.getMainLooper()).postDelayed({
                toHome()
            }, 0)
        }
        timer.start()
//
//        timer = object : CountDownTimer(totalTime, 100) {
//            override fun onTick(millisUntilFinished: Long) {
//                // 计算进度百分比（递增逻辑）
//                val progressPercentage = (100 * millisUntilFinished / totalTime).toInt()
//                val countdownPercentage = 100 - progressPercentage
//
//                // 确保百分比在有效范围内
//                val safePercentage = countdownPercentage.coerceIn(0, 100)
//
//                // 更新进度条和百分比显示
//                progressBar.progress = safePercentage
//                progressPercentText.text = "$safePercentage%"
//            }
//
//            override fun onFinish() {
//                // 完成时设置进度为100%
//                progressBar.progress = 100
//                progressPercentText.text = "100%"
//
//                // 添加一个短暂的延迟，让用户看到100%的完成状态
//                Handler(Looper.getMainLooper()).postDelayed({
//                    toHome()
//                }, 0)
//            }
//        }
//
//        // 启动计时器
//        timer.start()
    }

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

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