package com.rosetta.messenger.ui.chats

import androidx.compose.foundation.background
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.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.rosetta.messenger.R

@Composable
fun RequestsSection(
    count: Int,
    requests: List<DialogUiModel> = emptyList(),
    isDarkTheme: Boolean,
    onClick: () -> Unit
) {
    val titleColor = remember(isDarkTheme) {
        if (isDarkTheme) Color(0xFFAAAAAA) else Color(0xFF525252)
    }
    val subtitleColor =
        remember(isDarkTheme) { if (isDarkTheme) Color(0xFF6E6E6E) else Color(0xFF919191) }
    val iconBgColor =
        remember(isDarkTheme) { if (isDarkTheme) Color(0xFF3A3A3C) else Color(0xFFB8C2CC) }
    val badgeColor =
        remember(isDarkTheme) { if (isDarkTheme) Color(0xFF4E4E4E) else Color(0xFFC6C9CC) }

    val lastRequest = remember(requests) { requests.firstOrNull() }
    val subtitle = remember(lastRequest) {
        when {
            lastRequest == null -> ""
            lastRequest.opponentTitle.isNotEmpty() &&
                lastRequest.opponentTitle != lastRequest.opponentKey &&
                lastRequest.opponentTitle != lastRequest.opponentKey.take(7) -> lastRequest.opponentTitle
            lastRequest.opponentUsername.isNotEmpty() -> "@${lastRequest.opponentUsername}"
            else -> lastRequest.opponentKey.take(7)
        }
    }

    Column {
        Row(
            modifier =
                Modifier.fillMaxWidth()
                    .clickable(onClick = onClick)
                    .padding(
                        horizontal = TELEGRAM_DIALOG_AVATAR_START,
                        vertical = TELEGRAM_DIALOG_VERTICAL_PADDING
                    ),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Box(
                modifier =
                    Modifier.size(TELEGRAM_DIALOG_AVATAR_SIZE)
                        .background(iconBgColor, CircleShape),
                contentAlignment = Alignment.Center
            ) {
                Icon(
                    painter = painterResource(id = R.drawable.archive_filled),
                    contentDescription = null,
                    tint = Color.White,
                    modifier = Modifier.size(26.dp)
                )
            }

            Spacer(modifier = Modifier.width(TELEGRAM_DIALOG_AVATAR_GAP))

            Column(modifier = Modifier.weight(1f)) {
                Text(
                    text = "Requests",
                    fontWeight = FontWeight.SemiBold,
                    fontSize = 16.sp,
                    color = titleColor,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )

                if (subtitle.isNotEmpty()) {
                    Spacer(modifier = Modifier.height(4.dp))
                    Text(
                        text = subtitle,
                        fontSize = 14.sp,
                        color = subtitleColor,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis
                    )
                }
            }

            if (count > 0) {
                Spacer(modifier = Modifier.width(8.dp))
                Box(
                    modifier =
                        Modifier.defaultMinSize(minWidth = 22.dp, minHeight = 22.dp)
                            .background(badgeColor, CircleShape),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = if (count > 99) "99+" else count.toString(),
                        fontSize = 12.sp,
                        fontWeight = FontWeight.Bold,
                        color = Color.White,
                        lineHeight = 12.sp,
                        modifier = Modifier.padding(horizontal = 5.dp, vertical = 3.dp)
                    )
                }
            }
        }
    }
}
