pub trait Messenger {
    fn send(&self, msg: &str);
}

pub struct LimitTracker<'a, T: Messenger> {
    messenger: &'a T,
    value: usize,
    max: usize,
}

impl<'a, T> LimitTracker<'a, T>
where
    T: Messenger,
{
    pub fn new(messenger: &'a T, max: usize) -> LimitTracker<'a, T> {
        LimitTracker {
            messenger,
            value: 0,
            max,
        }
    }

    pub fn set_value(&mut self, value: usize) {
        self.value = value;

        let percentage_of_max = self.value as f64 / self.max as f64;

        if percentage_of_max >= 1.0 {
            self.messenger.send("Błąd: Twoja kwota została wykorzystana!");
        } else if percentage_of_max >= 0.9 {
            self.messenger
                .send("Plilne ostrzeżenie: wykorzystano ponad 90% Twojej kwoty!");
        } else if percentage_of_max >= 0.75 {
            self.messenger
                .send("Ostrzeżenie: wykorzystano pona 75% Twojej kwoty!");
        }
    }
}

// ANCHOR: here
#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::RefCell;

    struct MockMessenger {
        sent_messages: RefCell<Vec<String>>,
    }

    impl MockMessenger {
        fn new() -> MockMessenger {
            MockMessenger {
                sent_messages: RefCell::new(vec![]),
            }
        }
    }

    impl Messenger for MockMessenger {
        fn send(&self, message: &str) {
            self.sent_messages.borrow_mut().push(String::from(message));
        }
    }

    #[test]
    fn it_sends_an_over_75_percent_warning_message() {
        // --fragment pominięty--
        // ANCHOR_END: here
        let mock_messenger = MockMessenger::new();
        let mut limit_tracker = LimitTracker::new(&mock_messenger, 100);

        limit_tracker.set_value(80);
        // ANCHOR: here

        // ANCHOR: here
        assert_eq!(mock_messenger.sent_messages.borrow().len(), 1);
    }
}
// ANCHOR_END: here
