import SwiftUI

struct WaveGradientBackground: View {
    let intensity: Double // 0.0 to 1.0
    
    @State private var animationOffset: Double = 0
    
    // Dynamic colors based on intensity that fade to background
    private func meshColors() -> [Color] {
        let backgroundColor = Constants.Colors.Background.primary
        let baseOpacity = intensity * 0.8
        
        // Define main gradient colors based on intensity
        let accentColor: Color
        let secondaryColor: Color
        let tertiaryColor: Color
        
        if intensity < 0.3 {
            accentColor = Constants.Colors.Accent.brand
            secondaryColor = Constants.Colors.Accent.brand
            tertiaryColor = Constants.Colors.Accent.purple
        } else if intensity < 0.7 {
            accentColor = Color.purple
            secondaryColor = Color.pink
            tertiaryColor = Color.indigo
        } else {
            accentColor = Constants.Colors.Accent.brand
            secondaryColor = Constants.Colors.Accent.orange
            tertiaryColor = Constants.Colors.Accent.purple
        }
        
        // Animate color positions - shift colors through the mesh points
        let colorOffset = animationOffset
        let color1 = accentColor.opacity(baseOpacity)
        let color2 = secondaryColor.opacity(baseOpacity)
        let color3 = tertiaryColor.opacity(baseOpacity)
        
        // Create shifting color pattern that moves through the mesh
        let shiftedColors = [color1, color2, color3, color1, color2, color3]
        let startIndex = Int(colorOffset * 3) % 3
        
        return [
            // Top row - all background color (fade out)
            backgroundColor, backgroundColor, backgroundColor, backgroundColor,
            // Second row - mostly background with slight shifting accent
            backgroundColor, 
            shiftedColors[(startIndex + 0) % 3].opacity(baseOpacity * 0.1), 
            shiftedColors[(startIndex + 1) % 3].opacity(baseOpacity * 0.1), 
            backgroundColor,
            // Third row - more intense shifting accent colors
            shiftedColors[(startIndex + 1) % 3].opacity(baseOpacity * 0.3), 
            shiftedColors[(startIndex + 2) % 3].opacity(baseOpacity * 0.6), 
            shiftedColors[(startIndex + 0) % 3].opacity(baseOpacity * 0.5), 
            shiftedColors[(startIndex + 1) % 3].opacity(baseOpacity * 0.3),
            // Bottom row - full intensity shifting arc
            shiftedColors[(startIndex + 2) % 3].opacity(baseOpacity), 
            shiftedColors[(startIndex + 0) % 3].opacity(baseOpacity * 0.9), 
            shiftedColors[(startIndex + 1) % 3].opacity(baseOpacity * 0.8), 
            shiftedColors[(startIndex + 2) % 3].opacity(baseOpacity)
        ]
    }
    
    // Static mesh points for clean arc shape
    private func meshPoints() -> [SIMD2<Float>] {
        return [
            // Top row - static
            [0.0, 0.0], [0.33, 0.0], [0.67, 0.0], [1.0, 0.0],
            // Second row - gentle curve
            [0.0, 0.4], [0.33, 0.45], [0.67, 0.4], [1.0, 0.45],
            // Third row - wider arc formation
            [0.0, 0.75], [0.33, 0.8], [0.67, 0.78], [1.0, 0.8],
            // Bottom row - flat wide arc
            [0.0, 1.0], [0.33, 1.0], [0.67, 1.0], [1.0, 1.0]
        ]
    }
    
    var body: some View {
        MeshGradient(
            width: 4,
            height: 4,
            points: meshPoints(),
            colors: meshColors()
        )
        .blur(radius: intensity * 4 + 1)
        .onAppear {
            startAnimation()
        }
    }
    
    private func startAnimation() {
        withAnimation(.linear(duration: 8).repeatForever(autoreverses: true)) {
            animationOffset = 1.0
        }
    }
}



#Preview {
    ZStack {
        Constants.Colors.Background.primary
            .ignoresSafeArea()
        
        WaveGradientBackground(intensity: 0.8)
    }
}
