package cl.homelogic.platform.designsystem.components.atoms

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import cl.homelogic.platform.designsystem.foundations.Dimens
import cl.homelogic.platform.designsystem.foundations.Padding

@Composable
fun SimpleWindow(
    modifier: Modifier = Modifier.fillMaxSize(),
    unbounded: Boolean = false,
    topBar: @Composable () -> Unit = {},
    content: @Composable ColumnScope.() -> Unit,
) {
    val imeVisible by rememberUpdatedState(
        WindowInsets.ime.getBottom(LocalDensity.current) > 0,
    )

    Scaffold(
        topBar = topBar,
        contentWindowInsets = if (!imeVisible) {
            WindowInsets(
                left = Dimens.at0,
                top = Dimens.at0,
                right = Dimens.at0,
                bottom = Dimens.at0,
            )
        } else {
            WindowInsets.ime
        },
        modifier = modifier,
    ) { padding ->
        Surface(modifier = Modifier.padding(padding)) {
            Column(
                modifier = Modifier.padding(horizontal = Padding.big).takeIf { !unbounded }
                    ?: Modifier,
            ) {
                content()
                BottomSafeZone()
            }
        }
    }
}
