use crate::prelude::*;

use crate::graphs::SimpleGraph;
use crate::triples::filters::TripleFilter;
use crate::triples::Triple;

// FIXME Is this really only for SimpleGraph?
pub trait TripleCallback: FnMut(Arc<Triple>, &SimpleGraph) {}
impl<F> TripleCallback for F where F: FnMut(Arc<Triple>, &SimpleGraph) {}

pub struct TripleAction {
    test: Box<dyn TripleFilter>,
    action: Box<dyn TripleCallback>,
}

impl TripleAction {
    pub fn new(
        test: Option<impl TripleFilter + 'static>,
        action: impl TripleCallback + 'static,
    ) -> Self {
        Self {
            test: match test {
                Some(t) => Box::new(t),
                None => Box::new(move |t: Arc<Triple>| true),
            },
            action: Box::new(action),
        }
    }

    pub fn run(&mut self, triple: Arc<Triple>, graph: &SimpleGraph) {
        if (*self.test)(triple.clone()) {
            (*self.action)(triple.clone(), graph);
        }
    }
}
