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

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

impl_simple_inspector!(
    WaterInspector,
    "Water",
    entity_has_water,
    set_water,
    remove_water,
    Water::default(),
    water_ui
);
