use bevy::prelude::*;
use bevy_debug_log::LogViewerVisibility;

pub const GIT_HASH: &str = env!("VERGEN_GIT_SHA");

pub fn plugin(app: &mut App) {
    app.add_systems(Update, update);
    app.add_systems(Startup, setup);
}

fn update(mut commands: Commands, keyboard: Res<ButtonInput<KeyCode>>, gamepads: Query<&Gamepad>) {
    if keyboard.just_pressed(KeyCode::Backquote) {
        commands.trigger(LogViewerVisibility::Toggle);
    }

    for g in gamepads.iter() {
        if g.just_pressed(GamepadButton::North) {
            commands.trigger(LogViewerVisibility::Toggle);
        }
    }
}

fn setup(mut commands: Commands) {
    commands.spawn((
        Name::new("githash-ui"),
        Text::new(GIT_HASH),
        TextFont {
            font_size: 12.0,
            ..default()
        },
        TextLayout::new_with_justify(JustifyText::Left),
        Node {
            position_type: PositionType::Absolute,
            top: Val::Px(5.0),
            left: Val::Px(5.0),
            ..default()
        },
    ));
}
