import SwiftUI
import Utilities

/*
 This component displays a heart icon that animates and floats up
 as it fades in and out of the view. This is used when double-tapping to like
 a Hook, or maybe a Clip.

 The UUID is used to uniquely identify each heart animation instance,
 since the user can double-tap multiple times in quick succession.
 */
public struct AnimatedDoubleTapHeart: View {
    let id: UUID
    @State private var scale: CGFloat = 0.0
    @State private var rotation: Double = 0
    @State private var yOffset: CGFloat = 0
    @State private var opacity: Double = 1

    public init(id: UUID) {
        self.id = id
    }

    public var body: some View {
        Image.Icon.heartFilled
            .resizable()
            .frame(width: 64, height: 64)
            .foregroundColor(Color.SemanticV2.accentPink)
            .scaleEffect(scale)
            .rotationEffect(.degrees(rotation))
            .offset(y: yOffset)
            .opacity(opacity)
            .onAppear {
                animateHeart()
            }
    }

    private func animateHeart() {
        withAnimation(.bouncy(duration: 0.3, extraBounce: 0.4)) {
            scale = 1.0
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
            withAnimation(.linear(duration: 0.1)) {
                rotation = -2
            }
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
            withAnimation(.easeInOut(duration: 1.0)) {
                yOffset = -UIScreen.height / 2
            }
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
            withAnimation(.bouncy(duration: 0.8)) {
                scale = 0.85
                rotation = 15
            }
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.9) {
            withAnimation(.easeInOut(duration: 0.3)) {
                opacity = 0
            }
        }
    }
}
