import SwiftUI

// MARK: - Staggered Ripple Animation

/// Telegram-style staggered ripple rings that emanate from the avatar.
/// 4 rings, each delayed by 0.6s, creating a sound-wave effect.
struct CallPulsingRings: View {
    let size: CGFloat
    var color: Color = .white

    private let ringCount = 4
    private let ringDuration: Double = 2.4
    private let ringDelay: Double = 0.6
    private let maxScale: CGFloat = 1.8

    var body: some View {
        ZStack {
            ForEach(0..<ringCount, id: \.self) { index in
                RippleRing(
                    color: color,
                    duration: ringDuration,
                    delay: Double(index) * ringDelay,
                    maxScale: maxScale
                )
            }
        }
        .frame(width: size * maxScale + 2, height: size * maxScale + 2)
    }
}

// MARK: - Individual Ring

private struct RippleRing: View {
    let color: Color
    let duration: Double
    let delay: Double
    let maxScale: CGFloat

    @State private var isAnimating = false

    var body: some View {
        Circle()
            .stroke(color.opacity(0.12), lineWidth: 1.5)
            .background(Circle().fill(color.opacity(0.04)))
            .scaleEffect(isAnimating ? maxScale : 1.0)
            .opacity(isAnimating ? 0.0 : 0.6)
            .task {
                try? await Task.sleep(for: .seconds(delay))
                withAnimation(
                    .easeOut(duration: duration)
                    .repeatForever(autoreverses: false)
                ) {
                    isAnimating = true
                }
            }
    }
}
