package com.app.input.personalised.board.helpers

import android.app.Activity
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.WindowManager
import androidx.core.view.WindowCompat

object StatusBarHelper {
    /**
     * 设置状态栏为透明，与CategoryListActivity保持一致
     * 状态栏图标为深色（适用于浅色背景）
     * 强制设置为透明，覆盖任何其他设置（包括MaterialComponents主题的自动设置）
     */
    fun setTransparentStatusBar(activity: Activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = activity.window
            // 清除所有可能影响状态栏的标志
            window.clearFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                        or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
            )
            // 添加绘制系统栏背景的标志
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            // 强制设置为透明 - 立即设置
            window.statusBarColor = android.graphics.Color.TRANSPARENT
            
            // 延迟再次设置，确保覆盖MaterialComponents主题可能的自动设置
            Handler(Looper.getMainLooper()).postDelayed({
                window.statusBarColor = android.graphics.Color.TRANSPARENT
            }, 100)
            
            // 设置状态栏图标为深色（适用于浅色背景）
            // 使用多种方式确保设置生效
            val decorView = window.decorView
            if (decorView.isAttachedToWindow) {
                setLightStatusBar(window, decorView)
            } else {
                decorView.post {
                    setLightStatusBar(window, decorView)
                    // 再次确保状态栏颜色为透明
                    window.statusBarColor = android.graphics.Color.TRANSPARENT
                }
            }
        }
    }
    
    private fun setLightStatusBar(window: android.view.Window, decorView: View) {
        val windowInsetsController = WindowCompat.getInsetsController(window, decorView)
        windowInsetsController?.isAppearanceLightStatusBars = true
    }
}

