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
/* This file is part of DarkFi (https://dark.fi)
 *
 * Copyright (C) 2020-2023 Dyne.org foundation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use std::{cmp::Ordering, collections::HashMap, fmt::Write, str::FromStr};

use prettytable::{
    format::{consts::FORMAT_NO_COLSEP, FormatBuilder, LinePosition, LineSeparator},
    row, table, Cell, Row, Table,
};
use textwrap::fill;

use darkfi::{
    util::time::{timestamp_to_date, DateFormat},
    Result,
};

use crate::{
    primitives::{State, TaskInfo},
    TaskEvent,
};

pub fn print_task_list(tasks_map: HashMap<usize, TaskInfo>, ws: String) -> Result<()> {
    let mut tasks = tasks_map.clone().into_values().collect::<Vec<TaskInfo>>();

    let mut table = Table::new();
    table.set_format(
        FormatBuilder::new()
            .padding(1, 1)
            .separators(&[LinePosition::Title], LineSeparator::new('-', ' ', ' ', ' '))
            .build(),
    );
    table.set_titles(row!["ID", "Title", "Tags", "Project", "Assigned", "Due", "Rank"]);

    // group tasks by state.
    tasks.sort_by_key(|task| task.state.clone());

    // sort tasks by there rank only if they are not stopped.
    tasks.sort_by(|a, b| {
        if a.state != "stop" && b.state != "stop" {
            b.rank.partial_cmp(&a.rank).unwrap()
        } else {
            // because sort_by does not reorder equal elements
            Ordering::Equal
        }
    });

    let mut min_rank = None;
    let mut max_rank = None;

    if let Some(first) = tasks.first() {
        max_rank = first.rank;
    }

    if let Some(last) = tasks.last() {
        min_rank = last.rank;
    }

    for (task_id, task) in tasks_map {
        let state = State::from_str(&task.state.clone())?;

        let (max_style, min_style, mid_style, gen_style) = if state.is_start() {
            ("bFg", "Fc", "Fg", "Fg")
        } else if state.is_pause() {
            ("iFYBd", "iFYBd", "iFYBd", "iFYBd")
        } else if state.is_stop() {
            ("Fr", "Fr", "Fr", "Fr")
        } else {
            ("", "", "", "")
        };

        let rank = if let Some(r) = task.rank { r.to_string() } else { "".to_string() };
        let mut print_tags = vec![];
        for tag in &task.tags {
            let t = tag.replace('+', "");
            print_tags.push(t)
        }

        let due_ = match task.due {
            Some(ts) => ts.0,
            None => 0,
        };

        table.add_row(Row::new(vec![
            Cell::new(&task_id.to_string()).style_spec(gen_style),
            Cell::new(&task.title).style_spec(gen_style),
            Cell::new(&print_tags.join(", ")).style_spec(gen_style),
            Cell::new(&task.project.join(", ")).style_spec(gen_style),
            Cell::new(&task.assign.join(", ")).style_spec(gen_style),
            Cell::new(&timestamp_to_date(due_, DateFormat::Date)).style_spec(gen_style),
            if task.rank == max_rank {
                Cell::new(&rank).style_spec(max_style)
            } else if task.rank == min_rank {
                Cell::new(&rank).style_spec(min_style)
            } else {
                Cell::new(&rank).style_spec(mid_style)
            },
        ]));
    }

    let workspace = format!("Workspace: {}", ws);
    let mut ws_table = table!([workspace]);
    ws_table.set_format(
        FormatBuilder::new()
            .padding(1, 1)
            .separators(&[LinePosition::Bottom], LineSeparator::new('-', ' ', ' ', ' '))
            .build(),
    );

    if unsafe { libc::isatty(libc::STDOUT_FILENO) } == 1 {
        ws_table.printstd();
        table.printstd();
    } else {
        for row in table.row_iter() {
            for cell in row.iter() {
                print!("{}\t", cell.get_content());
            }
            println!();
        }
    }

    Ok(())
}

pub fn taskinfo_table(id: usize, taskinfo: TaskInfo) -> Result<Table> {
    let due_ = match taskinfo.due {
        Some(ts) => ts.0,
        None => 0,
    };

    let due = timestamp_to_date(due_, DateFormat::Date);
    let created_at = timestamp_to_date(taskinfo.created_at.0, DateFormat::DateTime);
    let rank = if let Some(r) = taskinfo.rank { r.to_string() } else { "".to_string() };

    let mut table = table!(
         [Bd => "ref_id", &taskinfo.ref_id],
         ["workspace", &taskinfo.workspace],
         [Bd =>"id", &id.to_string()],
         ["owner", &taskinfo.owner],
         [Bd =>"title", &taskinfo.title],
         ["tags", &taskinfo.tags.join(", ")],
         [Bd =>"desc", &taskinfo.desc.to_string()],
         ["assign", taskinfo.assign.join(", ")],
         [Bd =>"project", taskinfo.project.join(", ")],
         ["due", due],
         [Bd =>"rank", rank],
         ["created_at", created_at],
         [Bd =>"current_state", &taskinfo.state]);

    table.set_format(
        FormatBuilder::new()
            .padding(1, 1)
            .separators(&[LinePosition::Title], LineSeparator::new('-', ' ', ' ', ' '))
            .build(),
    );

    table.set_titles(row!["Name", "Value"]);
    Ok(table)
}

pub fn events_table(taskinfo: TaskInfo) -> Result<Table> {
    let (events, timestamps) = &events_as_string(taskinfo.events);
    let mut events_table = table!([events, timestamps]);
    events_table.set_format(*FORMAT_NO_COLSEP);
    events_table.set_titles(row!["Events"]);
    Ok(events_table)
}

pub fn comments_table(taskinfo: TaskInfo) -> Result<Table> {
    let (events, timestamps) = &comments_as_string(taskinfo.events);
    let mut comments_table = table!([events, timestamps]);
    comments_table.set_format(*FORMAT_NO_COLSEP);
    comments_table.set_titles(row!["Comments"]);

    Ok(comments_table)
}

pub fn print_task_info(id: usize, taskinfo: TaskInfo) -> Result<()> {
    let table = taskinfo_table(id, taskinfo.clone())?;
    table.printstd();

    let events_table = events_table(taskinfo.clone())?;
    events_table.printstd();

    let comments_table = comments_table(taskinfo)?;
    comments_table.printstd();

    println!();

    Ok(())
}

pub fn events_as_string(events: Vec<TaskEvent>) -> (String, String) {
    let mut events_str = String::new();
    let mut timestamps_str = String::new();
    let width = 50;
    for event in events {
        if event.action.as_str() == "comment" {
            continue
        }
        writeln!(timestamps_str, "{}", event.timestamp).unwrap();
        match event.action.as_str() {
            "title" => {
                writeln!(events_str, "- {} changed title to {}", event.author, event.content)
                    .unwrap();
            }
            "rank" => {
                writeln!(events_str, "- {} changed rank to {}", event.author, event.content)
                    .unwrap();
            }
            "state" => {
                writeln!(events_str, "- {} changed state to {}", event.author, event.content)
                    .unwrap();
            }
            "assign" => {
                writeln!(events_str, "- {} assigned {}", event.author, event.content).unwrap();
            }
            "project" => {
                writeln!(events_str, "- {} changed project to {}", event.author, event.content)
                    .unwrap();
            }
            "tags" => {
                writeln!(events_str, "- {} changed tags to {}", event.author, event.content)
                    .unwrap();
            }
            "due" => {
                writeln!(
                    events_str,
                    "- {} changed due date to {}",
                    event.author,
                    timestamp_to_date(event.content.parse::<u64>().unwrap_or(0), DateFormat::Date)
                )
                .unwrap();
            }
            "desc" => {
                // wrap long description
                let ev_content =
                    fill(&event.content, textwrap::Options::new(width).subsequent_indent("  "));
                // skip wrapped lines to align timestamp with the first line
                for _ in 1..ev_content.lines().count() {
                    writeln!(timestamps_str, " ").unwrap();
                }
                writeln!(events_str, "- {} changed description to: {}", event.author, ev_content)
                    .unwrap();
            }
            _ => {}
        }
    }
    (events_str, timestamps_str)
}

pub fn comments_as_string(events: Vec<TaskEvent>) -> (String, String) {
    let mut events_str = String::new();
    let mut timestamps_str = String::new();
    let width = 50;
    for event in events {
        if event.action.as_str() != "comment" {
            continue
        }
        writeln!(timestamps_str, "{}", event.timestamp).unwrap();
        if event.action.as_str() == "comment" {
            // wrap long comments
            let ev_content =
                fill(&event.content, textwrap::Options::new(width).subsequent_indent("    "));
            // skip wrapped lines to align timestamp with the first line
            for _ in 1..ev_content.lines().count() {
                writeln!(timestamps_str, " ").unwrap();
            }
            writeln!(events_str, "{}>  {}", event.author, ev_content).unwrap();
        }
    }
    (events_str, timestamps_str)
}

pub fn find_free_id(task_ids: &[u32]) -> u32 {
    for i in 1.. {
        if !task_ids.contains(&i) {
            return i
        }
    }
    1
}