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

pub struct LinesInspector;

impl ComponentInspector for LinesInspector {
    fn name(&self) -> &str {
        "Lines"
    }

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

    fn add_component(&self, world: &mut World, entity: Entity) {
        world.set_lines(entity, Lines::default());
    }

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

    fn ui(
        &mut self,
        world: &mut World,
        entity: Entity,
        ui: &mut egui::Ui,
        _editor: &mut EditorContext,
    ) {
        if let Some(lines) = world.get_lines_mut(entity) {
            ui.label(format!("Line Count: {}", lines.lines.len()));

            ui.separator();

            if ui.button("Add Line").clicked() {
                lines.push(Line::default());
            }

            ui.separator();

            let mut to_remove = None;
            for (index, line) in lines.lines.iter_mut().enumerate() {
                ui.group(|ui| {
                    ui.label(format!("Line {}", index));

                    ui.horizontal(|ui| {
                        ui.label("Start:");
                        ui.add(
                            egui::DragValue::new(&mut line.start.x)
                                .speed(0.1)
                                .prefix("X: "),
                        );
                        ui.add(
                            egui::DragValue::new(&mut line.start.y)
                                .speed(0.1)
                                .prefix("Y: "),
                        );
                        ui.add(
                            egui::DragValue::new(&mut line.start.z)
                                .speed(0.1)
                                .prefix("Z: "),
                        );
                    });

                    ui.horizontal(|ui| {
                        ui.label("End:");
                        ui.add(
                            egui::DragValue::new(&mut line.end.x)
                                .speed(0.1)
                                .prefix("X: "),
                        );
                        ui.add(
                            egui::DragValue::new(&mut line.end.y)
                                .speed(0.1)
                                .prefix("Y: "),
                        );
                        ui.add(
                            egui::DragValue::new(&mut line.end.z)
                                .speed(0.1)
                                .prefix("Z: "),
                        );
                    });

                    ui.horizontal(|ui| {
                        ui.label("Color:");
                        let mut color = [line.color.x, line.color.y, line.color.z, line.color.w];
                        if ui.color_edit_button_rgba_unmultiplied(&mut color).changed() {
                            line.color.x = color[0];
                            line.color.y = color[1];
                            line.color.z = color[2];
                            line.color.w = color[3];
                        }
                    });

                    if ui.button("Remove Line").clicked() {
                        to_remove = Some(index);
                    }
                });

                ui.add_space(4.0);
            }

            if let Some(index) = to_remove {
                lines.lines.remove(index);
                lines.mark_dirty();
            }

            if ui.button("Clear All Lines").clicked() {
                lines.clear();
            }
        }
    }
}
