package com.adealink.weparty.util

import android.app.Activity
import android.os.Build
import androidx.core.view.WindowInsetsCompat
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.commonui.ext.contentView
import com.adealink.weparty.commonui.ext.onWindowInsets

fun Activity?.applyImmersive(onMargin: (top: Int) -> Unit) {
    if (this == null) {
        return
    }
    //多窗口/画中画
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        if (isInMultiWindowMode || isInPictureInPictureMode) {
            val contentView = contentView()
            if (contentView == null) {
                onMargin.invoke(0)
                return
            }
            contentView.onWindowInsets { view, insets ->
                val systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
                onMargin.invoke(systemBarInsets.top)
                insets
            }
            return
        }
    }

    onMargin.invoke(statusBarHeight())
}

fun Activity?.isMultiWindowMode(): Boolean {
    if (this == null) {
        return false
    }

    //多窗口/画中画
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return isInMultiWindowMode || isInPictureInPictureMode
    }
    return false
}