use crate::movables::TriggerMovableEvent;
use bevy::{prelude::*, utils::hashbrown::HashSet};

#[derive(Resource)]
enum GatesState {
    Init,
    GateOpen1,
}

pub fn plugin(app: &mut App) {
    app.insert_resource(GatesState::Init);
    app.add_systems(Update, update);
}

fn update(
    mut commands: Commands,
    keyboard: Res<ButtonInput<KeyCode>>,
    mut state: ResMut<GatesState>,
    asset_server: Res<AssetServer>,
) {
    if keyboard.just_pressed(KeyCode::Digit1) {
        if matches!(*state, GatesState::Init) {
            *state = GatesState::GateOpen1;

            //TODO: put into a sound effects system
            commands.spawn((
                AudioPlayer::new(asset_server.load("sfx/effects/gate.ogg")),
                PlaybackSettings::DESPAWN,
            ));
            //TODO: put into actual key collect once we have that
            commands.spawn((
                AudioPlayer::new(asset_server.load("sfx/effects/key_collect.ogg")),
                PlaybackSettings::DESPAWN,
            ));

            let entities: HashSet<_> = vec!["fence_01", "fence_02"]
                .into_iter()
                .map(|s| String::from(s))
                .collect();

            commands.trigger(TriggerMovableEvent(entities));
        }
    }
}
