package ru.forbion.f7cloud.feature.mail

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
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.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.HorizontalDivider
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.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
import coil.compose.AsyncImage
import ru.forbion.f7cloud.core.designsystem.F7Colors
import ru.forbion.f7cloud.feature.contacts.ContactItem
import ru.forbion.f7cloud.feature.contacts.ContactListRow

@Composable
fun MailRecipientComposeField(
    label: String,
    value: String,
    onValueChange: (String) -> Unit,
    suggestions: List<ContactItem>,
    showSuggestions: Boolean,
    onSuggestionClick: (ContactItem) -> Unit,
    onDismissSuggestions: () -> Unit,
    trailingIconUrl: String? = null,
    onTrailingClick: (() -> Unit)? = null,
) {
    Box(modifier = Modifier.fillMaxWidth()) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 12.dp, vertical = 4.dp)
                .clip(RoundedCornerShape(8.dp))
                .border(1.dp, F7Colors.Border, RoundedCornerShape(8.dp))
                .background(Color.White)
                .padding(horizontal = 12.dp, vertical = 10.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            BasicTextField(
                value = value,
                onValueChange = onValueChange,
                modifier = Modifier.weight(1f),
                textStyle = TextStyle(fontSize = 15.sp, color = F7Colors.TextPrimary),
                cursorBrush = SolidColor(F7Colors.Primary),
                decorationBox = { inner ->
                    if (value.isEmpty()) {
                        Text(label, color = F7Colors.TextSecondary, fontSize = 15.sp)
                    }
                    inner()
                },
            )
            if (trailingIconUrl != null && onTrailingClick != null) {
                AsyncImage(
                    model = trailingIconUrl,
                    contentDescription = null,
                    modifier = Modifier
                        .size(16.dp)
                        .clickable(onClick = onTrailingClick),
                    contentScale = ContentScale.Fit,
                )
            }
        }
        if (showSuggestions && suggestions.isNotEmpty()) {
            Popup(
                alignment = Alignment.TopStart,
                onDismissRequest = onDismissSuggestions,
                properties = PopupProperties(focusable = false),
            ) {
                Column(
                    modifier = Modifier
                        .padding(horizontal = 12.dp, vertical = 2.dp)
                        .fillMaxWidth()
                        .heightIn(max = 240.dp)
                        .clip(RoundedCornerShape(8.dp))
                        .background(Color.White)
                        .border(1.dp, F7Colors.Border, RoundedCornerShape(8.dp)),
                ) {
                    suggestions.forEachIndexed { index, contact ->
                        ContactListRow(
                            contact = contact,
                            onClick = { onSuggestionClick(contact) },
                        )
                        if (index < suggestions.lastIndex) {
                            HorizontalDivider(color = F7Colors.Border.copy(alpha = 0.6f))
                        }
                    }
                }
            }
        }
    }
}
