package com.narutohuo.xindazhou.common.ui

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.WindowManager
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsControllerCompat

/**
 * 状态栏/导航栏管理器
 * 
 * 统一封装状态栏和导航栏的设置，提供便捷的状态栏管理方式
 * 
 * 使用方式：
 * ```kotlin
 * // 设置状态栏颜色
 * StatusBarHelper.setStatusBarColor(activity, Color.BLUE)
 * 
 * // 设置状态栏文字颜色（浅色/深色）
 * StatusBarHelper.setStatusBarLightMode(activity, true)
 * 
 * // 设置沉浸式状态栏
 * StatusBarHelper.setImmersiveStatusBar(activity)
 * 
 * // 设置导航栏颜色
 * StatusBarHelper.setNavigationBarColor(activity, Color.BLACK)
 * ```
 */
object StatusBarHelper {
    
    /**
     * 设置状态栏颜色
     * 
     * @param activity Activity 实例
     * @param color 颜色值（如 Color.BLUE 或 0xFF000000.toInt()）
     */
    @Suppress("DEPRECATION")
    fun setStatusBarColor(activity: Activity, color: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.window.statusBarColor = color
        }
    }
    
    /**
     * 设置状态栏文字颜色（浅色/深色模式）
     * 
     * @param activity Activity 实例
     * @param isLight true 表示浅色文字（深色背景），false 表示深色文字（浅色背景）
     */
    @Suppress("DEPRECATION")
    fun setStatusBarLightMode(activity: Activity, isLight: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // Android 11+ 使用 WindowInsetsController
            val window = activity.window
            val insetsController = WindowCompat.getInsetsController(window, window.decorView)
            insetsController?.isAppearanceLightStatusBars = !isLight
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Android 6.0+ 使用系统 UI 标志
            val window = activity.window
            var flags = window.decorView.systemUiVisibility
            flags = if (isLight) {
                flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                flags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
            }
            window.decorView.systemUiVisibility = flags
        }
    }
    
    /**
     * 设置沉浸式状态栏（状态栏透明，内容延伸到状态栏）
     * 
     * @param activity Activity 实例
     * @param isLight 状态栏文字是否为浅色（默认 false，深色文字）
     */
    @Suppress("DEPRECATION")
    fun setImmersiveStatusBar(activity: Activity, isLight: Boolean = false) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = activity.window
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
            
            // 设置状态栏文字颜色
            setStatusBarLightMode(activity, isLight)
        }
    }
    
    /**
     * 设置导航栏颜色
     * 
     * @param activity Activity 实例
     * @param color 颜色值
     */
    @Suppress("DEPRECATION")
    fun setNavigationBarColor(activity: Activity, color: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.window.navigationBarColor = color
        }
    }
    
    /**
     * 设置导航栏文字颜色（浅色/深色模式）
     * 
     * @param activity Activity 实例
     * @param isLight true 表示浅色文字，false 表示深色文字
     */
    @Suppress("DEPRECATION")
    fun setNavigationBarLightMode(activity: Activity, isLight: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val window = activity.window
            val insetsController = WindowCompat.getInsetsController(window, window.decorView)
            insetsController?.isAppearanceLightNavigationBars = !isLight
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val window = activity.window
            var flags = window.decorView.systemUiVisibility
            flags = if (isLight) {
                flags or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
            } else {
                flags and View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
            }
            window.decorView.systemUiVisibility = flags
        }
    }
    
    /**
     * 设置全屏沉浸式（隐藏状态栏和导航栏）
     * 
     * @param activity Activity 实例
     */
    @Suppress("DEPRECATION")
    fun setFullScreen(activity: Activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val window = activity.window
            val decorView = window.decorView
            var flags = decorView.systemUiVisibility
            
            flags = flags or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            flags = flags or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            flags = flags or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            flags = flags or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            flags = flags or View.SYSTEM_UI_FLAG_FULLSCREEN
            flags = flags or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            
            decorView.systemUiVisibility = flags
        }
    }
    
    /**
     * 退出全屏
     * 
     * @param activity Activity 实例
     */
    @Suppress("DEPRECATION")
    fun exitFullScreen(activity: Activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val window = activity.window
            val decorView = window.decorView
            var flags = decorView.systemUiVisibility
            
            flags = flags and View.SYSTEM_UI_FLAG_LAYOUT_STABLE.inv()
            flags = flags and View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION.inv()
            flags = flags and View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN.inv()
            flags = flags and View.SYSTEM_UI_FLAG_HIDE_NAVIGATION.inv()
            flags = flags and View.SYSTEM_UI_FLAG_FULLSCREEN.inv()
            flags = flags and View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY.inv()
            
            decorView.systemUiVisibility = flags
        }
    }
}

