import SwiftUI

// MARK: - Colors & Gradients
struct DesignSystem {
    struct Colors {
        static let deepSpace = Color(hex: "0a0a12")
        static let neonBlue = Color(hex: "00f2ff")
        static let neonPurple = Color(hex: "bd00ff")
        static let neonTeal = Color(hex: "00ff9d")
        
        static func roleGradient(for role: UserRole) -> LinearGradient {
            switch role {
            case .deaf:
                return LinearGradient(
                    gradient: Gradient(colors: [Color(hex: "43cea2"), Color(hex: "185a9d")]),
                    startPoint: .topLeading,
                    endPoint: .bottomTrailing
                )
            case .hearing:
                return LinearGradient(
                    gradient: Gradient(colors: [Color(hex: "FDB99B"), Color(hex: "CF8BF3"), Color(hex: "A770EF")]),
                    startPoint: .topLeading,
                    endPoint: .bottomTrailing
                )
            }
        }
    }
    
    struct Typography {
        static func title(_ text: String) -> Text {
            Text(text)
                .font(.system(.largeTitle, design: .rounded))
                .fontWeight(.heavy)
        }
    }
}

// MARK: - Modifiers

struct LiquidGlassCard: ViewModifier {
    func body(content: Content) -> some View {
        content
            .background(.ultraThinMaterial)
            .cornerRadius(24)
            .shadow(color: Color.black.opacity(0.15), radius: 10, x: 0, y: 5)
            .overlay(
                RoundedRectangle(cornerRadius: 24)
                    .stroke(.white.opacity(0.2), lineWidth: 1)
            )
    }
}

struct GlowingEdge: ViewModifier {
    var color: Color
    func body(content: Content) -> some View {
        content
            .shadow(color: color.opacity(0.6), radius: 20, x: 0, y: 0)
            .shadow(color: color.opacity(0.3), radius: 40, x: 0, y: 0)
    }
}

// MARK: - Views

struct MeshBackground: View {
    @State private var start = UnitPoint(x: 0, y: -2)
    @State private var end = UnitPoint(x: 4, y: 0)
    
    // Dynamic Orb States
    @State private var orb1Offset = CGSize.zero
    @State private var orb2Offset = CGSize.zero
    @State private var orb3Offset = CGSize.zero // Pulsing Blue Orb
    
    @State private var orb1Scale: CGFloat = 1.0
    // Orb 2 Shape Shifting (Independent Scales)
    @State private var orb2ScaleX: CGFloat = 1.0
    @State private var orb2ScaleY: CGFloat = 1.0
    
    @State private var orb3Scale: CGFloat = 1.0 // Pulsing Blue Orb
    @State private var orb3Opacity: Double = 0.3 // Pulsing Opacity
    
    let colors: [Color]
    
    var body: some View {
        GeometryReader { proxy in
            ZStack {
                Color.black.ignoresSafeArea()
                
                // 1. Moving Gradient Background
                LinearGradient(gradient: Gradient(colors: colors), startPoint: start, endPoint: end)
                    .opacity(0.4)
                    .ignoresSafeArea()
                    .blur(radius: 100)
                    .onAppear {
                        withAnimation(.easeInOut(duration: 10).repeatForever(autoreverses: true)) {
                            start = UnitPoint(x: 4, y: 0)
                            end = UnitPoint(x: 0, y: 2)
                        }
                    }
                
                // 2. Floating Orbs for Depth
                
                // Orb 3: Pulsing Blue Orb (Lightens/Darkens)
                Circle()
                    .fill(Color.blue)
                    .frame(width: proxy.size.width * 1.0, height: proxy.size.width * 1.0)
                    .blur(radius: 150) // Highly blurred
                    .opacity(orb3Opacity) // Pulsing Opacity
                    .scaleEffect(orb3Scale)
                    .offset(orb3Offset)
                    .position(x: proxy.size.width / 2, y: proxy.size.height / 2)
                
                // Orb 1
                Circle()
                    .fill(colors.first ?? .blue)
                    .frame(width: proxy.size.width * 0.9, height: proxy.size.width * 0.9)
                    .blur(radius: 120)
                    .scaleEffect(orb1Scale)
                    .offset(orb1Offset)
                    .position(x: proxy.size.width / 2, y: proxy.size.height / 2) // Center start
                
                // Orb 2 (Black Orb) - Shape Shifting
                Circle()
                    .fill(colors.last ?? .purple)
                    .frame(width: proxy.size.width * 0.8, height: proxy.size.width * 0.8)
                    .blur(radius: 120)
                    .scaleEffect(x: orb2ScaleX, y: orb2ScaleY) // Independent scaling creates ovals
                    .offset(orb2Offset)
                    .position(x: proxy.size.width / 2, y: proxy.size.height / 2) // Center start
            }
            .onAppear {
                animateOrb1(in: proxy.size)
                animateOrb2(in: proxy.size)
                animateOrb3(in: proxy.size)
            }
        }
        .ignoresSafeArea()
    }
    
    private func animateOrb1(in size: CGSize) {
        // Random wandering loop
        let duration = Double.random(in: 6...9)
        withAnimation(.easeInOut(duration: duration)) {
            orb1Offset = CGSize(
                width: CGFloat.random(in: -size.width/2...size.width/2),
                height: CGFloat.random(in: -size.height/2...size.height/2)
            )
            orb1Scale = CGFloat.random(in: 0.9...1.2)
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
            animateOrb1(in: size)
        }
    }
    
    private func animateOrb2(in size: CGSize) {
        // Random wandering loop with Shape Shifting
        let duration = Double.random(in: 7...10)
        withAnimation(.easeInOut(duration: duration)) {
            orb2Offset = CGSize(
                width: CGFloat.random(in: -size.width/2...size.width/2),
                height: CGFloat.random(in: -size.height/2...size.height/2)
            )
            // Independent X and Y scales morph the shape between circle and oval
            orb2ScaleX = CGFloat.random(in: 0.4...1.1)
            orb2ScaleY = CGFloat.random(in: 0.4...1.1)
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
            animateOrb2(in: size)
        }
    }
    
    private func animateOrb3(in size: CGSize) {
        // Pulsing Blue Orb Animation
        let duration = Double.random(in: 10...15)
        withAnimation(.easeInOut(duration: duration)) {
            orb3Offset = CGSize(
                width: CGFloat.random(in: -size.width/3...size.width/3),
                height: CGFloat.random(in: -size.height/3...size.height/3)
            )
            orb3Scale = CGFloat.random(in: 0.9...1.1)
            orb3Opacity = Double.random(in: 0.1...0.6) // Lightens and darkens
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
            animateOrb3(in: size)
        }
    }
}

extension Color {
    var complementary: Color {
        let uiColor = UIColor(self)
        var h: CGFloat = 0
        var s: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0
        
        uiColor.getHue(&h, saturation: &s, brightness: &b, alpha: &a)
        
        let newHue = h + 0.5
        let finalHue = newHue > 1.0 ? newHue - 1.0 : newHue
        
        return Color(hue: finalHue, saturation: s, brightness: b, opacity: a)
    }
}

extension View {
    func liquidCard() -> some View {
        self.modifier(LiquidGlassCard())
    }
    
    func glowingEdge(color: Color) -> some View {
        self.modifier(GlowingEdge(color: color))
    }
}
