package com.rosetta.messenger.ui.chats.utils

import androidx.compose.animation.core.CubicBezierEasing
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.StrokeJoin
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.path
import androidx.compose.ui.unit.dp
import com.rosetta.messenger.ui.chats.models.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * Utility functions and constants for Chat Detail Screen
 * Extracted from ChatDetailScreen.kt for better organization
 */

// Telegram's CubicBezier interpolator (0.199, 0.010, 0.279, 0.910)
val TelegramEasing = CubicBezierEasing(0.199f, 0.010f, 0.279f, 0.910f)

// Message delivery timeout constant (80 seconds, matching archive)
private const val MESSAGE_MAX_TIME_TO_DELIVERED_MS = 80_000L

/** Check if message can still be delivered (timeout not exceeded) */
fun isMessageDeliveredByTime(timestamp: Long, attachmentsCount: Int = 0): Boolean {
    val maxTime = if (attachmentsCount > 0) {
        MESSAGE_MAX_TIME_TO_DELIVERED_MS * attachmentsCount
    } else {
        MESSAGE_MAX_TIME_TO_DELIVERED_MS
    }
    return System.currentTimeMillis() - timestamp < maxTime
}

/** Get date text (today, yesterday or full date) */
fun getDateText(timestamp: Long): String {
    val messageDate = Calendar.getInstance().apply { timeInMillis = timestamp }
    val today = Calendar.getInstance()
    val yesterday = Calendar.getInstance().apply { add(Calendar.DAY_OF_YEAR, -1) }

    return when {
        messageDate.get(Calendar.YEAR) == today.get(Calendar.YEAR) &&
                messageDate.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR) -> "today"
        messageDate.get(Calendar.YEAR) == yesterday.get(Calendar.YEAR) &&
                messageDate.get(Calendar.DAY_OF_YEAR) == yesterday.get(Calendar.DAY_OF_YEAR) -> "yesterday"
        else -> SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).format(Date(timestamp))
    }
}

/** Get user initials from name */
fun getInitials(name: String): String {
    return name.trim()
        .split(Regex("\\s+"))
        .filter { it.isNotEmpty() }
        .let { words ->
            when {
                words.isEmpty() -> "??"
                words.size == 1 -> words[0].take(2).uppercase()
                else -> "${words[0].first()}${words[1].first()}".uppercase()
            }
        }
}

/** Get consistent avatar colors based on name/key */
fun getAvatarColor(name: String, isDarkTheme: Boolean): AvatarColors {
    val colors = listOf(
        Pair(Color(0xFFFF6B6B), Color.White),
        Pair(Color(0xFF4ECDC4), Color.White),
        Pair(Color(0xFF45B7D1), Color.White),
        Pair(Color(0xFFF7B731), Color.White),
        Pair(Color(0xFF5F27CD), Color.White),
        Pair(Color(0xFF00D2D3), Color.White),
        Pair(Color(0xFFFF9FF3), Color.White),
        Pair(Color(0xFF54A0FF), Color.White)
    )
    val index = name.hashCode().let { if (it < 0) -it else it } % colors.size
    val (bg, text) = colors[index]
    return AvatarColors(backgroundColor = bg, textColor = text)
}

/** Telegram Send Icon (horizontal plane) - custom SVG icon */
val TelegramSendIcon: ImageVector
    get() = ImageVector.Builder(
        name = "TelegramSendHorizontal",
        defaultWidth = 24.dp,
        defaultHeight = 24.dp,
        viewportWidth = 24f,
        viewportHeight = 24f
    ).apply {
        path(
            fill = null,
            stroke = SolidColor(Color.White),
            strokeLineWidth = 2f,
            strokeLineCap = StrokeCap.Round,
            strokeLineJoin = StrokeJoin.Round
        ) {
            moveTo(3.714f, 3.048f)
            arcToRelative(0.498f, 0.498f, 0f, false, false, -0.683f, 0.627f)
            lineToRelative(2.843f, 7.627f)
            arcToRelative(2f, 2f, 0f, false, true, 0f, 1.396f)
            lineToRelative(-2.842f, 7.627f)
            arcToRelative(0.498f, 0.498f, 0f, false, false, 0.682f, 0.627f)
            lineToRelative(18f, -8.5f)
            arcToRelative(0.5f, 0.5f, 0f, false, false, 0f, -0.904f)
            close()
        }
        path(
            fill = null,
            stroke = SolidColor(Color.White),
            strokeLineWidth = 2f,
            strokeLineCap = StrokeCap.Round,
            strokeLineJoin = StrokeJoin.Round
        ) {
            moveTo(6f, 12f)
            horizontalLineToRelative(16f)
        }
    }.build()

/** Thin microphone icon matching the visual weight of Telegram-style input icons */
val ThinMicrophoneIcon: ImageVector
    get() = ImageVector.Builder(
        name = "ThinMicrophone",
        defaultWidth = 24.dp,
        defaultHeight = 24.dp,
        viewportWidth = 24f,
        viewportHeight = 24f
    ).apply {
        // Microphone capsule
        path(
            fill = null,
            stroke = SolidColor(Color.White),
            strokeLineWidth = 1.5f,
            strokeLineCap = StrokeCap.Round,
            strokeLineJoin = StrokeJoin.Round
        ) {
            moveTo(9f, 5f)
            arcToRelative(3f, 3f, 0f, isMoreThanHalf = false, isPositiveArc = true, dx1 = 6f, dy1 = 0f)
            verticalLineToRelative(5f)
            arcToRelative(3f, 3f, 0f, isMoreThanHalf = false, isPositiveArc = true, dx1 = -6f, dy1 = 0f)
            close()
        }
        // U-shaped curve below capsule
        path(
            fill = null,
            stroke = SolidColor(Color.White),
            strokeLineWidth = 1.5f,
            strokeLineCap = StrokeCap.Round,
            strokeLineJoin = StrokeJoin.Round
        ) {
            moveTo(5f, 10f)
            arcToRelative(7f, 7f, 0f, isMoreThanHalf = false, isPositiveArc = false, dx1 = 14f, dy1 = 0f)
        }
        // Vertical stem
        path(
            fill = null,
            stroke = SolidColor(Color.White),
            strokeLineWidth = 1.5f,
            strokeLineCap = StrokeCap.Round,
            strokeLineJoin = StrokeJoin.Round
        ) {
            moveTo(12f, 17f)
            verticalLineToRelative(4f)
        }
        // Horizontal base
        path(
            fill = null,
            stroke = SolidColor(Color.White),
            strokeLineWidth = 1.5f,
            strokeLineCap = StrokeCap.Round,
            strokeLineJoin = StrokeJoin.Round
        ) {
            moveTo(8f, 21f)
            horizontalLineToRelative(8f)
        }
    }.build()
