package de.tsschulz.tt_tagebuch.app.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties

@Composable
fun StableAlertDialog(
    onDismissRequest: () -> Unit,
    title: @Composable (() -> Unit)? = null,
    text: @Composable (() -> Unit)? = null,
    confirmButton: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    dismissButton: @Composable (() -> Unit)? = null,
    properties: DialogProperties = DialogProperties(),
) {
    StableAlertDialogFrame(
        onDismissRequest = onDismissRequest,
        title = title,
        text = text,
        modifier = modifier,
        properties = properties,
        buttons = {
            dismissButton?.invoke()
            confirmButton()
        },
    )
}

@Composable
fun StableAlertDialog(
    onDismissRequest: () -> Unit,
    buttons: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    title: @Composable (() -> Unit)? = null,
    text: @Composable (() -> Unit)? = null,
    properties: DialogProperties = DialogProperties(),
) {
    StableAlertDialogFrame(
        onDismissRequest = onDismissRequest,
        title = title,
        text = text,
        modifier = modifier,
        properties = properties,
        buttons = buttons,
    )
}

@Composable
private fun StableAlertDialogFrame(
    onDismissRequest: () -> Unit,
    title: @Composable (() -> Unit)?,
    text: @Composable (() -> Unit)?,
    modifier: Modifier,
    properties: DialogProperties,
    buttons: @Composable () -> Unit,
) {
    val maxHeight = (LocalConfiguration.current.screenHeightDp * 0.86f).dp
    Dialog(onDismissRequest = onDismissRequest, properties = properties) {
        Card(
            modifier = modifier
                .fillMaxWidth()
                .widthIn(max = 560.dp)
                .heightIn(max = maxHeight),
            elevation = 8.dp,
        ) {
            Column(modifier = Modifier.padding(16.dp)) {
                if (title != null) {
                    androidx.compose.material.ProvideTextStyle(
                        MaterialTheme.typography.h6.copy(fontWeight = FontWeight.SemiBold),
                    ) {
                        title()
                    }
                }
                if (text != null) {
                    Column(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = if (title != null) 12.dp else 0.dp)
                            .heightIn(max = maxHeight - 112.dp),
                    ) {
                        text()
                    }
                }
                Row(
                    modifier = Modifier.fillMaxWidth().padding(top = 12.dp),
                    horizontalArrangement = Arrangement.End,
                    verticalAlignment = Alignment.CenterVertically,
                ) {
                    buttons()
                }
            }
        }
    }
}
