import SwiftUI

struct DragUpFooter: View {
    let onDragUp: () -> Void
    let text: String
    let shouldShimmer: Bool
    let arrowScale: Double // Scale factor for the arrow (1.0 to larger scale)
    
    @State private var shimmerOffset: Double = -1.0
    @State private var animationId: UUID = UUID()
    
    var body: some View {
        VStack(spacing: 12) {
            // Up arrow button
            dragButton
            
            // Description text
            descriptionText
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 12)
    }
    
    private var dragButton: some View {
        Button {
            onDragUp()
        } label: {
            ZStack {
                Circle()
                    .fill(Constants.Colors.Background.Fog.thin)
                    .frame(width: 32, height: 32)
                
                Image(systemName: "arrow.up")
                    .font(.system(size: 12, weight: .medium))
                    .foregroundColor(Constants.Colors.Foreground.primary)
            }
            .scaleEffect(arrowScale)
        }
        .buttonStyle(PlainButtonStyle())
    }
    
    private var descriptionText: some View {
        Group {
            if shouldShimmer {
                shimmerText
            } else {
                Text(text)
                    .font(Constants.Typography.small)
                    .foregroundColor(.white.opacity(0.3))
                    .tracking(0.28)
                    .multilineTextAlignment(.center)
            }
        }
    }
    
    private var shimmerText: some View {
        ZStack {
            // Base text at 30% opacity
            Text(text)
                .font(Constants.Typography.small)
                .tracking(0.28)
                .foregroundColor(.white.opacity(0.3))
                .multilineTextAlignment(.center)
            
            // Shimmer highlight at full opacity
            Text(text)
                .font(Constants.Typography.small)
                .tracking(0.28)
                .foregroundColor(.white)
                .multilineTextAlignment(.center)
                .mask(
                    Rectangle()
                        .fill(
                            LinearGradient(
                                colors: [
                                    Color.clear,
                                    Color.white,
                                    Color.clear
                                ],
                                startPoint: UnitPoint(x: shimmerOffset - 0.5, y: 0.5),
                                endPoint: UnitPoint(x: shimmerOffset + 0.5, y: 0.5)
                            )
                        )
                )
        }
        .id(animationId)
        .onAppear {
            if shouldShimmer {
                startShimmerAnimation()
            }
        }
        .onChange(of: shouldShimmer) { _, isShimmering in
            if isShimmering {
                startShimmerAnimation()
            } else {
                stopShimmerAnimation()
            }
        }
    }
    
    private func startShimmerAnimation() {
        // Generate new ID to force SwiftUI to recreate the view and restart animation
        animationId = UUID()
        shimmerOffset = -1.0
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
            withAnimation(.linear(duration: 1).repeatForever(autoreverses: false)) {
                shimmerOffset = 1.8
            }
        }
    }
    
    private func stopShimmerAnimation() {
        shimmerOffset = -1.0
    }
}

#Preview {
    VStack {
        Spacer()
        
        DragUpFooter(
            onDragUp: {
                print("Drag up tapped")
            },
            text: "Release to make a song!",
            shouldShimmer: true,
            arrowScale: 1.5 // Example scale
        )
    }
    .background(Constants.Colors.Background.primary)
    .frame(maxWidth: .infinity, maxHeight: .infinity)
}
