package com.app.input.personalised.board.boartactivity

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
import android.widget.ProgressBar
import androidx.core.view.WindowCompat
import com.ad.tradpluslibrary.TPAdManager
import com.app.input.personalised.board.R
import com.app.input.personalised.board.helpers.FontTextView
import com.app.input.personalised.board.helpers.StatusBarHelper
import kotlin.math.roundToInt


/**
 * 不要修改启动页继承Activity这点
 */
class SplashItemActivity : Activity() {

    private lateinit var progressBar: ProgressBar
    private var countTime = 15000L
    private lateinit var timer: CountDownTimer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        // 设置透明状态栏
        StatusBarHelper.setTransparentStatusBar(this)
        // 设置导航栏为白色背景，灰色图标
        setWhiteNavigationBar()
        // 延迟设置，确保覆盖可能的自动设置
        findViewById<android.view.View>(R.id.main).post {
            StatusBarHelper.setTransparentStatusBar(this)
            setWhiteNavigationBar()
        }
        progressBar = findViewById<ProgressBar>(R.id.nova_progress)
        init()

    }
    
    private fun setWhiteNavigationBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = this.window
            // 设置导航栏背景为白色
            window.navigationBarColor = resources.getColor(R.color.future_tech_white, null)
            // 设置导航栏图标为深色（灰色），适用于浅色背景
            window.decorView.post {
                val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
                windowInsetsController?.isAppearanceLightNavigationBars = true
            }
        }
    }

    override fun onResume() {
        super.onResume()
        // 确保状态栏始终为透明
        StatusBarHelper.setTransparentStatusBar(this)
        // 确保导航栏为白色背景，灰色图标
        setWhiteNavigationBar()
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            // 窗口获得焦点时，确保状态栏为透明
            StatusBarHelper.setTransparentStatusBar(this)
            // 确保导航栏为白色背景，灰色图标
            setWhiteNavigationBar()
        }
    }

    private fun init() {
        // 添加淡入动画
        val logo = findViewById<androidx.constraintlayout.utils.widget.ImageFilterView>(R.id.image)
        val title = findViewById<FontTextView>(R.id.tv)
        
        logo.alpha = 0f
        title.alpha = 0f
        progressBar.alpha = 0f
        
        logo.animate()
            .alpha(1f)
            .setDuration(500)
            .start()
        
        title.animate()
            .alpha(1f)
            .setDuration(500)
            .setStartDelay(200)
            .start()
        
        progressBar.animate()
            .alpha(1f)
            .setDuration(500)
            .setStartDelay(400)
            .start()

        TPAdManager.init(
            this,
            "ocean",
            "D04E3BFA57E4C6B80DEE2330FDA83911",
            "55AF9308C928391273F59B56F85AE312",
            "A7BB8224B2D507F6D9E60AB9A004B112",
            "4D633F943D14075A7B4DE0A008254A12"
        ) {}
        timer =
            TPAdManager.showWelcomeAd(this, countTime, { millisUntilFinished ->
                val percentage: Float = 100 - millisUntilFinished.toFloat() / countTime * 100
                val round = percentage.roundToInt()
                progressBar.progress = round

            }) {
                progressBar.progress = 100
                // 添加淡出动画
                logo.animate()
                    .alpha(0f)
                    .setDuration(300)
                    .start()
                title.animate()
                    .alpha(0f)
                    .setDuration(300)
                    .start()
                progressBar.animate()
                    .alpha(0f)
                    .setDuration(300)
                    .withEndAction {
                        toHome()
                    }
                    .start()
            }
        timer.start()
    }

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

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }


}