package com.isidroid.compose_components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.AlertDialogDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.DialogProperties


@Composable
fun DefaultAlertDialog(
    message: String,
    modifier: Modifier = Modifier,
    confirmButtonText: String? = null,
    onConfirmClick: () -> Unit = {},
    dismissButtonText: String? = null,
    onDismissClick: () -> Unit = {},
    dismissOnBackPress: Boolean = false,
    dismissOnClickOutside: Boolean = false,
    title: String? = null,
    onDismissRequest: () -> Unit = {}
) {
    var shouldShowDialog by remember { mutableStateOf(true) }
    if (!shouldShowDialog) return

    val shape = RoundedCornerShape(12.dp)

    AlertDialog(
        onDismissRequest = {
            shouldShowDialog = false
            onDismissRequest()
        },
        containerColor = AlertDialogDefaults.containerColor.copy(alpha = .75f),
        confirmButton = {
            AlertDialogButton(
                text = confirmButtonText,
                onClick = {
                    onConfirmClick()
                    shouldShowDialog = false
                })
        },
        dismissButton = {
            AlertDialogButton(
                text = dismissButtonText,
                onClick = {
                    onDismissClick()
                    shouldShowDialog = false
                })
        },
        modifier = modifier
            .clip(shape),
        shape = shape,

        text = {
            Column {
                Title(title)
                Message(message)
            }
        },
        properties = DialogProperties(
            dismissOnBackPress = dismissOnBackPress,
            dismissOnClickOutside = dismissOnClickOutside
        )
    )
}

@Composable
private fun Title(text: String?) {
    text ?: return
    Text(
        text = text,
        style = MaterialTheme.typography.bodyMedium,
        fontWeight = FontWeight.Medium,
        fontSize = 20.sp,
        lineHeight = 24.sp,
        textAlign = TextAlign.Center,
        color = MaterialTheme.colorScheme.onSecondaryContainer,
        modifier = Modifier
            .fillMaxWidth()
            .padding(bottom = 16.dp)
    )
}

@Composable
private fun Message(text: String) {
    Text(
        text = text,
        style = MaterialTheme.typography.bodyMedium,
        fontWeight = FontWeight.Normal,
        fontSize = 16.sp,
        lineHeight = 22.sp,
        letterSpacing = .1.sp,
        color = MaterialTheme.colorScheme.onSecondaryContainer,
        modifier = Modifier
    )

}

@Composable
private fun AlertDialogButton(text: String?, onClick: () -> Unit) {
    text ?: return
    Text(
        text = text.uppercase(),
        fontSize = 14.sp,
        lineHeight = 18.sp,
        letterSpacing = .2.sp,
//        color = ColorCardBorderSuccess,
        modifier = Modifier.clickable { onClick() }
    )
}
