use crate::Editor;
use crate::engine_editor::{
    EntityContextAction, TransformOperation, UndoableOperation, add_primitive_popup_ui,
    capture_hierarchy, entity_context_menu_ui, gizmo, selection,
};
use nightshade::prelude::*;

impl Editor {
    pub fn handle_popups(&mut self, world: &mut World, ui_context: &egui::Context) {
        if let Some(popup_pos) = self.context.ui.popup.add_primitive {
            let result = add_primitive_popup_ui(ui_context, world, popup_pos);
            if result.close_popup {
                self.context.ui.popup.add_primitive = None;
            }
            if let Some(entity) = result.spawned_entity {
                let hierarchy = Box::new(capture_hierarchy(world, entity));
                self.context.editor.undo_history.push(
                    UndoableOperation::EntityCreated {
                        hierarchy,
                        current_entity: entity,
                    },
                    "Add primitive".to_string(),
                );
                self.context.editor.selection.set_single(entity);
                self.context.ui.tree_dirty = true;
                self.project_state.mark_modified();
            }
        }

        if let Some(popup_pos) = self.context.ui.popup.entity_context_menu {
            let result = entity_context_menu_ui(
                ui_context,
                world,
                &self.context.editor.selection,
                popup_pos,
            );
            if result.close_popup {
                self.context.ui.popup.entity_context_menu = None;
            }
            match result.action {
                EntityContextAction::Grab => {
                    gizmo::start_modal_transform(
                        &mut self.context.editor,
                        world,
                        TransformOperation::Grab,
                    );
                }
                EntityContextAction::Rotate => {
                    gizmo::start_modal_transform(
                        &mut self.context.editor,
                        world,
                        TransformOperation::Rotate,
                    );
                }
                EntityContextAction::Scale => {
                    gizmo::start_modal_transform(
                        &mut self.context.editor,
                        world,
                        TransformOperation::Scale,
                    );
                }
                EntityContextAction::Duplicate => {
                    let changed = selection::duplicate_selection(&mut self.context.editor, world);
                    self.context.ui.tree_dirty |= changed;
                    if changed {
                        self.project_state.mark_modified();
                    }
                }
                EntityContextAction::Delete => {
                    let changed = selection::delete_selection(&mut self.context.editor, world);
                    self.context.ui.tree_dirty |= changed;
                    if changed {
                        self.project_state.mark_modified();
                    }
                }
                EntityContextAction::None => {}
            }
        }
    }

    pub fn render_quit_confirmation(&mut self, world: &mut World, ui_context: &egui::Context) {
        if !self.context.ui.popup.quit_confirmation {
            return;
        }

        let mut open = true;
        egui::Window::new("Quit")
            .collapsible(false)
            .resizable(false)
            .open(&mut open)
            .anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0])
            .show(ui_context, |ui| {
                if self.project_state.is_modified {
                    ui.label("You have unsaved changes. Are you sure you want to quit?");
                } else {
                    ui.label("Are you sure you want to quit?");
                }
                ui.add_space(8.0);
                ui.horizontal(|ui| {
                    if ui.button("Quit").clicked() {
                        world.resources.window.should_exit = true;
                        self.context.ui.popup.quit_confirmation = false;
                    }
                    if ui.button("Cancel").clicked() {
                        self.context.ui.popup.quit_confirmation = false;
                    }
                });
            });
        if !open {
            self.context.ui.popup.quit_confirmation = false;
        }
    }
}
