use crate::message::Message;

#[derive(Clone, Debug)]
struct HistoricItem {
    old: Message,
    new: Message,
    delta: f32,
}

#[derive(Debug)]
pub struct Historic {
    past: Vec<HistoricItem>,
    future: Vec<HistoricItem>,
    pub ignore_add: bool,
}

impl Historic {
    pub fn new() -> Self {
        Historic {
            past: vec![],
            future: vec![],
            ignore_add: false,
        }
    }

    pub fn add(&mut self, old: Message, new: Message, delta: f32) {
        if !self.ignore_add {
            self.future = vec![];
            self.past.push(HistoricItem {
                old: old,
                new: new,
                delta: delta,
            });
        }
    }
    pub fn undo(&mut self) -> Option<(Message, f32)> {
        if let Some(item) = self.past.pop() {
            self.future.push(item.clone());
            Some((item.old.clone(), item.delta))
        } else {
            None
        }
    }

    pub fn redo(&mut self) -> Option<(Message, f32)> {
        if let Some(item) = self.future.pop() {
            self.past.push(item.clone());
            Some((item.new.clone(), item.delta))
        } else {
            None
        }
    }
}
