use nalgebra_glm::{Vec2, Vec3};
use nightshade::prelude::*;

pub fn default_point_light() -> Light {
    Light {
        light_type: LightType::Point,
        color: nalgebra_glm::vec3(1.0, 1.0, 1.0),
        intensity: 1.0,
        range: 10.0,
        ..Default::default()
    }
}

pub fn default_spot_light() -> Light {
    Light {
        light_type: LightType::Spot,
        color: nalgebra_glm::vec3(1.0, 1.0, 1.0),
        intensity: 1.0,
        range: 10.0,
        inner_cone_angle: 30.0_f32.to_radians(),
        outer_cone_angle: 45.0_f32.to_radians(),
        ..Default::default()
    }
}

pub fn default_directional_light() -> Light {
    Light {
        light_type: LightType::Directional,
        color: nalgebra_glm::vec3(1.0, 1.0, 1.0),
        intensity: 1.0,
        ..Default::default()
    }
}

pub fn spawn_point_light(world: &mut World) -> Entity {
    let entities = EntityBuilder::new()
        .with_name(Name("Point Light".to_string()))
        .with_local_transform(LocalTransform::default())
        .with_global_transform(GlobalTransform::default())
        .spawn(world, 1);
    let entity = entities[0];
    world.core.set_light(entity, default_point_light());
    entity
}

pub fn spawn_spot_light(world: &mut World) -> Entity {
    let entities = EntityBuilder::new()
        .with_name(Name("Spot Light".to_string()))
        .with_local_transform(LocalTransform::default())
        .with_global_transform(GlobalTransform::default())
        .spawn(world, 1);
    let entity = entities[0];
    world.core.set_light(entity, default_spot_light());
    entity
}

pub struct AddPrimitiveResult {
    pub close_popup: bool,
    pub spawned_entity: Option<Entity>,
}

pub fn add_primitive_popup_ui(
    ui_context: &egui::Context,
    world: &mut World,
    popup_pos: Vec2,
) -> AddPrimitiveResult {
    let mut close_popup = false;
    let mut spawned_entity = None;

    let area_response = egui::Area::new(egui::Id::new("add_primitive_popup"))
        .fixed_pos(egui::pos2(popup_pos.x, popup_pos.y))
        .order(egui::Order::Foreground)
        .show(ui_context, |ui| {
            egui::Frame::popup(ui.style()).show(ui, |ui| {
                ui.set_min_width(120.0);
                ui.label("Add Primitive");
                ui.separator();

                if ui.button("Cube").clicked() {
                    spawned_entity = Some(spawn_cube_at(world, Vec3::zeros()));
                    close_popup = true;
                }
                if ui.button("Sphere").clicked() {
                    spawned_entity = Some(spawn_sphere_at(world, Vec3::zeros()));
                    close_popup = true;
                }
                if ui.button("Cylinder").clicked() {
                    spawned_entity = Some(spawn_cylinder_at(world, Vec3::zeros()));
                    close_popup = true;
                }
                if ui.button("Cone").clicked() {
                    spawned_entity = Some(spawn_cone_at(world, Vec3::zeros()));
                    close_popup = true;
                }
                if ui.button("Torus").clicked() {
                    spawned_entity = Some(spawn_torus_at(world, Vec3::zeros()));
                    close_popup = true;
                }
                if ui.button("Plane").clicked() {
                    spawned_entity = Some(spawn_plane_at(world, Vec3::zeros()));
                    close_popup = true;
                }
                ui.separator();
                ui.label("Lighting");
                ui.separator();
                if ui.button("Point Light").clicked() {
                    spawned_entity = Some(spawn_point_light(world));
                    close_popup = true;
                }
                if ui.button("Spot Light").clicked() {
                    spawned_entity = Some(spawn_spot_light(world));
                    close_popup = true;
                }
                ui.separator();
                if ui.button("Empty").clicked() {
                    let entities = EntityBuilder::new()
                        .with_name(Name("Empty".to_string()))
                        .with_local_transform(LocalTransform::default())
                        .with_global_transform(GlobalTransform::default())
                        .spawn(world, 1);
                    spawned_entity = Some(entities[0]);
                    close_popup = true;
                }
            });
        });

    if super::should_dismiss_popup(ui_context, area_response.response.rect, close_popup) {
        close_popup = true;
    }

    AddPrimitiveResult {
        close_popup,
        spawned_entity,
    }
}
