1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use chrono::NaiveDate;
use std::collections::HashSet;
use wildmatch::WildMatch;

use crate::data::activity;
use crate::data::activity::Activity;
use crate::data::bartib_file;

pub struct ActivityFilter<'a> {
    pub number_of_activities: Option<usize>,
    pub from_date: Option<NaiveDate>,
    pub to_date: Option<NaiveDate>,
    pub date: Option<NaiveDate>,
    pub project: Option<&'a str>,
}

#[must_use]
pub fn get_descriptions_and_projects(
    file_content: &[bartib_file::Line],
) -> Vec<(&String, &String)> {
    let mut activities: Vec<&activity::Activity> = get_activities(file_content).collect();
    get_descriptions_and_projects_from_activities(&mut activities)
}

fn get_descriptions_and_projects_from_activities<'a>(
    activities: &mut [&'a Activity],
) -> Vec<(&'a String, &'a String)> {
    activities.sort_by_key(|activity| activity.start);

    /* each activity should be placed in the list in the descending order of when it had been
       started last. To achieve this we reverse the order of the activities before we extract the
       set of descriptions and activities. Afterwards we also reverse the list of descriptions and
       activities.

       e.g. if tasks have been started in this order: a, b, c, a, c the list of descriptions and
           activities should have this order: b, a, c
    */
    activities.reverse();

    let mut known_descriptions_and_projects: HashSet<(&String, &String)> = HashSet::new();
    let mut descriptions_and_projects: Vec<(&String, &String)> = Vec::new();

    for description_and_project in activities.iter().map(|a| (&a.description, &a.project)) {
        if !known_descriptions_and_projects.contains(&description_and_project) {
            known_descriptions_and_projects.insert(description_and_project);
            descriptions_and_projects.push(description_and_project);
        }
    }

    descriptions_and_projects.reverse();
    descriptions_and_projects
}

#[must_use]
pub fn get_running_activities(file_content: &[bartib_file::Line]) -> Vec<&activity::Activity> {
    get_activities(file_content)
        .filter(|activity| !activity.is_stopped())
        .collect()
}

pub fn get_activities(
    file_content: &[bartib_file::Line],
) -> impl Iterator<Item = &activity::Activity> {
    file_content
        .iter()
        .filter_map(|line: &bartib_file::Line| match &line.activity {
            Ok(activity) => Some(activity),
            Err(_) => {
                println!(
                    "Warning: Ignoring line {}. Please see `bartib check` for further information",
                    line.line_number.unwrap_or(0),
                );
                None
            }
        })
}

pub fn filter_activities<'a>(
    activities: Vec<&'a activity::Activity>,
    filter: &'a ActivityFilter,
) -> Vec<&'a activity::Activity> {
    let from_date: NaiveDate;
    let to_date: NaiveDate;

    if let Some(date) = filter.date {
        from_date = date;
        to_date = date;
    } else {
        from_date = filter.from_date.unwrap_or(NaiveDate::MIN);
        to_date = filter.to_date.unwrap_or(NaiveDate::MAX);
    }

    activities
        .into_iter()
        .filter(move |activity| {
            activity.start.date() >= from_date && activity.start.date() <= to_date
        })
        .filter(move |activity| {
            filter
                .project
                .map_or(true, |p| WildMatch::new(p).matches(&activity.project))
        })
        .collect()
}

#[must_use]
pub fn get_last_activity_by_end(file_content: &[bartib_file::Line]) -> Option<&activity::Activity> {
    get_activities(file_content)
        .filter(|activity| activity.is_stopped())
        .max_by_key(|activity| {
            activity
                .end
                .unwrap_or_else(|| NaiveDate::MIN.and_hms_opt(0, 0, 0).unwrap())
        })
}

#[must_use]
pub fn get_last_activity_by_start(
    file_content: &[bartib_file::Line],
) -> Option<&activity::Activity> {
    get_activities(file_content).max_by_key(|activity| activity.start)
}

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

    #[test]
    fn get_descriptions_and_projects_test_simple() {
        let a1 = activity::Activity::start("p1".to_string(), "d1".to_string(), None);
        let a2 = activity::Activity::start("p1".to_string(), "d1".to_string(), None);
        let a3 = activity::Activity::start("p2".to_string(), "d1".to_string(), None);
        let mut activities = vec![&a1, &a2, &a3];

        let descriptions_and_projects =
            get_descriptions_and_projects_from_activities(&mut activities);

        assert_eq!(descriptions_and_projects.len(), 2);
        assert_eq!(
            *descriptions_and_projects.first().unwrap(),
            (&"d1".to_string(), &"p1".to_string())
        );
        assert_eq!(
            *descriptions_and_projects.get(1).unwrap(),
            (&"d1".to_string(), &"p2".to_string())
        );
    }

    #[test]
    fn get_descriptions_and_projects_test_restarted_activity() {
        let a1 = activity::Activity::start("p1".to_string(), "d1".to_string(), None);
        let a2 = activity::Activity::start("p2".to_string(), "d1".to_string(), None);
        let a3 = activity::Activity::start("p1".to_string(), "d1".to_string(), None);
        let mut activities = vec![&a1, &a2, &a3];

        let descriptions_and_projects =
            get_descriptions_and_projects_from_activities(&mut activities);

        assert_eq!(descriptions_and_projects.len(), 2);
        assert_eq!(
            *descriptions_and_projects.first().unwrap(),
            (&"d1".to_string(), &"p2".to_string())
        );
        assert_eq!(
            *descriptions_and_projects.get(1).unwrap(),
            (&"d1".to_string(), &"p1".to_string())
        );
    }
}