use crate::{
    cutscene::StartCutscene, global_observer, keys::KeyCollected, movables::TriggerMovableEvent,
    sounds::PlaySound,
};
use bevy::{platform::collections::HashSet, prelude::*};

pub fn plugin(app: &mut App) {
    global_observer!(app, on_key_collected);
}

fn on_key_collected(trigger: Trigger<KeyCollected>, mut commands: Commands) {
    match trigger.event().0.as_str() {
        "fence_gate" => {
            commands.trigger(StartCutscene("fence_01".to_string()));

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

            commands.trigger(PlaySound::Gate);
            commands.trigger(TriggerMovableEvent(entities));
        }
        "fence_shaft" => {
            commands.trigger(StartCutscene("cutscene_02".to_string()));

            let entities: HashSet<_> = vec!["fence_shaft"].into_iter().map(String::from).collect();

            commands.trigger(PlaySound::Gate);
            commands.trigger(TriggerMovableEvent(entities));
        }
        _ => {
            error!("unknown key logic: {}", trigger.event().0);
        }
    }
}
