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

pub struct RenderLayerInspector;

impl ComponentInspector for RenderLayerInspector {
    fn name(&self) -> &str {
        "Render Layer"
    }

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

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

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

    fn ui(
        &mut self,
        world: &mut World,
        entity: Entity,
        ui: &mut egui::Ui,
        _editor: &mut EditorContext,
    ) {
        if let Some(layer) = world.get_render_layer_mut(entity) {
            ui.label("Render Layer");

            let mut current_layer = layer.0;
            let layer_before = current_layer;

            ui.horizontal(|ui| {
                ui.radio_value(&mut current_layer, RenderLayer::WORLD, "World");
                ui.radio_value(&mut current_layer, RenderLayer::OVERLAY, "Overlay");
            });

            if current_layer != layer_before {
                layer.0 = current_layer;
            }

            ui.add_space(4.0);
            ui.label(
                egui::RichText::new(match current_layer {
                    RenderLayer::WORLD => {
                        "Objects in the world layer render normally with depth testing"
                    }
                    RenderLayer::OVERLAY => {
                        "Objects in the overlay layer always render on top of the world"
                    }
                    _ => "Custom layer",
                })
                .small()
                .color(egui::Color32::from_gray(150)),
            );
        }
    }
}
