use crate::Editor;
#[cfg(not(target_arch = "wasm32"))]
use crate::settings;
#[cfg(not(target_arch = "wasm32"))]
use nightshade::prelude::*;

impl Editor {
    #[cfg(not(target_arch = "wasm32"))]
    pub fn display_project_menu(&mut self, world: &mut World, ui: &mut egui::Ui) {
        let light_green = egui::Color32::from_rgb(144, 238, 144);

        ui.menu_button(
            egui::RichText::new("🔨 Project").color(light_green),
            |ui| {
                if ui.button("New Project").clicked() {
                    self.new_project(world);
                    ui.close();
                }
                ui.separator();
                if ui.button("Save Project").clicked() {
                    self.save_project(world);
                    ui.close();
                }
                if ui.button("Save Project As...").clicked() {
                    self.save_project_as(world);
                    ui.close();
                }
                if ui.button("Collect All and Save...").clicked() {
                    self.collect_all_and_save(world);
                    ui.close();
                }
                if ui.button("Load Project...").clicked() {
                    self.load_project(world);
                    ui.close();
                }
                ui.separator();

                let current_project_is_startup = if let Some(current_path) =
                    &self.context.project_edit.current_path
                    && let Some(startup_path) = &self.settings.data.startup_project
                {
                    current_path.to_string_lossy() == *startup_path
                } else {
                    false
                };

                if ui
                    .add_enabled(
                        self.context.project_edit.current_path.is_some()
                            && !current_project_is_startup,
                        egui::Button::new("Set as Startup Project"),
                    )
                    .clicked()
                {
                    crate::project_io::startup::set_as_startup_project(
                        &self.context.project_edit,
                        &mut self.settings,
                        &mut self.toasts,
                    );
                    ui.close();
                }
                if ui
                    .add_enabled(
                        self.settings.data.startup_project.is_some(),
                        egui::Button::new("Unset Startup Project"),
                    )
                    .clicked()
                {
                    crate::project_io::startup::unset_startup_project(
                        &mut self.settings,
                        &mut self.toasts,
                    );
                    ui.close();
                }
                ui.separator();

                let startup_project = self.settings.data.startup_project.clone();
                let action = self.settings.data.recent_projects.render_menu(
                    ui,
                    startup_project.as_deref(),
                    |path| settings::get_project_name(&path.to_string_lossy()),
                );
                if let Some(action) = action {
                    match action {
                        crate::mosaic::RecentFilesAction::Open(path) => {
                            self.load_project_from_path(world, &path);
                            ui.close();
                        }
                        crate::mosaic::RecentFilesAction::Clear => {
                            self.settings.data.recent_projects.clear();
                            if let Some(startup_path) = &self.settings.data.startup_project {
                                self.settings
                                    .data
                                    .recent_projects
                                    .add(std::path::PathBuf::from(startup_path));
                            }
                            if let Err(error) = self.settings.save() {
                                tracing::error!("Failed to save settings: {error}");
                            }
                            ui.close();
                        }
                    }
                }
            },
        );

        let project_name = self
            .context
            .project_edit
            .current_name
            .clone()
            .unwrap_or_else(|| "Untitled".to_string());
        let display_name = if self.project_state.is_modified {
            format!("{} *", project_name)
        } else {
            project_name.clone()
        };

        let text_color = if ui.visuals().dark_mode {
            egui::Color32::from_rgb(246, 247, 245)
        } else {
            egui::Color32::from_rgb(25, 25, 24)
        };
        ui.label(egui::RichText::new(&display_name).color(text_color));

        if ui
            .small_button("✏")
            .on_hover_text("Edit project name")
            .clicked()
        {
            self.context.project_edit.editing_name = true;
            self.context.project_edit.name_edit_buffer = project_name;
        }
    }
}
