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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
use crate::factorio::util::format_dotgraph;
use crate::gantt_mermaid::MermaidGanttBuilder;
use crate::num_traits::FromPrimitive;
use crate::types::{Direction, FactorioEntity, PlayerId, Position};
use noisy_float::types::{r64, R64};
use num_traits::ToPrimitive;
use parking_lot::RwLock;
use petgraph::algo::astar;
use petgraph::dot::{Config, Dot};
use petgraph::graph::{DefaultIx, EdgeIndex, NodeIndex};
use petgraph::stable_graph::{Edges, NodeIndices, StableGraph};
use petgraph::visit::EdgeRef;
use petgraph::Directed;
use ptree::graph::print_graph;
use serde::__private::Formatter;
use std::collections::HashMap;
use std::sync::Arc;

#[derive(Clone)]
pub struct TaskGraph {
    inner: TaskGraphInner,
    pub start_node: NodeIndex,
    pub end_node: NodeIndex,
    pub cursor: NodeIndex,
    groups: Vec<HashMap<PlayerId, NodeIndex>>,
}

impl TaskGraph {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        let mut graph = TaskGraphInner::new();
        let start_node = graph.add_node(TaskNode::new(None, "Process Start", None, 0.));
        let end_node = graph.add_node(TaskNode::new(None, "Process End", None, 0.));
        let cursor = start_node;
        graph.add_edge(start_node, end_node, 0.);
        TaskGraph {
            inner: graph,
            start_node,
            end_node,
            cursor,
            groups: Vec::new(),
        }
    }

    fn add_to_cursor(&mut self, node: NodeIndex) {
        if let Some(edge) = self.inner.find_edge(self.cursor, self.end_node) {
            self.inner.remove_edge(edge);
        }
        self.inner.add_edge(self.cursor, node, 0.);
        self.cursor = node;
        self.inner.add_edge(self.cursor, self.end_node, 0.);
    }

    fn add_to_group(&mut self, player_id: PlayerId, node: NodeIndex, cost: f64) {
        let mut cursor = self.cursor;
        if let Some(group) = self.groups.last_mut() {
            if let Some(player_cursor) = group.get(&player_id) {
                cursor = *player_cursor
            }
            group.insert(player_id, node);
            self.inner.add_edge(cursor, node, cost);
        } else {
            panic!("no group to add to?");
            // self.inner.add_edge(self.cursor, node, 0.);
        }
    }

    pub fn group_start(&mut self, label: &str) {
        let group_start =
            self.inner
                .add_node(TaskNode::new(None, &format!("Start: {}", label), None, 0.));
        self.add_to_cursor(group_start);
        self.groups.push(HashMap::new());
    }

    pub fn group_end(&mut self) {
        let group = self.groups.pop().expect("no open group");
        let group_end = self.inner.add_node(TaskNode::new(None, "End", None, 0.));
        if group.is_empty() {
            self.inner.add_edge(self.cursor, group_end, 0.);
        } else {
            let mut weights: HashMap<NodeIndex, R64> = HashMap::new();
            for (_, cursor) in group {
                weights.insert(cursor, self.weight(self.cursor, cursor));
            }
            let max_weight = *weights.values().max().unwrap();
            for (cursor, weight) in weights {
                self.inner
                    .add_edge(cursor, group_end, (max_weight - weight).to_f64().unwrap());
            }
        }
        if let Some(edge) = self.inner.find_edge(self.cursor, self.end_node) {
            self.inner.remove_edge(edge);
        }
        self.cursor = group_end;
        self.inner.add_edge(self.cursor, self.end_node, 0.);
    }

    pub fn add_mine_node(&mut self, player_id: PlayerId, cost: f64, target: MineTarget) {
        let node = self
            .inner
            .add_node(TaskNode::new_mine(player_id, target, cost));
        self.add_to_group(player_id, node, cost);
    }

    pub fn add_walk_node(&mut self, player_id: PlayerId, cost: f64, target: PositionRadius) {
        let node = self
            .inner
            .add_node(TaskNode::new_walk(player_id, target, cost));
        self.add_to_group(player_id, node, cost);
    }

    pub fn add_place_node(&mut self, player_id: PlayerId, cost: f64, entity: FactorioEntity) {
        let node = self
            .inner
            .add_node(TaskNode::new_place(player_id, entity, cost));
        self.add_to_group(player_id, node, cost);
    }

    pub fn node_weight(&self, idx: NodeIndex) -> Option<&TaskNode> {
        self.inner.node_weight(idx)
    }
    pub fn weight(&self, start: NodeIndex, goal: NodeIndex) -> R64 {
        let (weight, _) = self.astar(start, goal).expect("failed to find path");
        r64(weight)
    }

    pub fn node_indices(&self) -> NodeIndices<TaskNode, DefaultIx> {
        self.inner.node_indices()
    }

    pub fn shortest_path(&self) -> Option<f64> {
        let process_start = self.inner.node_indices().next().unwrap();
        let process_end = self.inner.node_indices().last().unwrap();
        self.shortest_path_between(process_start, process_end)
    }

    pub fn shortest_path_between(&self, start: NodeIndex, end: NodeIndex) -> Option<f64> {
        let (weight, _) = self.astar(start, end)?;
        Some(weight)
    }

    pub fn add_group_start_node(&mut self, parent: NodeIndex, label: &str) -> NodeIndex {
        let start =
            self.inner
                .add_node(TaskNode::new(None, &format!("Start: {}", label), None, 0.));
        self.inner.add_edge(parent, start, 0.);

        start
    }
    pub fn print(&self) {
        print_graph(&self.inner.clone().into(), self.start_node).unwrap();
    }
    pub fn graphviz_dot(&self) -> String {
        format_dotgraph(Dot::with_config(&self.inner, &[Config::GraphContentOnly]).to_string())
    }

    pub fn mermaid_gantt(&self, bot_ids: Vec<PlayerId>, title: &str) -> String {
        let mut builder = MermaidGanttBuilder::new(title);
        let total_runtime = self.shortest_path().expect("no path found");

        builder = builder.add_milestone("test", "m1", &duration_to_timestamp(total_runtime), 0.);
        // let milestone_by_index: HashMap<NodeIndex, String> = HashMap::new();

        for player_id in bot_ids {
            builder = builder.add_section(&format!("Bot {}", player_id));
            let mut cursor = self.start_node;
            let mut last_weight: Option<f64> = None;

            while cursor != self.end_node {
                let node = self
                    .node_weight(cursor)
                    .expect("NodeIndices should all be valid");

                let status = node.status.read();
                match *status {
                    TaskStatus::Planned(estimated) => {
                        builder = builder.add_action(
                            &node.name,
                            if estimated > 0. {
                                estimated
                            } else {
                                last_weight.unwrap_or(0.)
                            },
                            if cursor == self.start_node {
                                Some("00:00:00")
                            } else {
                                None
                            },
                        );
                    }
                    TaskStatus::Success(estimated, _realtime) => {
                        builder = builder.add_action(
                            &node.name,
                            if estimated > 0. {
                                estimated
                            } else {
                                last_weight.unwrap_or(0.)
                            },
                            if cursor == self.start_node {
                                Some("00:00:00")
                            } else {
                                None
                            },
                        );
                    }
                    _ => {}
                };

                let cursor_copy = cursor;
                for edge in self.edges_directed(cursor, petgraph::Direction::Outgoing) {
                    let target_idx = edge.target();
                    last_weight = Some(*edge.weight());
                    let target = self
                        .node_weight(target_idx)
                        .expect("NodeIndices should all be valid");
                    if target.player_id.is_none() || target.player_id.unwrap() == player_id {
                        cursor = target_idx;
                    }
                }
                if cursor == cursor_copy {
                    error!("no change in cursor!?");
                    break;
                }
            }
        }

        builder.build()
    }

    pub fn add_node(&mut self, task: TaskNode) -> NodeIndex {
        self.inner.add_node(task)
    }

    pub fn add_edge(&mut self, a: NodeIndex, b: NodeIndex, weight: f64) -> EdgeIndex {
        self.inner.add_edge(a, b, weight)
    }

    pub fn astar(&self, start: NodeIndex, goal: NodeIndex) -> Option<(f64, Vec<NodeIndex>)> {
        astar(
            &self.inner,
            start,
            |finish| finish == goal,
            |e| *e.weight(),
            |_| 0.,
        )
    }

    pub fn edges_directed(
        &self,
        i: NodeIndex,
        dir: petgraph::Direction,
    ) -> Edges<f64, Directed, DefaultIx> {
        self.inner.edges_directed(i, dir)
    }
}

#[derive(Debug, Clone)]
pub struct InventoryItem {
    pub name: String,
    pub count: u32,
}

impl InventoryItem {
    pub fn new(name: &str, count: u32) -> InventoryItem {
        InventoryItem {
            name: name.into(),
            count,
        }
    }
}

#[derive(Debug, Clone)]
pub struct InventoryLocation {
    pub entity_name: String,
    pub position: Position,
    pub inventory_type: u32,
}

#[derive(Debug, Clone)]
pub struct EntityPlacement {
    pub item_name: String,
    pub position: Position,
    pub direction: Direction,
}

#[derive(Debug, Clone)]
pub struct PositionRadius {
    pub position: Position,
    pub radius: f64,
}
impl PositionRadius {
    pub fn new(x: f64, y: f64, radius: f64) -> PositionRadius {
        PositionRadius {
            position: Position::new(x, y),
            radius,
        }
    }
    pub fn from_position(pos: &Position, radius: f64) -> PositionRadius {
        PositionRadius {
            position: pos.clone(),
            radius,
        }
    }
}

#[derive(Debug, Clone)]
pub struct MineTarget {
    pub position: Position,
    pub name: String,
    pub count: u32,
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum TaskData {
    Mine(MineTarget),
    Walk(PositionRadius),
    Craft(InventoryItem),
    InsertToInventory(InventoryLocation, InventoryItem),
    RemoveFromInventory(InventoryLocation, InventoryItem),
    PlaceEntity(FactorioEntity),
}

pub enum TaskStatus {
    Planned(f64),
    Running(u32, u32),
    Success(f64, u32),
    Failed(u32, u32, String),
}

#[derive(Clone)]
pub struct TaskNode {
    pub name: String,
    pub player_id: Option<PlayerId>,
    pub data: Option<TaskData>,
    pub status: Arc<RwLock<TaskStatus>>,
}

impl TaskNode {
    pub fn new(
        player_id: Option<PlayerId>,
        name: &str,
        data: Option<TaskData>,
        cost: f64,
    ) -> TaskNode {
        TaskNode {
            name: name.into(),
            player_id,
            data,
            status: Arc::new(RwLock::new(TaskStatus::Planned(cost))),
        }
    }
    pub fn new_craft(player_id: PlayerId, item: InventoryItem, cost: f64) -> TaskNode {
        TaskNode::new(
            Some(player_id),
            &*format!(
                "Craft {}{}",
                item.name,
                if item.count > 1 {
                    format!(" x {}", item.count)
                } else {
                    String::new()
                }
            ),
            Some(TaskData::Craft(item)),
            cost,
        )
    }
    pub fn new_walk(player_id: PlayerId, target: PositionRadius, cost: f64) -> TaskNode {
        TaskNode::new(
            Some(player_id),
            &*format!("Walk to {}", target.position),
            Some(TaskData::Walk(target)),
            cost,
        )
    }
    pub fn new_mine(player_id: PlayerId, target: MineTarget, cost: f64) -> TaskNode {
        TaskNode::new(
            Some(player_id),
            &*format!(
                "Mining {}{}",
                target.name,
                if target.count > 1 {
                    format!(" x {}", target.count)
                } else {
                    String::new()
                }
            ),
            Some(TaskData::Mine(target)),
            cost,
        )
    }
    pub fn new_place(player_id: PlayerId, entity: FactorioEntity, cost: f64) -> TaskNode {
        TaskNode::new(
            Some(player_id),
            &*format!(
                "Place {} at {} ({:?})",
                entity.name,
                entity.position,
                Direction::from_u8(entity.direction).unwrap()
            ),
            Some(TaskData::PlaceEntity(entity)),
            cost,
        )
    }
    pub fn new_insert_to_inventory(
        player_id: PlayerId,
        location: InventoryLocation,
        item: InventoryItem,
        cost: f64,
    ) -> TaskNode {
        TaskNode::new(
            Some(player_id),
            &*format!(
                "Insert {}x{} into {} at {}",
                &item.name, &item.count, location.entity_name, location.position
            ),
            Some(TaskData::InsertToInventory(location, item)),
            cost,
        )
    }
}

impl std::fmt::Display for TaskNode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.name)?;
        Ok(())
    }
}
impl std::fmt::Debug for TaskNode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.name)?;
        Ok(())
    }
}

pub struct TaskResult(i32);

pub type TaskGraphInner = StableGraph<TaskNode, f64>;

fn duration_to_timestamp(duration: f64) -> String {
    let duration = duration as u64;
    let seconds = duration % 60;
    let minutes = (duration / 60) % 60;
    let hours = (duration / 60 / 60) % 60;

    format!("{hours:02}:{minutes:02}:{seconds:02}")
}

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

    #[test]
    fn test_simple_group() {
        let mut task_graph = TaskGraph::new();
        task_graph.group_start("foo");
        task_graph.add_mine_node(
            1,
            3.,
            MineTarget {
                position: Position::default(),
                count: 1,
                name: "iron-ore".into(),
            },
        );
        task_graph.group_end();

        assert_eq!(
            task_graph.graphviz_dot(),
            r##"digraph {
    0 [ label = "Process Start" ]
    1 [ label = "Process End" ]
    2 [ label = "Start: foo" ]
    3 [ label = "Mining iron-ore" ]
    4 [ label = "End" ]
    0 -> 2 [ label = "0" ]
    2 -> 3 [ label = "3" ]
    3 -> 4 [ label = "0" ]
    4 -> 1 [ label = "0" ]
}
"##,
        );
    }

    #[test]
    fn test_diverging_group() {
        let mut task_graph = TaskGraph::new();
        task_graph.group_start("foo");
        task_graph.add_mine_node(
            1,
            3.,
            MineTarget {
                position: Position::default(),
                count: 1,
                name: "iron-ore".into(),
            },
        );
        task_graph.add_mine_node(
            1,
            3.,
            MineTarget {
                position: Position::default(),
                count: 1,
                name: "iron-ore".into(),
            },
        );
        task_graph.add_mine_node(
            2,
            3.,
            MineTarget {
                position: Position::default(),
                count: 1,
                name: "iron-ore".into(),
            },
        );
        task_graph.group_end();

        assert_eq!(
            task_graph.graphviz_dot(),
            r##"digraph {
    0 [ label = "Process Start" ]
    1 [ label = "Process End" ]
    2 [ label = "Start: foo" ]
    3 [ label = "Mining iron-ore" ]
    4 [ label = "Mining iron-ore" ]
    5 [ label = "Mining iron-ore" ]
    6 [ label = "End" ]
    0 -> 2 [ label = "0" ]
    2 -> 3 [ label = "3" ]
    2 -> 5 [ label = "3" ]
    3 -> 4 [ label = "3" ]
    4 -> 6 [ label = "0" ]
    5 -> 6 [ label = "3" ]
    6 -> 1 [ label = "0" ]
}
"##,
        );
    }
}