use super::{ComponentInspector, EditorContext};
use nightshade::prelude::*;

pub struct AnimationInspector;

impl ComponentInspector for AnimationInspector {
    fn name(&self) -> &str {
        "Animation Player"
    }

    fn has_component(&self, world: &World, entity: Entity) -> bool {
        world.entity_has_animation_player(entity)
    }

    fn add_component(&self, world: &mut World, entity: Entity) {
        world.add_components(entity, ANIMATION_PLAYER);
        world.set_animation_player(entity, AnimationPlayer::default());
    }

    fn remove_component(&self, world: &mut World, entity: Entity) {
        world.remove_animation_player(entity);
    }

    fn ui(
        &mut self,
        world: &mut World,
        entity: Entity,
        ui: &mut egui::Ui,
        _editor: &mut EditorContext,
    ) {
        if let Some(player) = world.get_animation_player_mut(entity) {
            ui.label(format!("Clips: {}", player.clips.len()));

            if player.clips.is_empty() {
                ui.label("No animation clips loaded");
                return;
            }

            let mut clip_to_play = None;
            ui.horizontal(|ui| {
                ui.label("Current Clip:");
                egui::ComboBox::from_id_salt("animation_clip_selector")
                    .selected_text(
                        player
                            .current_clip
                            .and_then(|index| player.clips.get(index))
                            .map(|clip| clip.name.as_str())
                            .unwrap_or("None"),
                    )
                    .show_ui(ui, |ui| {
                        for (index, clip) in player.clips.iter().enumerate() {
                            let is_selected = player.current_clip == Some(index);
                            if ui.selectable_label(is_selected, &clip.name).clicked() {
                                clip_to_play = Some(index);
                            }
                        }
                    });
            });
            if let Some(index) = clip_to_play {
                player.play(index);
            }

            if let Some(clip_index) = player.current_clip
                && let Some(clip) = player.clips.get(clip_index)
            {
                ui.label(format!("Duration: {:.2}s", clip.duration));
                ui.label(format!("Channels: {}", clip.channels.len()));
            }

            ui.separator();

            ui.horizontal(|ui| {
                ui.label("Time:");
                let duration = player
                    .current_clip
                    .and_then(|index| player.clips.get(index))
                    .map(|clip| clip.duration)
                    .unwrap_or(1.0);
                ui.add(egui::Slider::new(&mut player.time, 0.0..=duration).suffix("s"));
            });

            ui.horizontal(|ui| {
                ui.label("Speed:");
                ui.add(
                    egui::DragValue::new(&mut player.speed)
                        .speed(0.1)
                        .range(-5.0..=5.0),
                );
            });

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

            ui.separator();

            ui.horizontal(|ui| {
                if player.playing {
                    if ui.button("Pause").clicked() {
                        player.pause();
                    }
                } else if ui.button("Play").clicked() {
                    player.resume();
                }

                if ui.button("Stop").clicked() {
                    player.stop();
                }

                if ui.button("Restart").clicked()
                    && let Some(clip_index) = player.current_clip
                {
                    player.play(clip_index);
                }
            });

            ui.label(if player.playing {
                "Status: Playing"
            } else {
                "Status: Paused"
            });
        }
    }
}
