//! Flight HUD and UI elements for in-flight gameplay.

use bevy::prelude::*;

use super::FlightState;

const PANEL_BG: Color = Color::srgba(0.05, 0.07, 0.12, 0.8);
const PANEL_BORDER: Color = Color::srgb(0.25, 0.40, 0.62);
const TEXT_BRIGHT: Color = Color::srgb(0.82, 0.90, 1.0);
const TEXT_DIM: Color = Color::srgb(0.55, 0.65, 0.82);
const ACCENT: Color = Color::srgb(0.30, 0.72, 0.45);

/// Marker for flight HUD entities.
#[derive(Component)]
pub struct FlightUi;

/// Marker for the speed display text.
#[derive(Component)]
pub struct SpeedDisplay;

/// Marker for the status panel.
#[derive(Component)]
pub struct StatusPanel;

/// Setup the flight HUD when entering flight mode.
pub fn setup_flight_ui(mut commands: Commands) {
    bevy::log::info!("Setting up flight HUD");

    commands
        .spawn((
            Node {
                width: Val::Percent(100.0),
                height: Val::Percent(100.0),
                position_type: PositionType::Absolute,
                ..default()
            },
            Interaction::None, // Disable interaction - let clicks pass through
            FlightUi,
        ))
        .with_children(|root| {
            // Speed indicator (bottom left)
            spawn_speed_indicator(root);

            // Status panel (top left)
            spawn_status_panel(root);
        });
}

fn spawn_speed_indicator(parent: &mut ChildSpawnerCommands) {
    parent
        .spawn((
            Node {
                position_type: PositionType::Absolute,
                left: Val::Px(20.0),
                bottom: Val::Px(20.0),
                width: Val::Px(200.0),
                padding: UiRect::all(Val::Px(12.0)),
                border: UiRect::all(Val::Px(1.0)),
                ..default()
            },
            BackgroundColor(PANEL_BG),
            BorderColor(PANEL_BORDER),
            BorderRadius::all(Val::Px(8.0)),
            FlightUi,
        ))
        .with_children(|panel| {
            // Label
            panel.spawn((
                Text::new("SPEED"),
                TextFont {
                    font_size: 12.0,
                    ..default()
                },
                TextColor(TEXT_DIM),
            ));

            // Speed value
            panel.spawn((
                Text::new("0 m/s"),
                TextFont {
                    font_size: 24.0,
                    ..default()
                },
                TextColor(TEXT_BRIGHT),
                SpeedDisplay,
            ));
        });
}

fn spawn_status_panel(parent: &mut ChildSpawnerCommands) {
    parent
        .spawn((
            Node {
                position_type: PositionType::Absolute,
                left: Val::Px(20.0),
                top: Val::Px(20.0),
                width: Val::Px(250.0),
                padding: UiRect::all(Val::Px(12.0)),
                border: UiRect::all(Val::Px(1.0)),
                ..default()
            },
            BackgroundColor(PANEL_BG),
            BorderColor(PANEL_BORDER),
            BorderRadius::all(Val::Px(8.0)),
            FlightUi,
            StatusPanel,
        ))
        .with_children(|panel| {
            // Status label
            panel.spawn((
                Text::new("FLIGHT MODE"),
                TextFont {
                    font_size: 12.0,
                    ..default()
                },
                TextColor(ACCENT),
            ));

            // Flight status text
            panel.spawn((
                Text::new("Active"),
                TextFont {
                    font_size: 16.0,
                    ..default()
                },
                TextColor(TEXT_BRIGHT),
            ));

            // Controls hint
            panel.spawn((
                Node {
                    margin: UiRect::top(Val::Px(12.0)),
                    ..default()
                },
                FlightUi,
            ))
            .with_children(|hint| {
                hint.spawn((
                    Text::new("Click: Set destination\nOrbit camera: Drag to rotate"),
                    TextFont {
                        font_size: 11.0,
                        ..default()
                    },
                    TextColor(TEXT_DIM),
                ));
            });
        });
}

/// Update flight HUD elements with current flight state.
pub fn update_flight_ui(
    flight_query: Query<&FlightState>,
    mut speed_query: Query<&mut Text, With<SpeedDisplay>>,
) {
    let Ok(flight_state) = flight_query.single() else {
        return;
    };

    // Update speed display
    if let Ok(mut speed_text) = speed_query.single_mut() {
        speed_text.0 = format!("{:.0} m/s", flight_state.current_speed);
    }
}

/// Despawn flight HUD when exiting flight mode.
pub fn despawn_flight_ui(mut commands: Commands, query: Query<Entity, With<FlightUi>>) {
    for entity in &query {
        commands.entity(entity).despawn();
    }
}
