use crate::terminal::Terminal;
use std::collections::HashMap;

pub struct Pool {
    terminals: HashMap<String, Terminal>,
    counter: u64,
}

impl Pool {
    pub fn new() -> Self {
        Self {
            terminals: HashMap::new(),
            counter: 0,
        }
    }

    pub fn next_id(&mut self) -> String {
        self.counter += 1;
        format!("t{}", self.counter)
    }

    pub fn insert(&mut self, term: Terminal) {
        self.terminals.insert(term.id().to_string(), term);
    }

    pub fn get(&self, id: &str) -> Option<&Terminal> {
        self.terminals.get(id)
    }

    pub fn remove(&mut self, id: &str) -> Option<Terminal> {
        self.terminals.remove(id)
    }

    pub fn list(&self) -> Vec<&Terminal> {
        self.terminals.values().collect()
    }
}

#[cfg(test)]
mod tests {
    use super::Pool;

    #[test]
    fn next_id_sequence() {
        let mut pool = Pool::new();
        assert_eq!(pool.next_id(), "t1");
        assert_eq!(pool.next_id(), "t2");
        assert_eq!(pool.next_id(), "t3");
    }
}
