package com.rosetta.messenger.ui.components

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.*
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.unit.dp

@Composable
fun AnimatedDownloadIndicator(
    isActive: Boolean,
    color: Color = Color.White,
    modifier: Modifier = Modifier
) {
    AnimatedVisibility(
        visible = isActive,
        enter = fadeIn(animationSpec = tween(200)) + scaleIn(
            initialScale = 0.6f,
            animationSpec = spring(
                dampingRatio = Spring.DampingRatioMediumBouncy,
                stiffness = Spring.StiffnessMedium
            )
        ),
        exit = fadeOut(animationSpec = tween(200)) + scaleOut(
            targetScale = 0.6f,
            animationSpec = tween(150)
        ),
        modifier = modifier
    ) {
        // Infinite rotation for the circular progress arc
        val infiniteTransition = rememberInfiniteTransition(label = "download_rotation")
        val rotation by infiniteTransition.animateFloat(
            initialValue = 0f,
            targetValue = 360f,
            animationSpec = infiniteRepeatable(
                animation = tween(durationMillis = 1200, easing = LinearEasing),
                repeatMode = RepeatMode.Restart
            ),
            label = "download_rotation_angle"
        )

        // Pulsing arrow bounce
        val arrowBounce by infiniteTransition.animateFloat(
            initialValue = 0f,
            targetValue = 1f,
            animationSpec = infiniteRepeatable(
                animation = tween(durationMillis = 800, easing = FastOutSlowInEasing),
                repeatMode = RepeatMode.Reverse
            ),
            label = "arrow_bounce"
        )

        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier.size(24.dp)
        ) {
            Canvas(modifier = Modifier.size(24.dp)) {
                val centerX = size.width / 2
                val centerY = size.height / 2
                val radius = size.width / 2 - 2.dp.toPx()
                val strokeWidth = 2.dp.toPx()

                // 1) Rotating arc (circular progress indicator)
                rotate(degrees = rotation, pivot = Offset(centerX, centerY)) {
                    drawArc(
                        color = color,
                        startAngle = 0f,
                        sweepAngle = 120f,
                        useCenter = false,
                        style = Stroke(width = strokeWidth, cap = StrokeCap.Round),
                        topLeft = Offset(centerX - radius, centerY - radius),
                        size = androidx.compose.ui.geometry.Size(radius * 2, radius * 2)
                    )
                }

                // 2) Arrow pointing down (download symbol)
                val arrowOffset = arrowBounce * 1.5.dp.toPx()
                val arrowStroke = 2.dp.toPx()
                val arrowTop = centerY - 4.dp.toPx() + arrowOffset
                val arrowBottom = centerY + 4.dp.toPx() + arrowOffset
                val arrowWing = 3.dp.toPx()

                // Vertical line of arrow
                drawLine(
                    color = color,
                    start = Offset(centerX, arrowTop),
                    end = Offset(centerX, arrowBottom),
                    strokeWidth = arrowStroke,
                    cap = StrokeCap.Round
                )

                // Left wing of arrowhead
                drawLine(
                    color = color,
                    start = Offset(centerX - arrowWing, arrowBottom - arrowWing),
                    end = Offset(centerX, arrowBottom),
                    strokeWidth = arrowStroke,
                    cap = StrokeCap.Round
                )

                // Right wing of arrowhead
                drawLine(
                    color = color,
                    start = Offset(centerX + arrowWing, arrowBottom - arrowWing),
                    end = Offset(centerX, arrowBottom),
                    strokeWidth = arrowStroke,
                    cap = StrokeCap.Round
                )
            }
        }
    }
}
