use super::{ComponentInspector, InspectorContext, impl_simple_inspector};
use nightshade::ecs::audio::components::{AudioListener, AudioSource};
use nightshade::prelude::*;

fn audio_source_ui(
    world: &mut World,
    entity: Entity,
    ui: &mut egui::Ui,
    _context: &mut InspectorContext,
) {
    if let Some(source) = world.core.get_audio_source_mut(entity) {
        ui.label(egui::RichText::new("Audio Source").strong());

        let mut audio_ref_text = source.audio_ref.clone().unwrap_or_default();
        ui.horizontal(|ui| {
            ui.label("Audio Asset:");
            if ui.text_edit_singleline(&mut audio_ref_text).changed() {
                source.audio_ref = if audio_ref_text.is_empty() {
                    None
                } else {
                    Some(audio_ref_text)
                };
            }
        });

        ui.horizontal(|ui| {
            ui.label("Volume:");
            ui.add(egui::Slider::new(&mut source.volume, 0.0..=1.0));
        });

        ui.horizontal(|ui| {
            ui.label("Looping:");
            ui.checkbox(&mut source.looping, "");
        });

        ui.horizontal(|ui| {
            ui.label("Spatial:");
            ui.checkbox(&mut source.spatial, "");
        });

        ui.horizontal(|ui| {
            ui.label("Reverb:");
            ui.checkbox(&mut source.reverb, "");
        });

        ui.horizontal(|ui| {
            ui.label("Playing:");
            ui.label(if source.playing { "Yes" } else { "No" });
        });
    }
}

impl_simple_inspector!(
    AudioSourceInspector,
    "Audio Source",
    entity_has_audio_source,
    set_audio_source,
    remove_audio_source,
    AudioSource::default(),
    audio_source_ui
);

impl_simple_inspector!(
    AudioListenerInspector,
    "Audio Listener",
    entity_has_audio_listener,
    set_audio_listener,
    remove_audio_listener,
    AudioListener
);
