package llc.arma.ble.app.ui.common

import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.remember

val LocalBottomDialogState = compositionLocalOf<BottomState?> { null }

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun rememberBottomDialogState(): BottomDialogState {

    val state = LocalBottomDialogState.current

    return remember {
        BottomDialogState(
            sheetState = state?.sheetState,
            setContent = state?.setContent ?: { }
        )
    }
}

class BottomState @OptIn(ExperimentalMaterialApi::class) constructor(
    val sheetState: ModalBottomSheetState?,
    val setContent: (@Composable () -> Unit) -> Unit,
)

class BottomDialogState @OptIn(ExperimentalMaterialApi::class) constructor(
    val sheetState: ModalBottomSheetState?,
    val setContent: (@Composable () -> Unit) -> Unit,
) {

    @OptIn(ExperimentalMaterialApi::class)
    suspend fun show(
        content: @Composable () -> Unit
    ){
        setContent(content)
        //if(sheetState?.currentValue != ModalBottomSheetValue.Expanded)
        sheetState?.show()
    }

    @OptIn(ExperimentalMaterialApi::class)
    suspend fun hide(){
        sheetState?.hide()
        setContent { }
    }

}