use super::{ComponentInspector, EditorContext};
use nightshade::ecs::water::{VolumeFlowType, VolumeShape, Water};
use nightshade::prelude::*;

pub struct WaterInspector;

impl ComponentInspector for WaterInspector {
    fn name(&self) -> &str {
        "Water"
    }

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

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

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

    fn ui(
        &mut self,
        world: &mut World,
        entity: Entity,
        ui: &mut egui::Ui,
        _editor: &mut EditorContext,
    ) {
        if let Some(water) = world.get_water_mut(entity) {
            ui.horizontal(|ui| {
                ui.label("Enabled:");
                ui.checkbox(&mut water.enabled, "");
            });

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

            ui.horizontal(|ui| {
                ui.label("Base Color:");
                ui.color_edit_button_rgba_unmultiplied(&mut water.base_color);
            });

            ui.horizontal(|ui| {
                ui.label("Water Color:");
                ui.color_edit_button_rgba_unmultiplied(&mut water.water_color);
            });

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

            ui.horizontal(|ui| {
                ui.label("Base Height:");
                ui.add(egui::DragValue::new(&mut water.base_height).speed(0.1));
            });

            ui.horizontal(|ui| {
                ui.label("Wave Height:");
                ui.add(egui::Slider::new(&mut water.wave_height, 0.0..=2.0));
            });

            ui.horizontal(|ui| {
                ui.label("Choppy:");
                ui.add(egui::Slider::new(&mut water.choppy, 0.0..=10.0));
            });

            ui.horizontal(|ui| {
                ui.label("Speed:");
                ui.add(egui::Slider::new(&mut water.speed, 0.0..=5.0));
            });

            ui.horizontal(|ui| {
                ui.label("Frequency:");
                ui.add(egui::Slider::new(&mut water.frequency, 0.01..=2.0));
            });

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

            ui.horizontal(|ui| {
                ui.label("Specular Strength:");
                ui.add(egui::Slider::new(&mut water.specular_strength, 0.0..=5.0));
            });

            ui.horizontal(|ui| {
                ui.label("Fresnel Power:");
                ui.add(egui::Slider::new(&mut water.fresnel_power, 0.0..=10.0));
            });

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

            ui.horizontal(|ui| {
                ui.label("Vertical:");
                ui.checkbox(&mut water.is_vertical, "");
            });

            ui.horizontal(|ui| {
                ui.label("Volumetric:");
                ui.checkbox(&mut water.is_volumetric, "");
            });

            if water.is_volumetric {
                ui.separator();
                ui.label(egui::RichText::new("Volumetric Settings").strong());

                ui.horizontal(|ui| {
                    ui.label("Shape:");
                    egui::ComboBox::from_id_salt("volume_shape")
                        .selected_text(match water.volume_shape {
                            VolumeShape::Box => "Box",
                            VolumeShape::Cylinder => "Cylinder",
                            VolumeShape::Sphere => "Sphere",
                        })
                        .show_ui(ui, |ui| {
                            ui.selectable_value(&mut water.volume_shape, VolumeShape::Box, "Box");
                            ui.selectable_value(
                                &mut water.volume_shape,
                                VolumeShape::Cylinder,
                                "Cylinder",
                            );
                            ui.selectable_value(
                                &mut water.volume_shape,
                                VolumeShape::Sphere,
                                "Sphere",
                            );
                        });
                });

                ui.horizontal(|ui| {
                    ui.label("Flow Type:");
                    egui::ComboBox::from_id_salt("volume_flow_type")
                        .selected_text(match water.volume_flow_type {
                            VolumeFlowType::Waterfall => "Waterfall",
                            VolumeFlowType::Mist => "Mist",
                            VolumeFlowType::Cascade => "Cascade",
                        })
                        .show_ui(ui, |ui| {
                            ui.selectable_value(
                                &mut water.volume_flow_type,
                                VolumeFlowType::Waterfall,
                                "Waterfall",
                            );
                            ui.selectable_value(
                                &mut water.volume_flow_type,
                                VolumeFlowType::Mist,
                                "Mist",
                            );
                            ui.selectable_value(
                                &mut water.volume_flow_type,
                                VolumeFlowType::Cascade,
                                "Cascade",
                            );
                        });
                });

                ui.label("Volume Size:");
                ui.horizontal(|ui| {
                    ui.label("X:");
                    ui.add(
                        egui::DragValue::new(&mut water.volume_size[0])
                            .speed(0.1)
                            .range(0.1..=100.0),
                    );
                    ui.label("Y:");
                    ui.add(
                        egui::DragValue::new(&mut water.volume_size[1])
                            .speed(0.1)
                            .range(0.1..=100.0),
                    );
                    ui.label("Z:");
                    ui.add(
                        egui::DragValue::new(&mut water.volume_size[2])
                            .speed(0.1)
                            .range(0.1..=100.0),
                    );
                });
            }

            if !water.is_volumetric {
                ui.separator();
                ui.label(egui::RichText::new("Edge Settings").strong());

                ui.horizontal(|ui| {
                    ui.label("Edge Feather:");
                    ui.add(egui::Slider::new(
                        &mut water.edge_feather_distance,
                        0.0..=10.0,
                    ));
                });
            }
        }
    }
}
