use egui::{
    RichText,
    scroll_area::{ScrollBarVisibility, ScrollSource},
};

pub struct Error {
    message: String,
    visible: bool,
}

impl Error {
    pub fn new(message: String) -> Self {
        Self {
            message,
            visible: true,
        }
    }

    pub fn show(&mut self, ui: &mut egui::Ui) {
        egui::Window::new("Error")
            .open(&mut self.visible)
            .fixed_size([200.0, 100.0])
            .show(ui.ctx(), |ui| {
                ui.label(self.message.clone());
            });
    }
}

pub fn saved_status(ui: &mut egui::Ui, saved: bool, id: &str, name: &str) {
    ui.group(|ui| {
        ui.set_max_width(ui.available_width());

        // Create a container that will take up the full width
        ui.horizontal(|ui| {
            // Add a ScrollArea that will contain our content
            egui::ScrollArea::horizontal()
                .scroll_source(ScrollSource::ALL)
                .scroll_bar_visibility(ScrollBarVisibility::AlwaysHidden)
                .auto_shrink([false, false]) // Don't shrink in either direction
                .show(ui, |ui| {
                    // Create a horizontal layout that will contain our content
                    ui.horizontal(|ui| {
                        // Now add your content
                        ui.strong(name);
                        if saved {
                            ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
                        } else {
                            ui.label(RichText::new("* Unsaved").color(egui::Color32::YELLOW));
                        }

                        if ui
                            .add(egui::Button::new(format!("[id: {id}]")).frame(false))
                            .on_hover_text(" Click id to copy!")
                            .clicked()
                        {
                            ui.ctx().copy_text(id.to_string());
                        }
                    });
                });
        });
    });
}
