use super::{ComponentInspector, InspectorContext, impl_simple_inspector};
use nightshade::prelude::*;

fn lines_ui(world: &mut World, entity: Entity, ui: &mut egui::Ui, _context: &mut InspectorContext) {
    if let Some(lines) = world.core.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();
        }
    }
}

impl_simple_inspector!(
    LinesInspector,
    "Lines",
    entity_has_lines,
    set_lines,
    remove_lines,
    Lines::default(),
    lines_ui
);
