package ru.forbion.f7cloud.core.designsystem

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import okhttp3.Credentials

private fun TextStyle.doubled(): TextStyle = copy(
    fontSize = fontSize * 2,
    lineHeight = lineHeight * 2,
)

@Composable
fun F7NotificationRow(
    subject: String,
    message: String,
    relativeTime: String,
    iconUrl: String?,
    closeIconUrl: String,
    authHeader: String?,
    onClick: () -> Unit,
    onDismiss: () -> Unit,
    showDivider: Boolean,
    modifier: Modifier = Modifier,
) {
    val context = LocalContext.current
    Column(
        modifier = modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(horizontal = 16.dp, vertical = 16.dp),
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.End,
            verticalAlignment = Alignment.CenterVertically,
        ) {
            if (relativeTime.isNotBlank()) {
                Text(
                    text = relativeTime,
                    style = MaterialTheme.typography.labelSmall.doubled(),
                    fontWeight = FontWeight.Medium,
                    color = F7Colors.TextSecondary.copy(alpha = 0.55f),
                )
            }
            Box(
                modifier = Modifier
                    .padding(start = 8.dp)
                    .size(44.dp)
                    .clip(CircleShape)
                    .clickable(onClick = onDismiss),
                contentAlignment = Alignment.Center,
            ) {
                AsyncImage(
                    model = closeIconUrl,
                    contentDescription = "Закрыть",
                    modifier = Modifier.size(28.dp),
                )
            }
        }
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 4.dp),
            horizontalArrangement = Arrangement.spacedBy(14.dp),
            verticalAlignment = Alignment.Top,
        ) {
            Box(
                modifier = Modifier
                    .size(64.dp)
                    .clip(CircleShape)
                    .background(F7Colors.SurfaceMuted),
                contentAlignment = Alignment.Center,
            ) {
                val model = if (!iconUrl.isNullOrBlank()) {
                    ImageRequest.Builder(context)
                        .data(iconUrl)
                        .apply {
                            authHeader?.let { addHeader("Authorization", it) }
                        }
                        .build()
                } else {
                    null
                }
                if (model != null) {
                    AsyncImage(
                        model = model,
                        contentDescription = null,
                        modifier = Modifier.size(36.dp),
                        contentScale = ContentScale.Fit,
                    )
                }
            }
            Text(
                text = subject,
                style = MaterialTheme.typography.bodyMedium.doubled(),
                fontWeight = FontWeight.SemiBold,
                color = F7Colors.TextPrimary,
                modifier = Modifier.weight(1f),
            )
        }
        if (message.isNotBlank()) {
            Text(
                text = message,
                style = MaterialTheme.typography.bodySmall.doubled(),
                fontWeight = FontWeight.Medium,
                color = F7Colors.TextSecondary.copy(alpha = 0.7f),
                maxLines = 6,
                overflow = TextOverflow.Ellipsis,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(start = 78.dp, top = 6.dp),
            )
        }
    }
    if (showDivider) {
        HorizontalDivider(color = F7Colors.Border, thickness = 1.dp)
    }
}
