use super::ComponentInspector;
use nightshade::prelude::*;

pub struct MaterialInspector;

impl ComponentInspector for MaterialInspector {
    fn name(&self) -> &str {
        "Material"
    }

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

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

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

    fn ui(&mut self, world: &mut World, entity: Entity, ui: &mut egui::Ui) {
        ui.label("Material");
        if let Some(material) = world.get_material_mut(entity) {
            ui.horizontal(|ui| {
                ui.label("Base Color:");
                if ui
                    .color_edit_button_rgba_unmultiplied(&mut material.base_color)
                    .changed()
                {}
            });

            ui.horizontal(|ui| {
                ui.label("R:");
                if ui
                    .add(
                        egui::Slider::new(&mut material.base_color[0], 0.0..=1.0).fixed_decimals(2),
                    )
                    .changed()
                {}
            });

            ui.horizontal(|ui| {
                ui.label("G:");
                if ui
                    .add(
                        egui::Slider::new(&mut material.base_color[1], 0.0..=1.0).fixed_decimals(2),
                    )
                    .changed()
                {}
            });

            ui.horizontal(|ui| {
                ui.label("B:");
                if ui
                    .add(
                        egui::Slider::new(&mut material.base_color[2], 0.0..=1.0).fixed_decimals(2),
                    )
                    .changed()
                {}
            });

            ui.horizontal(|ui| {
                ui.label("A:");
                if ui
                    .add(
                        egui::Slider::new(&mut material.base_color[3], 0.0..=1.0).fixed_decimals(2),
                    )
                    .changed()
                {}
            });

            ui.separator();

            ui.horizontal(|ui| {
                ui.label("Alpha Mode:");
                egui::ComboBox::from_id_salt("alpha_mode")
                    .selected_text(match material.alpha_mode {
                        AlphaMode::Opaque => "Opaque",
                        AlphaMode::Mask => "Mask",
                        AlphaMode::Blend => "Blend",
                    })
                    .show_ui(ui, |ui| {
                        ui.selectable_value(&mut material.alpha_mode, AlphaMode::Opaque, "Opaque");
                        ui.selectable_value(&mut material.alpha_mode, AlphaMode::Mask, "Mask");
                        ui.selectable_value(&mut material.alpha_mode, AlphaMode::Blend, "Blend");
                    });
            });

            if material.alpha_mode == AlphaMode::Mask {
                ui.horizontal(|ui| {
                    ui.label("Alpha Cutoff:");
                    if ui
                        .add(
                            egui::Slider::new(&mut material.alpha_cutoff, 0.0..=1.0)
                                .fixed_decimals(2),
                        )
                        .changed()
                    {}
                });
            }

            ui.separator();

            ui.horizontal(|ui| {
                ui.label("Roughness:");
                ui.add(egui::Slider::new(&mut material.roughness, 0.0..=1.0).fixed_decimals(2));
            });

            ui.horizontal(|ui| {
                ui.label("Metallic:");
                ui.add(egui::Slider::new(&mut material.metallic, 0.0..=1.0).fixed_decimals(2));
            });

            ui.horizontal(|ui| {
                ui.label("Unlit:");
                ui.checkbox(&mut material.unlit, "");
            });

            ui.separator();

            ui.horizontal(|ui| {
                ui.label("Emissive Factor:");
            });

            ui.horizontal(|ui| {
                ui.label("R:");
                ui.add(
                    egui::Slider::new(&mut material.emissive_factor[0], 0.0..=1.0)
                        .fixed_decimals(2),
                );
            });

            ui.horizontal(|ui| {
                ui.label("G:");
                ui.add(
                    egui::Slider::new(&mut material.emissive_factor[1], 0.0..=1.0)
                        .fixed_decimals(2),
                );
            });

            ui.horizontal(|ui| {
                ui.label("B:");
                ui.add(
                    egui::Slider::new(&mut material.emissive_factor[2], 0.0..=1.0)
                        .fixed_decimals(2),
                );
            });

            ui.separator();

            ui.label("Textures:");

            let mut base_texture_str = material.base_texture.clone().unwrap_or_default();
            ui.horizontal(|ui| {
                ui.label("Base Texture:");
                if ui.text_edit_singleline(&mut base_texture_str).changed() {
                    material.base_texture = if base_texture_str.is_empty() {
                        None
                    } else {
                        Some(base_texture_str.clone())
                    };
                }
            });

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