package invalid.lena.rsend

import android.content.res.Configuration
import android.os.Build
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat

// Android 15 (target SDK 35) forces the window edge-to-edge: content otherwise
// draws behind the status and navigation bars. Pad the screen root (the toolbar
// plus the scroll content) by the system-bar insets so the toolbar sits below
// the status bar and the content stays clear of the navigation bar and keyboard.
// If the decor already insets the content the listener simply sees zero insets,
// so this is a no-op rather than double padding. Call once after setContentView.
fun AppCompatActivity.fitSystemBars() {
    val root = findViewById<View>(R.id.appRoot)
    ViewCompat.setOnApplyWindowInsetsListener(root) { v, insets ->
        val bars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
        val ime = insets.getInsets(WindowInsetsCompat.Type.ime())
        v.setPadding(bars.left, bars.top, bars.right, maxOf(bars.bottom, ime.bottom))
        WindowInsetsCompat.CONSUMED
    }
    // The bars are now transparent and show the window background instead of the
    // old solid bar colour, so match the icon contrast to day/night.
    if (Build.VERSION.SDK_INT >= 35) {
        val night = resources.configuration.uiMode and
            Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
        val controller = WindowCompat.getInsetsController(window, window.decorView)
        controller.isAppearanceLightStatusBars = !night
        controller.isAppearanceLightNavigationBars = !night
    }
}
