//! Flight HUD for in-flight gameplay.
//!
//! Shown while the player ship has a [`FlightState`](super::FlightState)
//! component. Spawned/despawned reactively by the [`super::hud`] module.
//!
//! The speed indicator (bottom-left) and status panel (top-left) are spawned
//! as independent top-level overlays with no full-screen root, so they only
//! block camera input when the cursor is directly over a panel.

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 every flight HUD panel entity (spawned while flying).
#[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;

/// Spawn the flight HUD panels.
pub fn setup_flight_ui(mut commands: Commands) {
    bevy::log::info!("Setting up flight HUD");
    spawn_speed_indicator(&mut commands);
    spawn_status_panel(&mut commands);
}

/// Speed indicator (bottom-left).
fn spawn_speed_indicator(commands: &mut Commands) {
    commands
        .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| {
            panel.spawn((
                Text::new("SPEED"),
                TextFont {
                    font_size: 12.0,
                    ..default()
                },
                TextColor(TEXT_DIM),
            ));
            panel.spawn((
                Text::new("0 m/s"),
                TextFont {
                    font_size: 24.0,
                    ..default()
                },
                TextColor(TEXT_BRIGHT),
                SpeedDisplay,
            ));
        });
}

/// Status panel (top-left).
fn spawn_status_panel(commands: &mut Commands) {
    commands
        .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| {
            panel.spawn((
                Text::new("FLIGHT MODE"),
                TextFont {
                    font_size: 12.0,
                    ..default()
                },
                TextColor(ACCENT),
            ));
            panel.spawn((
                Text::new("Active"),
                TextFont {
                    font_size: 16.0,
                    ..default()
                },
                TextColor(TEXT_BRIGHT),
            ));
            panel.spawn((
                Text::new("Click: Set destination\nOrbit camera: Drag to rotate"),
                TextFont {
                    font_size: 11.0,
                    ..default()
                },
                TextColor(TEXT_DIM),
            ));
        });
}

/// Update the flight HUD speed readout from the player's [`FlightState`].
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;
    };

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