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
mod perma;

use anyhow::Result;
use serde::{Deserialize, Serialize};

use abstio::MapName;
use abstutil::Counter;
use map_gui::tools::{ChooseSomething, PromptInput};
use map_model::PathRequest;
use widgetry::tools::PopupMsg;
use widgetry::{Choice, EventCtx, Key, Line, State, Widget};

use crate::partition::BlockID;
use crate::{App, BrowseNeighborhoods, ModalFilters, Partitioning, Transition};

/// Captures all of the edits somebody makes to a map in the LTN tool. Note this is separate from
/// `map_model::MapEdits`.
#[derive(Serialize, Deserialize)]
pub struct Proposal {
    pub map: MapName,
    pub name: String,
    pub abst_version: String,

    pub partitioning: Partitioning,
    pub modal_filters: ModalFilters,
}

impl Proposal {
    fn from_app(app: &App) -> Self {
        Self {
            map: app.map.get_name().clone(),
            name: app
                .session
                .proposal_name
                .clone()
                .unwrap_or(String::from("existing LTNs")),
            abst_version: map_gui::tools::version().to_string(),

            partitioning: app.session.partitioning.clone(),
            modal_filters: app.session.modal_filters.clone(),
        }
    }

    fn make_active(self, ctx: &EventCtx, app: &mut App) {
        app.session.proposal_name = Some(self.name);
        app.session.partitioning = self.partitioning;
        app.session.modal_filters = self.modal_filters;
        app.session.draw_all_filters = app.session.modal_filters.draw(ctx, &app.map);
    }

    /// Try to load a proposal. If it fails, returns a popup message state.
    pub fn load(ctx: &mut EventCtx, app: &mut App, path: String) -> Option<Box<dyn State<App>>> {
        match Self::inner_load(ctx, app, &path) {
            Ok(()) => None,
            Err(err) => Some(PopupMsg::new_state(
                ctx,
                "Error",
                vec![
                    format!("Couldn't load proposal {}", path),
                    err.to_string(),
                    "The format of saved proposals recently changed.".to_string(),
                    "Contact dabreegster@gmail.com if you need help restoring a file.".to_string(),
                ],
            )),
        }
    }

    fn inner_load(ctx: &mut EventCtx, app: &mut App, path: &str) -> Result<()> {
        let bytes = abstio::slurp_file(path)?;
        let decoder = flate2::read::GzDecoder::new(&bytes[..]);
        let value = serde_json::from_reader(decoder)?;
        let proposal = perma::from_permanent(&app.map, value)?;

        // TODO We could try to detect if the file's partitioning (road IDs and such) still matches
        // this version of the map or not

        // When initially loading a proposal from CLI flag, the partitioning will be a placeholder.
        // Don't stash it.
        if !app.session.partitioning.is_empty() {
            stash_current_proposal(app);

            // Start a new proposal
            app.session.alt_proposals.list.push(None);
            app.session.alt_proposals.current = app.session.alt_proposals.list.len() - 1;
        }

        proposal.make_active(ctx, app);

        Ok(())
    }
}

fn stash_current_proposal(app: &mut App) {
    *app.session
        .alt_proposals
        .list
        .get_mut(app.session.alt_proposals.current)
        .unwrap() = Some(Proposal::from_app(app));
}

fn switch_to_existing_proposal(ctx: &mut EventCtx, app: &mut App, idx: usize) {
    stash_current_proposal(app);

    let proposal = app
        .session
        .alt_proposals
        .list
        .get_mut(idx)
        .unwrap()
        .take()
        .unwrap();
    app.session.alt_proposals.current = idx;

    proposal.make_active(ctx, app);
}

fn save_ui(ctx: &mut EventCtx, app: &App, preserve_state: PreserveState) -> Box<dyn State<App>> {
    let default_name = app
        .session
        .proposal_name
        .clone()
        .unwrap_or_else(String::new);
    PromptInput::new_state(
        ctx,
        "Name this proposal",
        default_name,
        Box::new(|name, ctx, app| {
            // If we overwrite an existing proposal, all hell may break loose. AltProposals state
            // and file state are not synchronized / auto-saved.
            app.session.proposal_name = Some(name.clone());

            match inner_save(app) {
                // If we changed the name, we'll want to recreate the panel
                Ok(()) => preserve_state.switch_to_state(ctx, app),
                Err(err) => Transition::Multi(vec![
                    preserve_state.switch_to_state(ctx, app),
                    Transition::Push(PopupMsg::new_state(
                        ctx,
                        "Error",
                        vec![format!("Couldn't save proposal: {}", err)],
                    )),
                ]),
            }
        }),
    )
}

fn inner_save(app: &App) -> Result<()> {
    let proposal = Proposal::from_app(app);
    let path = abstio::path_ltn_proposals(app.map.get_name(), &proposal.name);

    let json_value = perma::to_permanent(&app.map, &proposal)?;
    let mut output_buffer = Vec::new();
    let mut encoder =
        flate2::write::GzEncoder::new(&mut output_buffer, flate2::Compression::best());
    serde_json::to_writer(&mut encoder, &json_value)?;
    encoder.finish()?;
    abstio::write_raw(path, &output_buffer)
}

fn load_picker_ui(
    ctx: &mut EventCtx,
    app: &App,
    preserve_state: PreserveState,
) -> Box<dyn State<App>> {
    // Don't bother trying to filter out proposals currently loaded -- by loading twice, somebody
    // effectively makes a copy to modify a bit
    ChooseSomething::new_state(
        ctx,
        "Load which proposal?",
        // basename (and thus list_all_objects) turn "foo.json.gz" into "foo.json", so further
        // strip out the extension.
        // TODO Fix basename, but make sure nothing downstream breaks
        Choice::strings(
            abstio::list_all_objects(abstio::path_all_ltn_proposals(app.map.get_name()))
                .into_iter()
                .map(abstutil::basename)
                .collect(),
        ),
        Box::new(|name, ctx, app| {
            match Proposal::load(
                ctx,
                app,
                abstio::path_ltn_proposals(app.map.get_name(), &name),
            ) {
                Some(err_state) => Transition::Replace(err_state),
                None => preserve_state.switch_to_state(ctx, app),
            }
        }),
    )
}

pub struct AltProposals {
    // All entries are filled out, except for the current proposal being worked on
    list: Vec<Option<Proposal>>,
    current: usize,
}

impl AltProposals {
    pub fn new() -> Self {
        Self {
            list: vec![None],
            current: 0,
        }
    }

    pub fn to_widget(&self, ctx: &EventCtx, app: &App) -> Widget {
        let mut col = vec![
            Line("LTN Policy Proposals")
                .small_heading()
                .into_widget(ctx),
            Widget::row(vec![
                ctx.style().btn_outline.text("New").build_def(ctx),
                ctx.style().btn_outline.text("Load").build_def(ctx),
                ctx.style().btn_outline.text("Save").build_def(ctx),
            ]),
        ];
        for (idx, proposal) in self.list.iter().enumerate() {
            let button = if let Some(proposal) = proposal {
                ctx.style()
                    .btn_solid_primary
                    .text(format!("{} - {}", idx + 1, proposal.name))
                    .hotkey(Key::NUM_KEYS[idx])
                    .build_widget(ctx, &format!("switch to proposal {}", idx))
            } else {
                ctx.style()
                    .btn_solid_primary
                    .text(format!(
                        "{} - {}",
                        idx + 1,
                        app.session
                            .proposal_name
                            .as_ref()
                            .unwrap_or(&String::from("existing LTNs")),
                    ))
                    .disabled(true)
                    .build_def(ctx)
            };
            col.push(Widget::row(vec![
                button,
                ctx.style()
                    .btn_close()
                    .disabled(self.list.len() == 1)
                    .build_widget(ctx, &format!("hide proposal {}", idx)),
            ]));
            // If somebody tries to load too many proposals, just stop
            if idx == 9 {
                break;
            }
        }
        Widget::col(col).section(ctx)
    }

    pub fn handle_action(
        ctx: &mut EventCtx,
        app: &mut App,
        preserve_state: PreserveState,
        action: &str,
    ) -> Option<Transition> {
        match action {
            "New" => {
                stash_current_proposal(app);

                // This is expensive -- maybe we should just calculate this once and keep a copy
                // forever
                ctx.loading_screen("create new proposal", |ctx, timer| {
                    crate::clear_current_proposal(ctx, app, timer);
                });

                // Start a new proposal
                app.session.alt_proposals.list.push(None);
                app.session.alt_proposals.current = app.session.alt_proposals.list.len() - 1;
            }
            "Load" => {
                return Some(Transition::Push(load_picker_ui(ctx, app, preserve_state)));
            }
            "Save" => {
                return Some(Transition::Push(save_ui(ctx, app, preserve_state)));
            }
            _ => {
                if let Some(x) = action.strip_prefix("switch to proposal ") {
                    let idx = x.parse::<usize>().unwrap();
                    switch_to_existing_proposal(ctx, app, idx);
                } else if let Some(x) = action.strip_prefix("hide proposal ") {
                    let idx = x.parse::<usize>().unwrap();
                    if idx == app.session.alt_proposals.current {
                        // First make sure we're not hiding the current proposal
                        switch_to_existing_proposal(ctx, app, if idx == 0 { 1 } else { idx - 1 });
                    }

                    // Remove it
                    app.session.alt_proposals.list.remove(idx);

                    // Fix up indices
                    if idx < app.session.alt_proposals.current {
                        app.session.alt_proposals.current -= 1;
                    }
                } else {
                    return None;
                }
            }
        }

        Some(preserve_state.switch_to_state(ctx, app))
    }
}

// After switching proposals, we have to recreate state
//
// To preserve per-neigbhorhood states, we have to transform neighborhood IDs, which may change if
// the partitioning is different. If the boundary is a bit different, match up by all the blocks in
// the current neighborhood.
pub enum PreserveState {
    BrowseNeighborhoods,
    Route,
    Connectivity(Vec<BlockID>),
    Shortcuts(Option<PathRequest>, Vec<BlockID>),
}

impl PreserveState {
    fn switch_to_state(self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        match self {
            PreserveState::BrowseNeighborhoods => {
                Transition::Replace(BrowseNeighborhoods::new_state(ctx, app))
            }
            PreserveState::Route => {
                Transition::Replace(crate::route_planner::RoutePlanner::new_state(ctx, app))
            }
            PreserveState::Connectivity(blocks) => {
                // Count which new neighborhoods have the blocks from the original. Pick the one
                // with the most matches
                let mut count = Counter::new();
                for block in blocks {
                    count.inc(app.session.partitioning.block_to_neighborhood(block));
                }
                Transition::Replace(crate::connectivity::Viewer::new_state(
                    ctx,
                    app,
                    count.max_key(),
                ))
            }
            PreserveState::Shortcuts(req, blocks) => {
                let mut count = Counter::new();
                for block in blocks {
                    count.inc(app.session.partitioning.block_to_neighborhood(block));
                }
                Transition::Replace(crate::shortcut_viewer::BrowseShortcuts::new_state(
                    ctx,
                    app,
                    count.max_key(),
                    req,
                ))
            }
        }
    }
}