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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/// The Report struct is used by the StatsController to structure the report printed at the end of execution
///
/// Report contains several StatSummary structs that are used to generate the report table
use owo_colors::OwoColorize;
use tabled::{
    format::Format,
    object::{Columns, Rows},
    Alignment, Modify, Panel, Table, Tabled,
};
/// Describes the columns of the report table
#[derive(Tabled)]
pub struct StatSummary {
    pub statistic: String,
    pub value: String,
    pub notes: String,
}

impl StatSummary {
    pub fn new(statistic: String, value: String, notes: Option<String>) -> Self {
        Self {
            statistic,
            value,
            notes: if let Some(notes) = notes {
                notes
            } else {
                "".to_string()
            },
        }
    }
}
impl std::default::Default for StatSummary {
    fn default() -> Self {
        Self {
            statistic: "".to_string(),
            value: "".to_string(),
            notes: "".to_string(),
        }
    }
}
/// Describes the columns of the detected attributes table
#[derive(Tabled)]
struct DetectedAttribute {
    pub attribute: String,
    pub detected: String,
}

/// The Report struct is used by the StatsController to structure the report printed at the end of execution
///
/// Contains convenience methods to add stats to the report, and to generate the report table
pub struct Report {
    pub(crate) stats: Vec<StatSummary>,
    filter_stats_table: Option<Table>,
    detected_attributes: Vec<DetectedAttribute>,
    processing_time: std::time::Duration,
    fatal_error: Option<String>,
    report_table: Option<Table>,
}
impl Report {
    pub fn new(processing_time: std::time::Duration) -> Self {
        Self {
            stats: Vec::new(),
            detected_attributes: Vec::new(),
            processing_time,
            filter_stats_table: None,
            fatal_error: None,
            report_table: None,
        }
    }
    pub fn add_filter_stats(&mut self, filter_stats_table: Table) {
        self.filter_stats_table = Some(filter_stats_table);
    }
    pub fn add_stat(&mut self, stat: StatSummary) {
        self.stats.push(stat);
    }
    pub fn add_detected_attribute(&mut self, attribute: String, detected: String) {
        self.detected_attributes.push(DetectedAttribute {
            attribute,
            detected,
        });
    }
    pub fn add_fatal_error(&mut self, error: String) {
        self.fatal_error = Some(error);
    }
    pub fn print(&mut self) {
        let is_windows = std::env::consts::OS.to_lowercase().contains("windows"); // Windows can't have ANSI colors :(

        let mut global_stats_table = Table::new(&self.stats);
        format_global_stats_sub_table(&mut global_stats_table, is_windows);
        let mut detected_attributes_table = Table::new(&self.detected_attributes);
        detected_attributes_table = format_sub_table(
            &detected_attributes_table,
            "Detected Attributes".to_string(),
            if is_windows {
                SubtableColor::NoColor
            } else {
                SubtableColor::Yellow
            },
        );

        if self.filter_stats_table.is_some() {
            let filter_stats_table = format_sub_table(
                self.filter_stats_table.as_ref().unwrap(),
                "Filter Stats".to_string(),
                if is_windows {
                    SubtableColor::NoColor
                } else {
                    SubtableColor::Purple
                },
            );
            let multi_table = tabled::col![
                global_stats_table,
                tabled::row![detected_attributes_table, filter_stats_table]
            ];
            self.report_table = Some(format_super_table(
                &multi_table,
                self.processing_time,
                is_windows,
            ));
        } else {
            let multi_table =
                tabled::col![global_stats_table, tabled::row![detected_attributes_table]];
            self.report_table = Some(format_super_table(
                &multi_table,
                self.processing_time,
                is_windows,
            ));
        }
        if self.fatal_error.is_some() {
            let mut error_table = self.report_table.clone().unwrap();
            error_table
                .with(Panel::header("FATAL ERROR - EARLY TERMINATION"))
                .with(
                    Modify::new(Rows::single(0))
                        .with(Alignment::center())
                        .with(Format::new(|x| {
                            let x = x.to_uppercase();
                            x.red().to_string()
                        })),
                );
            self.report_table = Some(error_table);
        }
        println!("{}", self.report_table.as_ref().unwrap());
    }
}

/// The super table is the table that contains all the other tables
fn format_super_table(
    super_table: &Table,
    processing_time: std::time::Duration,
    is_os_windows: bool,
) -> Table {
    let mut modded_table = super_table.clone();
    let style = tabled::Style::modern()
        .horizontals([tabled::style::HorizontalLine::new(
            1,
            tabled::Style::modern().get_horizontal(),
        )
        .main(Some('═'))
        .intersection(None)])
        .verticals([tabled::style::VerticalLine::new(
            1,
            tabled::Style::modern().get_vertical(),
        )]);

    modded_table.with(style).with(Panel::header("Report")).with(
        Modify::new(Rows::single(0))
            .with(Alignment::center())
            .with(Format::new(|x| {
                let mut x = x.to_uppercase();
                if !is_os_windows {
                    x = x.green().to_string()
                }
                x
            })),
    );

    let height = modded_table.count_rows();
    modded_table
        .with(Panel::footer(format!("Processed in {processing_time:?}")))
        .with(
            Modify::new(Rows::single(height))
                .with(Alignment::center())
                .with(Format::new(|x| {
                    if is_os_windows {
                        x.to_string()
                    } else {
                        x.dimmed().to_string()
                    }
                })),
        );
    modded_table
}

fn format_global_stats_sub_table(global_stats_table: &mut Table, os_is_windows: bool) {
    let style = tabled::Style::modern()
        .off_left()
        .off_right()
        .off_top()
        .off_bottom()
        .off_vertical()
        .horizontals([tabled::style::HorizontalLine::new(
            1,
            tabled::Style::modern().get_horizontal(),
        )
        .main(Some('═'))
        .intersection(None)]);

    if os_is_windows {
        // Boring no colors
        global_stats_table
            .with(style)
            .with(Modify::new(Rows::single(0)).with(Format::new(|x| x.to_uppercase())))
            .with(Modify::new(Columns::new(2..)).with(Format::new(|s| s.to_string())))
            .with(Panel::header("Global Stats"))
            .with(
                Modify::new(Rows::single(0))
                    .with(Alignment::center())
                    .with(Format::new(|x| x.to_uppercase())),
            );
    } else {
        // Fun ANSI colors
        global_stats_table
            .with(style)
            .with(Modify::new(Rows::single(0)).with(Format::new(|x| x.to_uppercase())))
            .with(Modify::new(Columns::single(0)).with(Format::new(|s| s.blue().to_string())))
            .with(
                Modify::new(Columns::single(1)).with(Format::new(|s| s.bright_cyan().to_string())),
            )
            .with(Modify::new(Columns::new(2..)).with(Format::new(|s| s.yellow().to_string())))
            .with(Panel::header("Global Stats"))
            .with(
                Modify::new(Rows::single(0))
                    .with(Alignment::center())
                    .with(Format::new(|x| {
                        let x = x.to_uppercase();
                        x.bright_yellow().to_string()
                    })),
            );
    }
}

#[allow(dead_code)]
enum SubtableColor {
    Purple,
    Green,
    Blue,
    Yellow,
    Red,
    NoColor, // Windows...
}
/// Formats a subtable to use the same style as the main table
/// Adds a header to the subtable in all caps, purple, and aligned center
fn format_sub_table(subtable: &Table, header: String, color: SubtableColor) -> Table {
    let mut modded_subtable = subtable.clone();
    let style = tabled::Style::modern()
        .off_left()
        .off_right()
        .off_top()
        .off_bottom()
        .off_vertical()
        .horizontals([tabled::style::HorizontalLine::new(
            1,
            tabled::Style::modern().get_horizontal(),
        )
        .main(Some('═'))
        .intersection(None)]);
    modded_subtable.with(style);
    modded_subtable.with(Panel::header(header)).with(
        Modify::new(Rows::single(0))
            .with(Alignment::center())
            .with(Format::new(|x| {
                let x = x.to_uppercase();
                match color {
                    SubtableColor::Purple => x.bright_purple().to_string(),
                    SubtableColor::Green => x.green().to_string(),
                    SubtableColor::Blue => x.blue().to_string(),
                    SubtableColor::Yellow => x.yellow().to_string(),
                    SubtableColor::Red => x.red().to_string(),
                    SubtableColor::NoColor => x, // Colors are not available for Windows
                }
            })),
    );
    modded_subtable.with(
        Modify::new(Rows::single(1)).with(Format::new(|x| match color {
            SubtableColor::Purple => x.bright_purple().to_string(),
            SubtableColor::Green => x.green().to_string(),
            SubtableColor::Blue => x.blue().to_string(),
            SubtableColor::Yellow => x.yellow().to_string(),
            SubtableColor::Red => x.red().to_string(),
            SubtableColor::NoColor => x.to_string(), // Colors are not available for Windows
        })),
    );

    modded_subtable
}

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

    macro_rules! assert_stdout_contains {
        ($test:expr, $expected:literal) => {{
            use gag::BufferRedirect;
            use std::io::Read;

            let mut buf = BufferRedirect::stdout().unwrap();

            $test;

            let mut output = String::new();
            buf.read_to_string(&mut output).unwrap();
            drop(buf);

            assert!(output.contains($expected));
        }};
    }

    #[test]
    fn test_summary_contains_filtered_links_rdhs() {
        let processing_time = std::time::Instant::now();
        let mut report = Report::new(processing_time.elapsed());
        let statistic_tot_errs = "Total errors".to_string();
        report.add_stat(StatSummary::new(statistic_tot_errs, "0".to_string(), None));
        report.add_stat(StatSummary::new(
            "Total RDHs".to_string(),
            "725800".to_string(),
            None,
        ));
        let filtered_links = StatSummary::new(
            "Filtered links".to_string(),
            String::from("0"),
            Some(String::from("Not found: 2")),
        );
        let observed_links =
            StatSummary::new("Links observed".to_string(), "1 7 8 9".to_string(), None);

        let filter_table = Table::new(vec![filtered_links, observed_links]);
        report.add_filter_stats(filter_table);
        assert_stdout_contains!(report.print(), "Filtered links");
        assert_stdout_contains!(report.print(), "Total RDHs");
        assert_stdout_contains!(report.print(), "725800");
    }

    #[test]
    fn test_summary_contains_filtered_links_rdhs_windows() {
        let filtered_links = StatSummary::new(
            "Filtered links".to_string(),
            String::from("0"),
            Some(String::from("Not found: 2")),
        );
        let observed_links =
            StatSummary::new("Links observed".to_string(), "1 7 8 9".to_string(), None);

        let mut filter_table = Table::new(vec![filtered_links, observed_links]);
        println!("before:\n{filter_table}");
        format_global_stats_sub_table(&mut filter_table, true);
        println!("After:\n{filter_table}");
        assert_stdout_contains!(println!("{filter_table}"), "Filtered links");
    }

    #[test]
    fn test_fatal_error_report() {
        let processing_time = std::time::Instant::now();
        let fatal_error = "Fatal Error happened";
        let mut report = Report::new(processing_time.elapsed());
        report.add_fatal_error(fatal_error.to_string());

        assert_stdout_contains!(report.print(), "FATAL ERROR");
    }

    #[test]
    fn stats_summar_default() {
        let stats_summary = StatSummary::default();

        assert_eq!(stats_summary.statistic, "");
        assert_eq!(stats_summary.value, "");
        assert_eq!(stats_summary.notes, "");
    }

    #[test]
    fn test_os() {
        println!("{}", std::env::consts::OS);
    }
}