//! Development tools for the game. This plugin is only enabled in dev builds.

use bevy::{
    color::palettes::css::*,
    dev_tools::states::log_transitions,
    input::common_conditions::{input_just_pressed, input_toggle_active},
    prelude::*,
    ui::UiDebugOptions,
};

#[cfg(all(feature = "dev_native", feature = "inspector"))]
use bevy_inspector_egui::{
    DefaultInspectorConfigPlugin, bevy_egui::EguiPlugin, quick::WorldInspectorPlugin,
};

use crate::screens::Screen;

const TOGGLE_KEY: KeyCode = KeyCode::Backquote;
const TOGGLE_ORIG: KeyCode = KeyCode::NumpadAdd;

pub(super) fn plugin(app: &mut App) {
    // activate inspector if feature is active
    #[cfg(all(feature = "dev_native", feature = "inspector"))]
    app.add_plugins((
        DefaultInspectorConfigPlugin,
        EguiPlugin {
            enable_multipass_for_primary_context: true,
        },
        WorldInspectorPlugin::default().run_if(input_toggle_active(false, KeyCode::Backquote)),
    ));
    // Log `Screen` state transitions.
    app.add_systems(Update, log_transitions::<Screen>);

    // Toggle the debug overlay for UI.
    app.add_systems(
        Update,
        toggle_debug_ui.run_if(input_just_pressed(TOGGLE_KEY)),
    );
    app.add_systems(
        Update,
        toggle_origin.run_if(input_just_pressed(TOGGLE_ORIG)),
    );
}

fn toggle_debug_ui(mut options: ResMut<UiDebugOptions>) {
    options.toggle();
}

fn toggle_origin(mut gizmos: Gizmos) {
    info!("cucu?");
    gizmos.cross_2d(Vec2::new(-0., 0.), 12., FUCHSIA);
}
