package com.stand.standapp

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity

class SplashActivity : AppCompatActivity() {
    private val handler = Handler(Looper.getMainLooper())
    private var checkCount = 0
    private val maxChecks = 20 // 最多检查20次(2秒)

    private val checkInitRunnable = object : Runnable {
        override fun run() {
            if (MyApplication.pNative != null || checkCount >= maxChecks) {
                navigateToLogin()
            } else {
                checkCount++
                handler.postDelayed(this, 100)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        // 异步检查PTT JNI初始化状态，避免硬等待2秒
        handler.post(checkInitRunnable)
    }

    private fun navigateToLogin() {
        if (isFinishing || isDestroyed) return
        startActivity(Intent(this, LoginActivity::class.java))
        finish()
    }

    override fun onDestroy() {
        handler.removeCallbacks(checkInitRunnable)
        super.onDestroy()
    }
}
