use crate::Editor;
use crate::engine_editor::{picking, render_add_node_modal};
use crate::widgets::AssetCategory;
use crate::windows::{animations_panel_ui, assets_panel_ui, visual_effects_panel_ui};
use nightshade::prelude::*;

impl Editor {
    pub fn render_auxiliary_windows(&mut self, world: &mut World, ui_context: &egui::Context) {
        self.render_drag_drop_overlay(ui_context);
        self.render_assets_panel(ui_context);
        self.flush_notifications();

        animations_panel_ui(
            ui_context,
            world,
            &mut self.context.ui.visibility.animations_panel,
            &mut self.context.editor.selection,
            &mut self.context.editor.undo_history,
            &mut self.project_state,
            &mut self.context.ui.tree_dirty,
        );

        visual_effects_panel_ui(
            ui_context,
            world,
            &mut self.context.ui.visibility.visual_effects_panel,
            &mut self.context.assets.grayscale_enabled,
        );

        if render_add_node_modal(
            &mut self.context.editor.selection,
            &mut self.context.editor.undo_history,
            &mut self.context.ui.popup.add_node_open,
            &mut self.context.ui.popup.add_node_search,
            &mut self.tree_cache,
            world,
            ui_context,
        ) {
            self.project_state.mark_modified();
        }

        crate::mosaic::render_theme_editor_window(ui_context, &mut self.theme_state);

        crate::asset_loading::handle_asset_drop(&mut self.context, &self.mosaic, world, ui_context);
    }

    fn render_drag_drop_overlay(&self, ui_context: &egui::Context) {
        if self.context.drag_drop.hovered_file_path.is_some() {
            let message = match self.context.drag_drop.file_category {
                Some(AssetCategory::Texture) => "Drop HDR file to use as skybox",
                Some(AssetCategory::Mesh) => "Drop mesh file to import to scene",
                Some(AssetCategory::Project) => "Drop project file to load",
                _ => "Drop file to load",
            };
            crate::mosaic::render_drop_overlay_with_category(ui_context, None, &[], message);
        }
    }

    fn render_assets_panel(&mut self, ui_context: &egui::Context) {
        #[cfg(not(target_arch = "wasm32"))]
        {
            let assets_result = assets_panel_ui(
                ui_context,
                self.project.as_ref(),
                &mut self.context.ui.visibility.assets_panel,
                &mut self.context.drag_drop.pending_asset_update,
            );
            if assets_result.scan_requested {
                crate::project_io::assets::scan_assets_directory(
                    &self.context.project_edit,
                    &mut self.context.assets.prefabs,
                    &mut self.toasts,
                );
            }
        }
        #[cfg(target_arch = "wasm32")]
        assets_panel_ui(
            ui_context,
            self.project.as_ref(),
            &mut self.context.ui.visibility.assets_panel,
            &mut self.context.drag_drop.pending_asset_update,
        );
        if let Some((key, new_path)) = self.context.drag_drop.pending_asset_update.take() {
            crate::project_io::assets::update_asset_path(
                &mut self.project,
                &mut self.project_state,
                #[cfg(not(target_arch = "wasm32"))]
                &mut self.toasts,
                &key,
                &new_path,
            );
        }
    }

    pub fn flush_notifications(&mut self) {
        #[cfg(not(target_arch = "wasm32"))]
        for (kind, message) in self.context.notifications.drain(..) {
            self.toasts.push(kind, message, 3.0);
        }
    }

    pub fn render_overlays(&mut self, world: &mut World, ui_context: &egui::Context) {
        picking::draw_marquee_selection(&self.context.editor.marquee, ui_context);

        #[cfg(not(target_arch = "wasm32"))]
        {
            let delta_time = world.resources.window.timing.delta_time;
            self.toasts.tick(delta_time);
            self.toasts.render(ui_context);
        }
        #[cfg(target_arch = "wasm32")]
        let _ = world;
    }
}
