package com.seven.ikar.kotlin.feature.forward

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
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.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.seven.ikar.kotlin.core.contacts.DiscoverUserItem
import com.seven.ikar.kotlin.ui.AvatarView
import com.seven.ikar.kotlin.ui.IkarScreenSurface
import com.seven.ikar.kotlin.ui.IkarSearchCard
import com.seven.ikar.kotlin.ui.IkarSectionCard
import com.seven.ikar.kotlin.ui.IkarSimpleHeader
import com.seven.ikar.kotlin.ui.IkarRowDivider
import com.seven.ikar.kotlin.ui.formatDiscoverPresence
import com.seven.ikar.kotlin.ui.formatPresenceColor
import com.seven.ikar.kotlin.ui.theme.TelegramBlue
import com.seven.ikar.kotlin.ui.theme.TelegramInkMuted

@Composable
fun ForwardScreen(
    state: ForwardUiState,
    onBackClick: () -> Unit,
    onQueryChanged: (String) -> Unit,
    onUserClick: (DiscoverUserItem) -> Unit
) {
    IkarScreenSurface(modifier = Modifier.fillMaxSize()) {
        Column(modifier = Modifier.fillMaxSize()) {
            Column(
                modifier = Modifier.padding(start = 18.dp, top = 16.dp, end = 18.dp, bottom = 12.dp),
                verticalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                IkarSimpleHeader(title = "Переслать", onBackClick = onBackClick)

                IkarSearchCard(
                    value = state.query,
                    onValueChange = onQueryChanged,
                    placeholder = "Поиск контакта"
                )

                state.errorMessage?.let {
                    Text(it, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodyMedium)
                }
            }

            IkarSectionCard(
                modifier = Modifier
                    .weight(1f)
                    .padding(horizontal = 18.dp, vertical = 0.dp)
                    .padding(bottom = 24.dp),
                padding = androidx.compose.foundation.layout.PaddingValues(0.dp)
            ) {
                Text(
                    "Выберите получателя",
                    modifier = Modifier.padding(start = 18.dp, top = 16.dp, end = 18.dp, bottom = 8.dp),
                    style = MaterialTheme.typography.labelLarge,
                    color = TelegramBlue
                )

                LazyColumn {
                    if (state.items.isEmpty()) {
                        item {
                            Column(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(horizontal = 24.dp, vertical = 28.dp),
                                horizontalAlignment = Alignment.CenterHorizontally,
                                verticalArrangement = Arrangement.spacedBy(8.dp)
                            ) {
                                Text("Контакты не найдены", style = MaterialTheme.typography.headlineMedium)
                                Text(
                                    "Откройте доступ к контактам или уточните поиск.",
                                    color = TelegramInkMuted,
                                    style = MaterialTheme.typography.bodyMedium
                                )
                            }
                        }
                    } else {
                        items(state.items, key = { it.id }) { item ->
                            Column {
                                Row(
                                    modifier = Modifier
                                        .fillMaxWidth()
                                        .clickable { onUserClick(item) }
                                        .padding(horizontal = 18.dp, vertical = 12.dp),
                                    horizontalArrangement = Arrangement.spacedBy(14.dp),
                                    verticalAlignment = Alignment.CenterVertically
                                ) {
                                    AvatarView(imageUrl = item.avatarUrl, title = item.displayName, size = 54.dp)
                                    Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(3.dp)) {
                                        Text(item.displayName, style = MaterialTheme.typography.titleLarge, maxLines = 1, overflow = TextOverflow.Ellipsis)
                                        Text(
                                            formatDiscoverPresence(item),
                                            style = MaterialTheme.typography.bodySmall,
                                            color = formatPresenceColor(item)
                                        )
                                    }
                                }
                                IkarRowDivider(startIndent = 86.dp)
                            }
                        }
                    }
                }
            }
        }
    }
}
