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
use geom::Duration;
use map_gui::tools::{
    DrawRoadLabels, InputWaypoints, TripManagement, TripManagementState, WaypointID,
};
use map_model::{PathV2, PathfinderCache};
use synthpop::{TripEndpoint, TripMode};
use widgetry::mapspace::{ToggleZoomed, World};
use widgetry::{
    ButtonBuilder, Color, ControlState, EventCtx, GeomBatch, GfxCtx, Key, Line, Outcome, Panel,
    RoundedF64, Spinner, State, Text, Widget,
};

use crate::{colors, App, BrowseNeighbourhoods, Transition};

pub struct RoutePlanner {
    top_panel: Panel,
    left_panel: Panel,
    waypoints: InputWaypoints,
    files: TripManagement<App, RoutePlanner>,
    world: World<WaypointID>,
    draw_routes: ToggleZoomed,
    labels: DrawRoadLabels,
    // TODO We could save the no-filter variations map-wide
    pathfinder_cache: PathfinderCache,
}

impl TripManagementState<App> for RoutePlanner {
    fn mut_files(&mut self) -> &mut TripManagement<App, Self> {
        &mut self.files
    }

    fn app_session_current_trip_name(app: &mut App) -> &mut Option<String> {
        &mut app.session.current_trip_name
    }

    fn sync_from_file_management(&mut self, ctx: &mut EventCtx, app: &mut App) {
        self.waypoints
            .overwrite(app, self.files.current.waypoints.clone());
        self.update_everything(ctx, app);
    }
}

impl RoutePlanner {
    pub fn new_state(ctx: &mut EventCtx, app: &mut App) -> Box<dyn State<App>> {
        let mut rp = RoutePlanner {
            top_panel: crate::components::TopPanel::panel(ctx, app),
            left_panel: Panel::empty(ctx),
            waypoints: InputWaypoints::new_max_2(app),
            files: TripManagement::new(app),
            world: World::unbounded(),
            draw_routes: ToggleZoomed::empty(ctx),
            labels: DrawRoadLabels::only_major_roads(),
            pathfinder_cache: PathfinderCache::new(),
        };

        if let Some(current_name) = &app.session.current_trip_name {
            rp.files.set_current(current_name);
        }
        rp.sync_from_file_management(ctx, app);

        Box::new(rp)
    }

    pub fn button(ctx: &EventCtx) -> Widget {
        ctx.style()
            .btn_outline
            .text("Plan a route")
            .hotkey(Key::R)
            .build_def(ctx)
    }

    // Updates the panel and draw_routes
    fn update_everything(&mut self, ctx: &mut EventCtx, app: &mut App) {
        self.files.autosave(app);
        let results_widget = self.recalculate_paths(ctx, app);

        let contents = Widget::col(vec![
            app.session.alt_proposals.to_widget(ctx, app),
            BrowseNeighbourhoods::button(ctx, app),
            ctx.style()
                .btn_back("Analyze neighbourhood")
                .hotkey(Key::Escape)
                .build_def(ctx)
                .hide(app.session.consultation.is_none()),
            Line("Plan a route").small_heading().into_widget(ctx),
            Widget::col(vec![
                self.files.get_panel_widget(ctx),
                Widget::horiz_separator(ctx, 1.0),
                self.waypoints.get_panel_widget(ctx),
            ])
            .section(ctx),
            Widget::col(vec![
                Widget::row(vec![
                    Line("Slow-down factor for main roads:")
                        .into_widget(ctx)
                        .centered_vert(),
                    Spinner::f64_widget(
                        ctx,
                        "main road penalty",
                        (1.0, 10.0),
                        app.session.main_road_penalty,
                        0.5,
                    ),
                ]),
                Text::from_multiline(vec![
                    Line("1 means free-flow traffic conditions").secondary(),
                    Line("Increase to see how drivers may try to detour in heavy traffic")
                        .secondary(),
                ])
                .into_widget(ctx),
            ])
            .section(ctx),
            results_widget.section(ctx),
        ]);
        let mut panel = crate::components::LeftPanel::builder(ctx, &self.top_panel, contents)
            // Hovering on waypoint cards
            .ignore_initial_events()
            .build(ctx);
        panel.restore(ctx, &self.left_panel);
        self.left_panel = panel;

        // Fade all neighbourhood interiors, so it's very clear when a route cuts through
        let mut batch = GeomBatch::new();
        for info in app.session.partitioning.all_neighbourhoods().values() {
            batch.push(app.cs.fade_map_dark, info.block.polygon.clone());
        }

        let mut world = World::bounded(app.map.get_bounds());
        world.draw_master_batch(ctx, batch);
        self.waypoints.rebuild_world(ctx, &mut world, |x| x, 0);
        world.initialize_hover(ctx);
        world.rebuilt_during_drag(&self.world);
        self.world = world;
    }

    // Returns a widget to display
    fn recalculate_paths(&mut self, ctx: &mut EventCtx, app: &App) -> Widget {
        let map = &app.map;

        let mut paths: Vec<(PathV2, Color)> = Vec::new();

        // The baseline is the one ignoring ALL filters (pre-existing and new)
        let baseline_time = {
            let mut total_time = Duration::ZERO;
            let mut params = map.routing_params().clone();
            params.main_road_penalty = app.session.main_road_penalty;

            for pair in self.waypoints.get_waypoints().windows(2) {
                if let Some(path) = TripEndpoint::path_req(pair[0], pair[1], TripMode::Drive, map)
                    .and_then(|req| {
                        self.pathfinder_cache
                            .pathfind_with_params(map, req, params.clone())
                    })
                {
                    total_time += path.get_cost();
                    paths.push((path, *colors::PLAN_ROUTE_BEFORE));
                }
            }

            total_time
        };

        // The route respecting the filters
        let drive_around_filters_time = {
            let mut params = map.routing_params().clone();
            app.session.modal_filters.update_routing_params(&mut params);
            params.main_road_penalty = app.session.main_road_penalty;

            let mut total_time = Duration::ZERO;
            let mut paths_after = Vec::new();
            for pair in self.waypoints.get_waypoints().windows(2) {
                if let Some(path) = TripEndpoint::path_req(pair[0], pair[1], TripMode::Drive, map)
                    .and_then(|req| {
                        self.pathfinder_cache
                            .pathfind_with_params(map, req, params.clone())
                    })
                {
                    total_time += path.get_cost();
                    paths_after.push((path, *colors::PLAN_ROUTE_AFTER));
                }
            }
            // To simplify colors, don't draw this path when it's the same as the baseline
            if total_time != baseline_time {
                paths.append(&mut paths_after);
            }

            total_time
        };

        let biking_time = {
            // No custom params, but don't use the map's built-in bike CH. Changes to one-way
            // streets haven't been reflected, and it's cheap enough to use Dijkstra's for
            // calculating one path at a time anyway.
            let mut total_time = Duration::ZERO;
            for pair in self.waypoints.get_waypoints().windows(2) {
                if let Some(path) = TripEndpoint::path_req(pair[0], pair[1], TripMode::Bike, map)
                    .and_then(|req| {
                        self.pathfinder_cache.pathfind_with_params(
                            map,
                            req,
                            map.routing_params().clone(),
                        )
                    })
                {
                    total_time += path.get_cost();
                    paths.push((path, *colors::PLAN_ROUTE_BIKE));
                }
            }
            total_time
        };

        let walking_time = {
            // Same as above -- don't use the built-in CH.
            let mut total_time = Duration::ZERO;
            for pair in self.waypoints.get_waypoints().windows(2) {
                if let Some(path) = TripEndpoint::path_req(pair[0], pair[1], TripMode::Walk, map)
                    .and_then(|req| {
                        self.pathfinder_cache.pathfind_with_params(
                            map,
                            req,
                            map.routing_params().clone(),
                        )
                    })
                {
                    total_time += path.get_cost();
                    paths.push((path, *colors::PLAN_ROUTE_WALK));
                }
            }
            total_time
        };

        self.draw_routes = map_gui::tools::draw_overlapping_paths(ctx, app, paths);

        Widget::col(vec![
            Widget::row(vec![
                card(
                    ctx,
                    "Driving without any filters",
                    "This route drives through pre-existing and new filters",
                    baseline_time,
                    *colors::PLAN_ROUTE_BEFORE,
                ),
                if baseline_time == drive_around_filters_time {
                    Widget::col(vec![
                        Line("Driving around filters")
                            .fg(colors::PLAN_ROUTE_BEFORE.invert())
                            .into_widget(ctx),
                        Line("No difference")
                            .fg(colors::PLAN_ROUTE_BEFORE.invert())
                            .into_widget(ctx),
                    ])
                    .bg(*colors::PLAN_ROUTE_BEFORE)
                    .padding(16)
                } else {
                    card(
                        ctx,
                        "Driving around filters",
                        "This route drives around all filters",
                        drive_around_filters_time,
                        *colors::PLAN_ROUTE_AFTER,
                    )
                },
            ])
            .evenly_spaced(),
            Widget::row(vec![
                card(ctx, "Biking", "This cycling route doesn't avoid high-stress roads or hills, and assumes an average 10mph pace", biking_time, *colors::PLAN_ROUTE_BIKE),
                card(ctx, "Walking", "This walking route doesn't avoid high-stress roads or hills, and assumes an average 3 mph pace", walking_time, *colors::PLAN_ROUTE_WALK),
            ])
            .evenly_spaced(),
        ])
    }
}

impl State<App> for RoutePlanner {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        if let Some(t) = crate::components::TopPanel::event(ctx, app, &mut self.top_panel, help) {
            return t;
        }

        let panel_outcome = self.left_panel.event(ctx);
        if let Outcome::Clicked(ref x) = panel_outcome {
            if x == "Browse neighbourhoods" {
                return Transition::Replace(BrowseNeighbourhoods::new_state(ctx, app));
            }
            if x == "Analyze neighbourhood" {
                return Transition::Pop;
            }
            if let Some(t) = self.files.on_click(ctx, app, x) {
                // Bit hacky...
                if matches!(t, Transition::Keep) {
                    self.sync_from_file_management(ctx, app);
                }
                return t;
            }
            if let Some(t) = crate::save::AltProposals::handle_action(
                ctx,
                app,
                crate::save::PreserveState::Route,
                x,
            ) {
                return t;
            }
        }

        if let Outcome::Changed(ref x) = panel_outcome {
            if x == "main road penalty" {
                app.session.main_road_penalty =
                    self.left_panel.spinner::<RoundedF64>("main road penalty").0;
                self.update_everything(ctx, app);
            }
        }

        if self
            .waypoints
            .event(app, panel_outcome, self.world.event(ctx))
        {
            // Sync from waypoints to file management
            // TODO Maaaybe this directly live in the InputWaypoints system?
            self.files.current.waypoints = self.waypoints.get_waypoints();
            self.update_everything(ctx, app);
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.top_panel.draw(g);
        self.left_panel.draw(g);

        self.world.draw(g);
        self.draw_routes.draw(g);
        app.session.draw_all_filters.draw(g);
        if g.canvas.is_unzoomed() {
            self.labels.draw(g, app);
        }
    }
}

fn help() -> Vec<&'static str> {
    vec![
        "You can test how different driving routes are affected by proposed LTNs.",
        "",
        "The fastest route may not cut through neighbourhoods normally,",
        "but you can adjust the slow-down factor to mimic rush hour conditions",
    ]
}

fn card(
    ctx: &EventCtx,
    label: &'static str,
    tooltip: &'static str,
    time: Duration,
    color: Color,
) -> Widget {
    // TODO Convoluted way to add tooltips to text with a background
    let mut txt = Text::new();
    txt.add_line(Line(label).fg(color.invert()));
    txt.add_line(Line(time.to_rounded_string(0)).fg(color.invert()));
    let (batch, _) = txt
        .render_autocropped(ctx)
        .batch()
        .container()
        .bg(color)
        .padding(16)
        .into_geom(ctx, None);
    ButtonBuilder::new()
        .custom_batch(batch, ControlState::Default)
        .disabled(true)
        .disabled_tooltip(tooltip)
        .build_widget(ctx, label)
}