use std::path::PathBuf;
use egui::Ui;
use egui_file_dialog::FileDialog;
use crate::ui::debugger::Action;


pub struct DsaFileDialog<T> {
    inner: FileDialog, // or whatever backend you're using
    constructor: Option<Box<dyn FnOnce(PathBuf) -> T>>,
}

impl<T> DsaFileDialog<T> {
    pub fn new() -> Self {
        Self {
            inner: egui_file_dialog::FileDialog::new()
                .add_save_extension("DSA source", "dsa")
                .add_save_extension("DSA binary", "dsb")
                .add_save_extension("DSC source", "dsc"),
            constructor: None,
        }
    }

    pub fn open_pick(&mut self, constructor: impl FnOnce(PathBuf) -> T + 'static) {
        self.constructor = Some(Box::new(constructor));
        self.inner.pick_file();
    }

    pub fn open_save(&mut self, constructor: impl FnOnce(PathBuf) -> T + 'static) {
        self.constructor = Some(Box::new(constructor));
        self.inner.save_file();
    }

    /// Call every frame. Returns Some(action) the frame a file is picked.
    pub fn show(&mut self, ctx: &egui::Context) -> Option<T> {
        self.inner.update(ctx);

        if let Some(path) = self.inner.take_picked() {
            return self.constructor.take().map(|f| f(path));
        }
        None
    }
}