//
//  PulsingModifier.swift
//  CasinoKit
//
//  Visual hint modifier that pulses to draw attention.
//

import SwiftUI

/// Modifier that adds a pulsing animation to draw attention to interactive elements.
public struct PulsingModifier: ViewModifier {
    let isActive: Bool
    let color: Color
    let scale: CGFloat
    
    @State private var animationAmount: CGFloat = 1.0
    
    public func body(content: Content) -> some View {
        content
            .overlay {
                if isActive {
                    RoundedRectangle(cornerRadius: CasinoDesign.CornerRadius.large)
                        .stroke(color, lineWidth: CasinoDesign.LineWidth.medium)
                        .scaleEffect(animationAmount)
                        .opacity(2 - animationAmount)
                        .animation(
                            .easeInOut(duration: 1.5)
                                .repeatForever(autoreverses: false),
                            value: animationAmount
                        )
                        .onAppear {
                            animationAmount = scale
                        }
                }
            }
    }
}

public extension View {
    /// Adds a pulsing animation to draw attention to the view.
    func pulsing(
        isActive: Bool,
        color: Color = .white,
        scale: CGFloat = 1.3
    ) -> some View {
        modifier(PulsingModifier(isActive: isActive, color: color, scale: scale))
    }
}

#Preview {
    ZStack {
        Color.black
        
        VStack(spacing: CasinoDesign.Spacing.xLarge) {
            Button("Tap Here") {
                // Action
            }
            .padding()
            .background(Color.Sheet.accent)
            .foregroundStyle(.black)
            .clipShape(.rect(cornerRadius: CasinoDesign.CornerRadius.medium))
            .pulsing(isActive: true, color: .white)
            
            Text("Pulsing highlights interactive areas")
                .foregroundStyle(.white)
                .font(.caption)
        }
    }
}

