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

fn decal_ui(world: &mut World, entity: Entity, ui: &mut egui::Ui, _context: &mut InspectorContext) {
    if let Some(decal) = world.core.get_decal_mut(entity) {
        ui.label(egui::RichText::new("Texture").strong());
        let mut texture_name = decal.texture.clone().unwrap_or_default();
        if ui.text_edit_singleline(&mut texture_name).changed() {
            decal.texture = if texture_name.is_empty() {
                None
            } else {
                Some(texture_name)
            };
        }

        let mut emissive_name = decal.emissive_texture.clone().unwrap_or_default();
        ui.horizontal(|ui| {
            ui.label("Emissive Texture:");
            if ui.text_edit_singleline(&mut emissive_name).changed() {
                decal.emissive_texture = if emissive_name.is_empty() {
                    None
                } else {
                    Some(emissive_name)
                };
            }
        });

        ui.horizontal(|ui| {
            ui.label("Emissive Strength:");
            ui.add(egui::Slider::new(&mut decal.emissive_strength, 0.0..=10.0));
        });

        ui.separator();
        ui.label(egui::RichText::new("Appearance").strong());

        ui.horizontal(|ui| {
            ui.label("Color:");
            ui.color_edit_button_rgba_unmultiplied(&mut decal.color);
        });

        ui.horizontal(|ui| {
            ui.label("Width:");
            ui.add(
                egui::DragValue::new(&mut decal.size.x)
                    .speed(0.1)
                    .range(0.01..=100.0),
            );
            ui.label("Height:");
            ui.add(
                egui::DragValue::new(&mut decal.size.y)
                    .speed(0.1)
                    .range(0.01..=100.0),
            );
        });

        ui.separator();
        ui.label(egui::RichText::new("Projection").strong());

        ui.horizontal(|ui| {
            ui.label("Depth:");
            ui.add(egui::Slider::new(&mut decal.depth, 0.01..=10.0));
        });

        ui.horizontal(|ui| {
            ui.label("Normal Threshold:");
            ui.add(egui::Slider::new(&mut decal.normal_threshold, 0.0..=1.0));
        });

        ui.horizontal(|ui| {
            ui.label("Fade Start:");
            ui.add(
                egui::DragValue::new(&mut decal.fade_start)
                    .speed(1.0)
                    .range(0.0..=1000.0),
            );
        });

        ui.horizontal(|ui| {
            ui.label("Fade End:");
            ui.add(
                egui::DragValue::new(&mut decal.fade_end)
                    .speed(1.0)
                    .range(0.0..=1000.0),
            );
        });
    }
}

impl_simple_inspector!(
    DecalInspector,
    "Decal",
    entity_has_decal,
    set_decal,
    remove_decal,
    Decal::default(),
    decal_ui
);
