use rune::{Image, ImageCache, Waker};
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

pub struct Images {
    cache: ImageCache,
    held: RefCell<HashMap<PathBuf, Image>>,
}

impl Images {
    pub fn new(waker: Waker) -> Self {
        Self {
            cache: ImageCache::new(waker),
            held: RefCell::default(),
        }
    }

    pub fn file(&self, path: &Path) -> Image {
        self.held
            .borrow_mut()
            .entry(path.to_path_buf())
            .or_insert_with(|| self.cache.file(path, 560.0, 2.0))
            .clone()
    }
}
