use egui_toast::{Toast, ToastOptions, Toasts};
use nightshade::editor_plugin::egui::{self, Align2, Direction, RichText};

pub use egui_toast::ToastKind as ToastLevel;

#[derive(Default)]
pub struct ToastService {
    toasts: Toasts,
    initialized: bool,
}

impl ToastService {
    pub fn show_toast(&mut self, text: impl Into<String>, level: ToastLevel) {
        self.toasts.add(Toast {
            text: RichText::new(text).strong().into(),
            kind: level,
            options: ToastOptions::default()
                .duration(std::time::Duration::from_secs(3))
                .show_progress(true)
                .show_icon(true),
            ..Default::default()
        });
    }

    pub fn show(&mut self, ctx: &egui::Context) {
        if !self.initialized {
            self.toasts = Toasts::new()
                .anchor(Align2::RIGHT_BOTTOM, (-10.0, -10.0))
                .direction(Direction::BottomUp);
            self.initialized = true;
        }
        self.toasts.show(ctx);
    }
}
