use egui::{Modal, Id, Layout, Align, RichText};

pub struct ErrorDialog {
    open: bool,
    message: String,
}

impl ErrorDialog {
    pub fn new() -> Self {
        Self {
            open: false,
            message: String::new(),
        }
    }

    pub fn open(&mut self, message: impl Into<String>) {
        self.open = true;
        self.message = message.into();
    }

    pub fn show(&mut self, ctx: &egui::Context) {
        if !self.open {
            return
        }

        let modal = Modal::new(Id::new("error_dialog")).show(ctx, |ui| {
            ui.set_width(300.0);

            ui.vertical_centered(|ui| {
                ui.label(RichText::new("Error").strong().size(16.0));
                ui.add_space(6.0);
                ui.separator();
                ui.add_space(8.0);
                ui.label(&self.message);
            });

            ui.add_space(8.0);
            ui.separator();

            ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
                if ui.button(RichText::new("Ok").strong()).clicked() {
                    self.open = false;
                    return;
                }
            });
        });
    }
}

impl Default for ErrorDialog {
    fn default() -> Self { Self::new() }
}