import SwiftUI

struct RouteVisualization: View {
    let departureCode: String
    let arrivalCode: String
    var departureTime: String?
    var arrivalTime: String?
    var codeSize: CGFloat = 28
    var timeSize: CGFloat = 18

    var body: some View {
        HStack(alignment: .top, spacing: 0) {
            // Departure info
            VStack(spacing: 4) {
                Text(departureCode)
                    .font(FlightTheme.airportCode(codeSize))
                    .foregroundStyle(FlightTheme.textPrimary)

                if let departureTime {
                    Text(departureTime)
                        .font(FlightTheme.time(timeSize))
                        .foregroundStyle(FlightTheme.textSecondary)
                }
            }
            .frame(minWidth: 60)

            // Curved arc with airplane
            RouteArc()
                .padding(.top, 4)

            // Arrival info
            VStack(spacing: 4) {
                Text(arrivalCode)
                    .font(FlightTheme.airportCode(codeSize))
                    .foregroundStyle(FlightTheme.textPrimary)

                if let arrivalTime {
                    Text(arrivalTime)
                        .font(FlightTheme.time(timeSize))
                        .foregroundStyle(FlightTheme.textSecondary)
                }
            }
            .frame(minWidth: 60)
        }
    }
}

// MARK: - Curved Arc Shape

private struct ArcCurve: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        let startPoint = CGPoint(x: 0, y: rect.height)
        let endPoint = CGPoint(x: rect.width, y: rect.height)
        let controlPoint = CGPoint(x: rect.midX, y: rect.height - (rect.width * 0.3))

        path.move(to: startPoint)
        path.addQuadCurve(to: endPoint, control: controlPoint)

        return path
    }
}

// MARK: - Route Arc View

private struct RouteArc: View {
    var body: some View {
        GeometryReader { geometry in
            let width = geometry.size.width
            let height = geometry.size.height
            let arcHeight = width * 0.3
            let midX = width / 2

            // The peak of the arc: midpoint of the quadratic bezier
            // For a quad bezier from (0, h) to (w, h) with control (midX, h - arcH),
            // the midpoint at t=0.5 is at y = h - arcH/2
            let airplaneY = height - arcHeight / 2

            ZStack {
                // Dashed curved arc
                ArcCurve()
                    .stroke(
                        FlightTheme.routeArc,
                        style: StrokeStyle(
                            lineWidth: 1.5,
                            dash: [6, 4]
                        )
                    )

                // Airplane icon at the top of the arc
                Image(systemName: "airplane")
                    .font(.system(size: 14, weight: .regular))
                    .foregroundStyle(FlightTheme.textSecondary)
                    .rotationEffect(.degrees(-45))
                    .position(x: midX, y: airplaneY)
            }
        }
        .frame(height: 40)
        .frame(maxWidth: .infinity)
    }
}

// MARK: - Preview

#Preview {
    VStack(spacing: 40) {
        RouteVisualization(
            departureCode: "DFW",
            arrivalCode: "MAD",
            departureTime: "16:45",
            arrivalTime: "09:05"
        )

        RouteVisualization(
            departureCode: "JFK",
            arrivalCode: "LHR"
        )

        RouteVisualization(
            departureCode: "SFO",
            arrivalCode: "NRT",
            departureTime: "11:30",
            arrivalTime: "14:55",
            codeSize: 22,
            timeSize: 14
        )
    }
    .padding(24)
    .background(FlightTheme.background)
}
