use games::*;

fn main() {
    App::new()
        .init_resource::<Thing>()
        .add_plugins(BaseGamePlugin::default())
        .add_systems(Startup, init_ui)
        .add_systems(
            Update,
            (
                sync_resource_to_ui::<Thing>.run_if(resource_changed::<Thing>),
                update_foo.run_if(on_message::<MouseMotion>),
            ),
        )
        .run();
}

#[derive(Resource, Debug, Default)]
struct Thing(u8);

impl Display for Thing {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

fn init_ui(mut commands: Commands) {
    commands.spawn((
        Node {
            align_self: AlignSelf::Start,
            justify_self: JustifySelf::Center,
            ..default()
        },
        Text("Displays a sync'd resource tracking the number of mouse events (mod 256)".into()),
    ));
    commands.spawn((
        Node {
            align_self: AlignSelf::Center,
            justify_self: JustifySelf::Center,
            ..default()
        },
        Text("Placeholder".into()),
        SyncResource::<Thing>::default(),
    ));
}

fn update_foo(mut events: MessageReader<MouseMotion>, mut thing: ResMut<Thing>) {
    events.read().for_each(|_| {
        thing.0 = thing.0.overflowing_add(1).0;
    });
}
