//! Renders game-registered editor panel state (ADR 0007 dogfood UI).

use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts, EguiPrimaryContextPass};

use game::editor_ext::FpsDemoPanelState;

pub struct GameEditorPanelsPlugin;

impl Plugin for GameEditorPanelsPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(EguiPrimaryContextPass, fps_demo_panel_ui);
    }
}

fn fps_demo_panel_ui(mut contexts: EguiContexts, mut panel: ResMut<FpsDemoPanelState>) -> Result {
    let Ok(ctx) = contexts.ctx_mut() else {
        return Ok(());
    };

    if panel.headline.is_empty() {
        panel.headline = "FPS Foundation Demo".into();
    }

    if ctx.input(|input| {
        input.modifiers.command && input.key_pressed(egui::Key::G) && input.modifiers.shift
    }) {
        panel.open = !panel.open;
    }

    if !panel.open {
        return Ok(());
    }

    let mut open = panel.open;
    egui::Window::new("FPS Demo (Game EditorPlugin)")
        .open(&mut open)
        .default_width(280.0)
        .show(ctx, |ui| {
            ui.label("Registered from `game::editor_ext` via ADR 0007.");
            ui.small("Toggle: Ctrl+Shift+G");
            ui.separator();
            ui.heading(&panel.headline);
            ui.label("Game crate owns panel state; editor renders the dock window.");
        });
    panel.open = open;
    Ok(())
}
