//
//  CozyAnimations.swift
//  lichunWebsocket
//
//  Animation library for cozy interactions
//

import SwiftUI

struct CozyAnimations {
    // Extra bouncy for primary interactions (delegates to AppAnimations to avoid drift)
    static let bounce = AppAnimations.bounce

    // Gentle float for idle states
    static let float = Animation.easeInOut(duration: 2.0).repeatForever(autoreverses: true)

    // Quick pop for feedback
    static let pop = Animation.spring(response: 0.2, dampingFraction: 0.55)

    // Smooth slide for transitions
    static let slide = Animation.spring(response: 0.35, dampingFraction: 0.75)

    // Soft fade (delegates to AppAnimations to avoid drift)
    static let fade = AppAnimations.easeOut
}

// View extension for transition effects
extension View {
    /// Float in transition - scale + opacity
    func floatInTransition(delay: Double = 0) -> some View {
        self.transition(
            .asymmetric(
                insertion: .scale(scale: 0.92, anchor: .center).combined(with: .opacity)
                    .animation(
                        .easeOut(duration: 0.15).delay(delay)
                    ),
                removal: .scale(scale: 0.95, anchor: .center).combined(with: .opacity)
                    .animation(.easeIn(duration: 0.1))
            )
        )
    }

    /// Number pop animation when value changes
    func popOnChange<V: Equatable>(of value: V) -> some View {
        self.modifier(PopOnChangeModifier(value: value))
    }
}

struct PopOnChangeModifier<V: Equatable>: ViewModifier {
    let value: V
    @State private var scale: CGFloat = 1.0

    func body(content: Content) -> some View {
        content
            .scaleEffect(scale)
            .onChange(of: value) { _ in
                withAnimation(.spring(response: 0.3, dampingFraction: 0.4)) {
                    scale = 1.2
                }
                withAnimation(.spring(response: 0.4, dampingFraction: 0.6).delay(0.1)) {
                    scale = 1.0
                }
            }
    }
}
