package com.example.base.extensions

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Rect
import android.view.View
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity


fun Activity.startActivity(clazz: Class<out Activity>) {
    val intent = Intent(this, clazz)
    this.startActivity(intent)
}

fun Fragment.observeKeyboardChange(onChange: (isShowing: Boolean) -> Unit) {
    requireActivity().observeKeyboardChange(onChange)
}

fun Activity.observeKeyboardChange(onChange: (isShowing: Boolean) -> Unit) {
    val rootView = this.window.decorView
    val r = Rect()
    var lastHeight = 0
    rootView.viewTreeObserver.addOnGlobalLayoutListener {
        rootView.getWindowVisibleDisplayFrame(r)
        val height = r.height()
        if (lastHeight == 0) {
            lastHeight = height
        } else {
            val diff = lastHeight - height
            if (diff > 200) {
                onChange(true)
                lastHeight = height
            } else if (diff < -200) {
                onChange(false)
                lastHeight = height
            }
        }
    }
}

/**
 * 当前键盘显示状态
 */
fun FragmentActivity.isKeyboardShown(): Boolean {
    val rootView = this.window.decorView.findViewById<View>(android.R.id.content)

    val softKeyboardHeight = 100
    val r = Rect()
    rootView.getWindowVisibleDisplayFrame(r)
    val dm = rootView.resources.displayMetrics
    val heightDiff = rootView.bottom - r.bottom
    return heightDiff > softKeyboardHeight * dm.density

}

/**
 * WindowInsetsController
 * WindowInsetsController是一个接口类，它主要作用就是控制窗口行为，此类在Android R（API 30）添加，意在简化原先代码动态修改窗口SystemBars（StatusBar&NavigationBar）表现。
 * 链接： https://juejin.cn/post/6940048488071856164
 *     fun initSystemBarsByAndroidX() {
 *          var controller = ViewCompat.getWindowInsetsController(window.decorView)
 *          // 设置状态栏反色
 *          controller?.isAppearanceLightStatusBars = true
 *          // 取消状态栏反色
 *          controller?.isAppearanceLightStatusBars = false
 *          // 设置导航栏反色
 *          controller?.isAppearanceLightNavigationBars = true
 *          // 取消导航栏反色
 *          controller?.isAppearanceLightNavigationBars = false
 *          // 隐藏状态栏
 *          controller?.hide(WindowInsets.Type.statusBars())
 *          // 显示状态栏
 *          controller?.show(WindowInsets.Type.statusBars())
 *          // 隐藏导航栏
 *          controller?.hide(WindowInsets.Type.navigationBars())
 *          // 显示导航栏
 *          controller?.show(WindowInsets.Type.navigationBars())
 *          // 同时隐藏状态栏和导航栏
 *          controller?.hide(WindowInsets.Type.systemBars())
 *          // 同时隐藏状态栏和导航栏
 *          controller?.show(WindowInsets.Type.systemBars())
 *      }
 */
@SuppressLint("WrongConstant")
fun Activity.setStatusBarLight(isLight: Boolean) {
    val controller = ViewCompat.getWindowInsetsController(window.decorView)
    // 设置状态栏反色
    controller?.isAppearanceLightStatusBars = isLight
}

fun Fragment.setStatusBarLight(isLight: Boolean) {
    activity?.setStatusBarLight(isLight)

}

/**
 * 获取当前是否为深色模式
 * 深色模式的值为:0x21
 * 浅色模式的值为:0x11
 * @return true 为是深色模式   false为不是深色模式
 */
fun Context.isDarkMode(): Boolean {
    return resources.configuration.uiMode == 0x21
}
