use super::commands::shared_commands::EmptyCommand;
use super::commands::IStateCommand;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Mutex;
pub struct StateManager {
changed_states: HashMap<u16, Rc<Mutex<Box<IStateCommand>>>>,
}
impl StateManager {
pub fn new() -> StateManager {
StateManager {
changed_states: HashMap::new(),
}
}
pub fn restore_changes(&mut self) {
for (id, item) in self.changed_states.iter_mut() {
let mut item = item.lock().unwrap();
item.undo();
}
}
pub fn register_change(&mut self, change: Box<IStateCommand>, key: u16) {
self.changed_states.insert(key, Rc::new(Mutex::new(change)));
}
pub fn get(&mut self, state_key: u16) -> Rc<Mutex<Box<IStateCommand>>> {
if self.changed_states.contains_key(&state_key) {
return self.changed_states[&state_key].clone();
}
return Rc::new(Mutex::new(Box::new(EmptyCommand)));
}
pub fn get_changes_count(&self) -> u16 {
return self.changed_states.len() as u16;
}
}