package eu.gaudian.translator.view.hints

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.SheetState
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.view.composable.AppButton
import kotlinx.coroutines.launch

/**
 * A shared modal bottom sheet for displaying hint content.
 * The content area is scrollable if it exceeds the available space.
 *
 * @param onDismissRequest Called when the user requests to dismiss the sheet.
 * @param sheetState The state of the bottom sheet.
 * @param content The hint content to display inside the sheet.
 */
@Composable
fun HintBottomSheet(
    onDismissRequest: () -> Unit,
    sheetState: SheetState,
    content: Hint,
) {
    val scope = rememberCoroutineScope()
    ModalBottomSheet(
        onDismissRequest = onDismissRequest,
        sheetState = rememberModalBottomSheetState(
            skipPartiallyExpanded = true
        )
    ) {


        Column(modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)) {

            Column(
                modifier = Modifier
                    .weight(1f, fill = false)
                    .verticalScroll(rememberScrollState())
            ) {
                content.Render()
            }

            Spacer(modifier = Modifier.height(16.dp))
            AppButton(
                onClick = {
                    scope.launch { sheetState.hide() }.invokeOnCompletion {
                        if (!sheetState.isVisible) {
                            onDismissRequest()
                        }
                    }
                },
                modifier = Modifier
                    .align(Alignment.End)
                    .padding(bottom = 8.dp)
            ) {
                Text(stringResource(R.string.got_it))
            }
        }
    }
}