rmf_site_editor 0.0.3

File format parsing for rmf_site_editor
Documentation
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
/*
 * Copyright (C) 2022 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

use bevy::{
    ecs::{
        event::Events,
        hierarchy::ChildOf,
        system::{BoxedSystem, SystemParam, SystemState},
    },
    prelude::*,
};
use rmf_site_picking::Preview;
use std::{
    collections::{BTreeMap, BTreeSet, HashMap},
    error::Error,
    path::PathBuf,
    sync::Arc,
};
use thiserror::Error as ThisError;

use crate::{exit_confirmation::SiteChanged, recency::RecencyRanking, site::*, ExportFormat};
use rmf_site_format::*;
use sdformat::yaserde;

#[derive(Event)]
pub struct SaveSite {
    pub site: Entity,
    pub to_location: PathBuf,
    pub format: ExportFormat,
}

#[derive(Debug, Clone)]
pub struct SavingArgs {
    /// ID of the site which is being saved.
    pub site: Entity,
}

/// The result of trying to save data for an extension.
pub type SavingResult<E> = Result<serde_json::Value, E>;

/// A system used to extend saving behavior which can generate a [`serde_json::Value`]
/// to serialize the data related to this extension that needs to be saved.
pub(crate) type SavingSystem =
    BoxedSystem<In<SavingArgs>, Result<serde_json::Value, Arc<dyn Error>>>;

// TODO(MXG): Change all these errors to use u32 SiteIDs instead of entities
#[derive(ThisError, Debug, Clone)]
pub enum SiteGenerationError {
    #[error("the specified entity [{0:?}] does not refer to a site")]
    InvalidSiteEntity(Entity),
    #[error("an object [{object:?}] has a reference to an anchor [{anchor:?}] that is not valid")]
    BrokenAnchorReference { object: Entity, anchor: Entity },
    #[error("an object [{object:?}] has a reference to a group [{group:?}] that is not valid")]
    BrokenAffiliation { object: Entity, group: Entity },
    #[error("an object has a reference to an empty group")]
    EmptyAffiliation(Entity),
    #[error("an object has a reference to a level that does not exist")]
    BrokenLevelReference(Entity),
    #[error("an object has a reference to a nav graph that does not exist")]
    BrokenNavGraphReference(Entity),
    #[error("an issue has a reference to an object that does not exist")]
    BrokenIssueReference(Entity),
    #[error("lift {0} is missing its anchor group")]
    BrokenLift(u32),
    #[error(
        "anchor {anchor:?} is being referenced for site {site:?} but does not belong to that site"
    )]
    InvalidAnchorReference { site: u32, anchor: u32 },
    #[error(
        "lift door {door:?} is referencing an anchor that does not belong to its lift {anchor:?}"
    )]
    InvalidLiftDoorReference { door: Entity, anchor: Entity },
    #[error("an object has a reference to a modifier that does not exist")]
    BrokenModifier(Entity),
    #[error("Extension [{extension}] encountered an error: {error}")]
    ExtensionError {
        extension: Arc<str>,
        error: Arc<dyn Error>,
    },
    #[error("A site element [{0:?}] is missing a site ID")]
    MissingSiteID(Entity),
}

/// This is used when a drawing is being edited to fix its parenting before we
/// attempt to save the site.
// TODO(@mxgrey): Remove this when we no longer need to de-parent drawings while
// editing them.
fn assemble_edited_drawing(world: &mut World) {
    let Some(c) = world.get_resource::<CurrentEditDrawing>().copied() else {
        return;
    };
    let Some(c) = c.target() else { return };
    let Ok(mut level) = world.get_entity_mut(c.level) else {
        return;
    };
    level.add_children(&[c.drawing]);
}

/// Revert the drawing back to the root so it can continue to be edited.
fn disassemble_edited_drawing(world: &mut World) {
    let Some(c) = world.get_resource::<CurrentEditDrawing>().copied() else {
        return;
    };
    let Some(c) = c.target() else { return };
    let Ok(mut level) = world.get_entity_mut(c.level) else {
        return;
    };
    level.remove_children(&[c.drawing]);
}

/// Look through all the elements that we will be saving and assign a SiteID
/// component to any elements that do not have one already.
fn assign_site_ids(world: &mut World, site: Entity) -> Result<(), SiteGenerationError> {
    let mut state: SystemState<(
        Query<
            Entity,
            (
                Or<(
                    With<Anchor>,
                    With<DoorType>,
                    With<DrawingMarker>,
                    With<FloorMarker>,
                    With<LightKind>,
                    With<ModelMarker>,
                    With<PhysicalCameraProperties>,
                    With<WallMarker>,
                )>,
                Without<Pending>,
            ),
        >,
        Query<Entity, (With<ModelMarker>, With<Group>)>,
        Query<Entity, (With<ModelMarker>, Without<Group>, Without<Preview>)>,
        Query<(Entity, &Affiliation<Entity>), With<ScenarioModifiers<Entity>>>,
        Query<Entity, (With<Task>, Without<Pending>)>,
        Query<
            Entity,
            (
                Or<(With<LaneMarker>, With<LocationTags>, With<NavGraphMarker>)>,
                Without<Pending>,
            ),
        >,
        Query<Entity, (With<LevelElevation>, Without<Pending>)>,
        Query<Entity, (With<LiftCabin<Entity>>, Without<Pending>)>,
        Query<
            Entity,
            (
                Or<(
                    With<Anchor>,
                    With<FiducialMarker>,
                    With<MeasurementMarker>,
                    With<Group>,
                )>,
                Without<Pending>,
            ),
        >,
        Query<(), With<DrawingMarker>>,
        Query<&ChildCabinAnchorGroup>,
        Query<Entity, (With<Anchor>, Without<Pending>)>,
        Query<&SiteID>,
        Query<&Children>,
        AssignSiteID,
    )> = SystemState::new(world);

    let (
        level_children,
        model_descriptions,
        model_instances,
        scenarios,
        tasks,
        nav_graph_elements,
        levels,
        lifts,
        drawing_children,
        drawings,
        cabin_anchor_groups,
        cabin_anchor_group_children,
        site_ids,
        children,
        mut assign_site_ids,
    ) = state.get_mut(world);

    let mut new_entities = Vec::new();

    let site_children = match children.get(site) {
        Ok(children) => children,
        Err(_) => {
            // The site seems to have no children at all. That's suspicious but
            // not impossible if the site is completely empty. In that case
            // there is no need to assign any SiteIDs
            return Ok(());
        }
    };

    for site_child in site_children {
        if let Ok(level) = levels.get(*site_child) {
            if !site_ids.contains(level) {
                new_entities.push(level);
            }

            if let Ok(current_level_children) = children.get(level) {
                for child in current_level_children {
                    if level_children.contains(*child) {
                        if !site_ids.contains(*child) {
                            new_entities.push(*child);
                        }

                        if drawings.contains(*child) {
                            if let Ok(drawing_children) = children.get(*child) {
                                for drawing_child in drawing_children {
                                    if !site_ids.contains(*drawing_child) {
                                        new_entities.push(*drawing_child);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if let Ok(model_description) = model_descriptions.get(*site_child) {
            if !site_ids.contains(model_description) {
                new_entities.push(model_description);
            }
        }

        if let Ok(model_instance) = model_instances.get(*site_child) {
            if !site_ids.contains(model_instance) {
                new_entities.push(model_instance);
            }
        }

        if let Ok((scenario, _)) = scenarios.get(*site_child) {
            // Ensure root scenarios have the smallest Site_ID, since when deserializing, child scenarios would
            // require parent scenarios to already be spawned and have its parent entity
            let mut queue = vec![scenario];
            let mut target_scenario = scenario;
            while let Ok((e, target_parent)) = scenarios.get(target_scenario) {
                let Some(p) = target_parent.0 else {
                    break;
                };
                queue.push(e);
                target_scenario = p;
            }
            queue.reverse();

            while let Some(scenario) = queue.pop() {
                if !site_ids.contains(scenario) {
                    new_entities.push(scenario);
                }
            }
        }

        if let Ok(task) = tasks.get(*site_child) {
            if !site_ids.contains(task) {
                new_entities.push(task);
            }
        }

        if let Ok(e) = drawing_children.get(*site_child) {
            // Sites can contain anchors and fiducials but should not contain
            // measurements, so this query doesn't make perfect sense to use
            // here, but it shouldn't be harmful and it saves us from writing
            // yet another query.
            if !site_ids.contains(e) {
                new_entities.push(e);
            }
        }

        if let Ok(e) = nav_graph_elements.get(*site_child) {
            if !site_ids.contains(e) {
                new_entities.push(e);
            }
        }

        if let Ok(lift) = lifts.get(*site_child) {
            if let Ok(anchor_group) = cabin_anchor_groups.get(*site_child) {
                if let Ok(anchor_children) = children.get(**anchor_group) {
                    for anchor_child in anchor_children {
                        if let Ok(e) = cabin_anchor_group_children.get(*anchor_child) {
                            if !site_ids.contains(e) {
                                new_entities.push(e);
                            }
                        }
                    }
                }
            }
            if !site_ids.contains(lift) {
                new_entities.push(lift);
            }

            if let Ok(children) = children.get(lift) {
                for child in children {
                    if level_children.contains(*child) {
                        if !site_ids.contains(*child) {
                            new_entities.push(*child);
                        }
                    }
                }
            }
        }
    }

    let mut next_site_id = assign_site_ids
        .assign_for(site)
        .ok_or(SiteGenerationError::InvalidSiteEntity(site))?;
    for e in &new_entities {
        next_site_id.assign_to(*e);
    }

    state.apply(world);
    Ok(())
}

#[derive(SystemParam)]
pub struct AssignSiteID<'w, 's> {
    next: Query<'w, 's, &'static mut NextSiteID>,
    existing: Query<'w, 's, &'static SiteID>,
    commands: Commands<'w, 's>,
}

impl<'w, 's> AssignSiteID<'w, 's> {
    pub fn assign_for(&mut self, site: Entity) -> Option<SiteIDAssigner<'w, 's, '_>> {
        self.next.get_mut(site).ok().map(|next| SiteIDAssigner {
            next,
            existing: &self.existing,
            commands: &mut self.commands,
        })
    }
}

pub struct SiteIDAssigner<'w, 's, 'a> {
    next: Mut<'a, NextSiteID>,
    existing: &'a Query<'w, 's, &'static SiteID>,
    commands: &'a mut Commands<'w, 's>,
}

impl<'w, 's, 'a> SiteIDAssigner<'w, 's, 'a> {
    pub fn assign_to(&mut self, entity: Entity) -> u32 {
        if let Ok(id) = self.existing.get(entity) {
            // Skip the assignment if the entity already has a SiteID
            return id.0;
        }

        let n = **self.next;
        self.commands.entity(entity).insert(SiteID(n));
        **self.next += 1;
        return n;
    }
}

fn collect_site_anchors(world: &mut World, site: Entity) -> BTreeMap<u32, Anchor> {
    let mut state: SystemState<(
        Query<&Children>,
        Query<(&SiteID, &Anchor), Without<Pending>>,
    )> = SystemState::new(world);

    let mut site_anchors = BTreeMap::new();
    let (q_children, q_anchors) = state.get(world);
    if let Ok(children) = q_children.get(site) {
        for child in children {
            if let Ok((site_id, anchor)) = q_anchors.get(*child) {
                site_anchors.insert(site_id.0, anchor.clone());
            }
        }
    }

    site_anchors
}

fn generate_levels(
    world: &mut World,
    site: Entity,
) -> Result<BTreeMap<u32, Level>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<&Children, With<NameOfSite>>,
        Query<(&Anchor, &SiteID)>,
        Query<&SiteID, With<Group>>,
        Query<
            (
                &Edge<Entity>,
                Option<&Original<Edge<Entity>>>,
                &NameInSite,
                &DoorType,
                &SiteID,
            ),
            Without<Pending>,
        >,
        Query<
            (
                &NameInSite,
                &AssetSource,
                &Pose,
                &PixelsPerMeter,
                &PreferredSemiTransparency,
                &SiteID,
                &Children,
            ),
            (With<DrawingMarker>, Without<Pending>),
        >,
        Query<
            (
                &Point<Entity>,
                Option<&Original<Point<Entity>>>,
                &Affiliation<Entity>,
                &SiteID,
            ),
            (With<FiducialMarker>, Without<Pending>),
        >,
        Query<
            (
                &Path<Entity>,
                Option<&Original<Path<Entity>>>,
                &Affiliation<Entity>,
                &PreferredSemiTransparency,
                &SiteID,
            ),
            (With<FloorMarker>, Without<Pending>),
        >,
        Query<(&LightKind, &Pose, &SiteID)>,
        Query<
            (
                &Edge<Entity>,
                Option<&Original<Edge<Entity>>>,
                &Distance,
                &SiteID,
            ),
            (With<MeasurementMarker>, Without<Pending>),
        >,
        Query<(&NameInSite, &Pose, &PhysicalCameraProperties, &SiteID), Without<Pending>>,
        Query<
            (
                &Edge<Entity>,
                Option<&Original<Edge<Entity>>>,
                &Affiliation<Entity>,
                &SiteID,
            ),
            (With<WallMarker>, Without<Pending>),
        >,
        Query<
            (
                &NameInSite,
                &LevelElevation,
                &GlobalFloorVisibility,
                &GlobalDrawingVisibility,
                &SiteID,
                &Children,
                Option<&RecencyRanking<FloorMarker>>,
                Option<&RecencyRanking<DrawingMarker>>,
            ),
            Without<Pending>,
        >,
        Query<&SiteID>,
        Query<(&Pose, &NameInSite, &SiteID), With<UserCameraPoseMarker>>,
    )> = SystemState::new(world);

    let (
        q_site_children,
        q_anchors,
        q_groups,
        q_doors,
        q_drawings,
        q_fiducials,
        q_floors,
        q_lights,
        q_measurements,
        q_physical_cameras,
        q_walls,
        q_levels,
        q_site_ids,
        q_user_camera_poses,
    ) = state.get(world);

    let get_anchor_id = |object, anchor| {
        let (_, site_id) = q_anchors
            .get(anchor)
            .map_err(|_| SiteGenerationError::BrokenAnchorReference { object, anchor })?;
        Ok(site_id.0)
    };

    let get_group_id = |object, group| {
        q_groups
            .get(group)
            .map(|id| id.0)
            .map_err(|_| SiteGenerationError::BrokenAffiliation { object, group })
    };

    let get_anchor_id_edge = |object, edge: &Edge<Entity>| {
        let left = get_anchor_id(object, edge.left())?;
        let right = get_anchor_id(object, edge.right())?;
        Ok(Edge::new(left, right))
    };

    let get_anchor_id_path = |object, entities: &Vec<Entity>| {
        let mut anchor_ids = Vec::new();
        anchor_ids.reserve(entities.len());
        for entity in entities {
            let id = get_anchor_id(object, *entity)?;
            anchor_ids.push(id);
        }
        Ok(Path(anchor_ids))
    };

    let mut levels = BTreeMap::new();
    if let Ok(site_children) = q_site_children.get(site) {
        for c in site_children.iter() {
            if let Ok((
                name,
                elevation,
                floor_vis,
                drawing_vis,
                level_id,
                level_children,
                floor_ranking,
                drawing_ranking,
            )) = q_levels.get(c)
            {
                let mut level = Level::new(
                    LevelProperties {
                        name: name.clone(),
                        elevation: elevation.clone(),
                        global_floor_visibility: floor_vis.clone(),
                        global_drawing_visibility: drawing_vis.clone(),
                    },
                    RankingsInLevel {
                        floors: floor_ranking
                            .map(|r| r.to_u32(&q_site_ids))
                            .unwrap_or(Vec::new()),
                        drawings: drawing_ranking
                            .map(|r| r.to_u32(&q_site_ids))
                            .unwrap_or(Vec::new()),
                    },
                );
                for c in level_children.iter() {
                    if let Ok((anchor, id)) = q_anchors.get(c) {
                        level.anchors.insert(id.0, anchor.clone());
                    }
                    if let Ok((edge, o_edge, name, kind, id)) = q_doors.get(c) {
                        let edge = o_edge.map(|x| &x.0).unwrap_or(edge);
                        let anchors = get_anchor_id_edge(c, edge)?;
                        level.doors.insert(
                            id.0,
                            Door {
                                anchors,
                                name: name.clone(),
                                kind: kind.clone(),
                                marker: DoorMarker,
                            },
                        );
                    }
                    if let Ok((
                        name,
                        source,
                        pose,
                        pixels_per_meter,
                        preferred_alpha,
                        id,
                        children,
                    )) = q_drawings.get(c)
                    {
                        let mut measurements = BTreeMap::new();
                        let mut fiducials = BTreeMap::new();
                        let mut anchors = BTreeMap::new();
                        for e in children.iter() {
                            if let Ok((anchor, anchor_id)) = q_anchors.get(e) {
                                anchors.insert(anchor_id.0, anchor.clone());
                            }
                            if let Ok((edge, o_edge, distance, id)) = q_measurements.get(e) {
                                let edge = o_edge.map(|x| &x.0).unwrap_or(edge);
                                let anchors = get_anchor_id_edge(e, edge)?;
                                measurements.insert(
                                    id.0,
                                    Measurement {
                                        anchors,
                                        distance: distance.clone(),
                                        marker: MeasurementMarker,
                                    },
                                );
                            }
                            if let Ok((point, o_point, affiliation, id)) = q_fiducials.get(e) {
                                let point = o_point.map(|x| &x.0).unwrap_or(point);
                                let anchor = Point(get_anchor_id(e, point.0)?);
                                let affiliation = if let Affiliation(Some(a)) = affiliation {
                                    Affiliation(Some(get_group_id(e, *a)?))
                                } else {
                                    Affiliation(None)
                                };
                                fiducials.insert(
                                    id.0,
                                    Fiducial {
                                        anchor,
                                        affiliation,
                                        marker: FiducialMarker,
                                    },
                                );
                            }
                        }
                        level.drawings.insert(
                            id.0,
                            Drawing {
                                properties: DrawingProperties {
                                    name: name.clone(),
                                    source: source.clone(),
                                    pose: pose.clone(),
                                    pixels_per_meter: pixels_per_meter.clone(),
                                    preferred_semi_transparency: preferred_alpha.clone(),
                                },
                                anchors,
                                fiducials,
                                measurements,
                            },
                        );
                    }
                    if let Ok((path, o_path, texture, preferred_alpha, id)) = q_floors.get(c) {
                        let path = o_path.map(|x| &x.0).unwrap_or(path);
                        let anchors = get_anchor_id_path(c, &path)?;
                        let texture = if let Affiliation(Some(e)) = texture {
                            Affiliation(Some(get_group_id(c, *e)?))
                        } else {
                            Affiliation(None)
                        };

                        level.floors.insert(
                            id.0,
                            Floor {
                                anchors,
                                texture,
                                preferred_semi_transparency: preferred_alpha.clone(),
                                marker: FloorMarker,
                            },
                        );
                    }
                    if let Ok((kind, pose, id)) = q_lights.get(c) {
                        level.lights.insert(
                            id.0,
                            Light {
                                pose: pose.clone(),
                                kind: kind.clone(),
                            },
                        );
                    }
                    if let Ok((name, pose, properties, id)) = q_physical_cameras.get(c) {
                        level.physical_cameras.insert(
                            id.0,
                            PhysicalCamera {
                                name: name.clone(),
                                pose: pose.clone(),
                                properties: properties.clone(),
                                previewable: PreviewableMarker,
                            },
                        );
                    }
                    if let Ok((edge, o_edge, texture, id)) = q_walls.get(c) {
                        let edge = o_edge.map(|x| &x.0).unwrap_or(edge);
                        let anchors = get_anchor_id_edge(c, edge)?;
                        let texture = if let Affiliation(Some(e)) = texture {
                            Affiliation(Some(get_group_id(c, *e)?))
                        } else {
                            Affiliation(None)
                        };

                        level.walls.insert(
                            id.0,
                            Wall {
                                anchors,
                                texture,
                                marker: WallMarker,
                            },
                        );
                    }
                    if let Ok((pose, name, id)) = q_user_camera_poses.get(c) {
                        level.user_camera_poses.insert(
                            id.0,
                            UserCameraPose {
                                name: name.clone(),
                                pose: pose.clone(),
                                marker: UserCameraPoseMarker,
                            },
                        );
                    }
                }
                levels.insert(level_id.0, level);
            }
        }
    }
    return Ok(levels);
}

type QueryLift<'w, 's> = Query<
    'w,
    's,
    (
        Entity,
        &'static NameInSite,
        &'static Edge<Entity>,
        Option<&'static Original<Edge<Entity>>>,
        &'static LiftCabin<Entity>,
        &'static IsStatic,
        &'static InitialLevel<Entity>,
        &'static SiteID,
        &'static ChildOf,
    ),
    Without<Pending>,
>;

fn generate_lifts(
    world: &mut World,
    site: Entity,
) -> Result<BTreeMap<u32, Lift<u32>>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<(&SiteID, &Anchor), Without<Pending>>,
        QueryLiftDoor,
        Query<&SiteID, (With<LevelElevation>, Without<Pending>)>,
        QueryLift,
        Query<Entity, With<CabinAnchorGroup>>,
        Query<&ChildOf, Without<Pending>>,
        Query<&Children>,
        Query<&SiteID>,
    )> = SystemState::new(world);

    let (
        q_anchors,
        q_doors,
        q_levels,
        q_lifts,
        q_cabin_anchor_groups,
        q_child_of,
        q_children,
        q_site_id,
    ) = state.get(world);

    let mut lifts = BTreeMap::new();

    let get_anchor_id = |object, anchor| {
        let (site_id, _) = q_anchors
            .get(anchor)
            .map_err(|_| SiteGenerationError::BrokenAnchorReference { object, anchor })?;
        Ok(site_id.0)
    };

    let get_level_id = |entity| -> Result<u32, SiteGenerationError> {
        let site_id = q_levels
            .get(entity)
            .map_err(|_| SiteGenerationError::BrokenLevelReference(entity))?;
        Ok(site_id.0)
    };

    let get_anchor_id_edge = |object, edge: &Edge<Entity>| {
        let left = get_anchor_id(object, edge.left())?;
        let right = get_anchor_id(object, edge.right())?;
        Ok(Edge::new(left, right))
    };

    let confirm_entity_parent = |intended_parent, child| {
        if let Ok(actual_parent) = q_child_of.get(child) {
            if actual_parent.parent() == intended_parent {
                return true;
            }
        }

        return false;
    };

    let validate_site_anchor = |anchor| {
        if confirm_entity_parent(site, anchor) {
            return Ok(());
        }

        Err(SiteGenerationError::InvalidAnchorReference {
            site: q_site_id.get(site).unwrap().0,
            anchor: q_site_id.get(anchor).unwrap().0,
        })
    };

    let validate_site_anchors = |edge: &Edge<Entity>| {
        validate_site_anchor(edge.left())?;
        validate_site_anchor(edge.right())?;
        Ok(())
    };

    for (lift_entity, name, edge, o_edge, cabin, is_static, initial_level, id, child_of) in &q_lifts
    {
        if child_of.parent() != site {
            continue;
        }

        // TODO(MXG): Clean up this spaghetti
        let anchor_group_entity = match match q_children.get(lift_entity) {
            Ok(children) => children,
            Err(_) => return Err(SiteGenerationError::BrokenLift(id.0)),
        }
        .iter()
        .find(|c| q_cabin_anchor_groups.contains(*c))
        {
            Some(c) => c,
            None => return Err(SiteGenerationError::BrokenLift(id.0)),
        };

        let edge = o_edge.map(|x| &x.0).unwrap_or(edge);
        validate_site_anchors(edge)?;

        let validate_level_door_anchor = |door: Entity, anchor: Entity| {
            if confirm_entity_parent(anchor_group_entity, anchor) {
                return Ok(());
            }

            Err(SiteGenerationError::InvalidLiftDoorReference { door, anchor })
        };

        let validate_level_door_anchors = |door: Entity, edge: &Edge<Entity>| {
            validate_level_door_anchor(door, edge.left())?;
            validate_level_door_anchor(door, edge.right())?;
            get_anchor_id_edge(door, edge)
        };

        let mut cabin_anchors = BTreeMap::new();
        let mut cabin_doors = BTreeMap::new();
        if let Ok(children) = q_children.get(lift_entity) {
            for child in children {
                if let Ok(anchor_group) = q_cabin_anchor_groups.get(*child) {
                    if let Ok(anchor_children) = q_children.get(anchor_group) {
                        for anchor_child in anchor_children {
                            if let Ok((site_id, anchor)) = q_anchors.get(*anchor_child) {
                                cabin_anchors.insert(site_id.0, anchor.clone());
                            }
                        }
                    }
                }

                if let Ok((site_id, door_type, edge, o_edge, visits)) = q_doors.get(*child) {
                    let edge = o_edge.map(|x| &x.0).unwrap_or(edge);
                    cabin_doors.insert(
                        site_id.0,
                        LiftCabinDoor {
                            kind: door_type.clone(),
                            reference_anchors: validate_level_door_anchors(*child, edge)?,
                            visits: LevelVisits(
                                visits
                                    .iter()
                                    .map(|level| get_level_id(*level))
                                    .collect::<Result<_, _>>()?,
                            ),
                            marker: Default::default(),
                        },
                    );
                }
            }
        }

        let reference_anchors = get_anchor_id_edge(lift_entity, edge)?;
        lifts.insert(
            id.0,
            Lift {
                cabin_doors,
                properties: LiftProperties {
                    name: name.clone(),
                    reference_anchors,
                    cabin: cabin.to_u32(&q_doors),
                    is_static: is_static.clone(),
                    initial_level: InitialLevel(
                        initial_level
                            .0
                            .map_or(Ok(None), |level| get_level_id(level).map(|id| Some(id)))?,
                    ),
                },
                cabin_anchors,
            },
        );
    }

    return Ok(lifts);
}

fn generate_fiducials(
    world: &mut World,
    parent: Entity,
) -> Result<BTreeMap<u32, Fiducial<u32>>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<&SiteID, (With<Anchor>, Without<Pending>)>,
        Query<&SiteID, (With<Group>, Without<Pending>)>,
        Query<
            (&Point<Entity>, &Affiliation<Entity>, &SiteID),
            (With<FiducialMarker>, Without<Pending>),
        >,
        Query<&Children>,
    )> = SystemState::new(world);

    let (q_anchor_ids, q_group_ids, q_fiducials, q_children) = state.get(world);

    let Ok(children) = q_children.get(parent) else {
        return Ok(BTreeMap::new());
    };

    let mut fiducials = BTreeMap::new();
    for child in children {
        let Ok((point, affiliation, site_id)) = q_fiducials.get(*child) else {
            continue;
        };
        let anchor = q_anchor_ids
            .get(point.0)
            .map_err(|_| SiteGenerationError::BrokenAnchorReference {
                object: *child,
                anchor: point.0,
            })?
            .0;
        let anchor = Point(anchor);
        let affiliation = if let Some(e) = affiliation.0 {
            let group_id = q_group_ids
                .get(e)
                .map_err(|_| SiteGenerationError::BrokenAffiliation {
                    object: *child,
                    group: e,
                })?
                .0;
            Affiliation(Some(group_id))
        } else {
            Affiliation(None)
        };

        fiducials.insert(
            site_id.0,
            Fiducial {
                anchor,
                affiliation,
                marker: Default::default(),
            },
        );
    }

    Ok(fiducials)
}

fn generate_fiducial_groups(
    world: &mut World,
    parent: Entity,
) -> Result<BTreeMap<u32, FiducialGroup>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<(&NameInSite, &SiteID), (With<Group>, With<FiducialMarker>)>,
        Query<&Children>,
    )> = SystemState::new(world);

    let (q_groups, q_children) = state.get(world);

    let Ok(children) = q_children.get(parent) else {
        return Ok(BTreeMap::new());
    };

    let mut fiducial_groups = BTreeMap::new();
    for child in children {
        let Ok((name, site_id)) = q_groups.get(*child) else {
            continue;
        };
        fiducial_groups.insert(site_id.0, FiducialGroup::new(name.clone()));
    }

    Ok(fiducial_groups)
}

fn generate_texture_groups(
    world: &mut World,
    parent: Entity,
) -> Result<BTreeMap<u32, TextureGroup>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<(&NameInSite, &Texture, &SiteID), With<Group>>,
        Query<&Children>,
    )> = SystemState::new(world);

    let (q_groups, q_children) = state.get(world);

    let Ok(children) = q_children.get(parent) else {
        return Ok(BTreeMap::new());
    };

    let mut texture_groups = BTreeMap::new();
    for child in children {
        let Ok((name, texture, site_id)) = q_groups.get(*child) else {
            continue;
        };
        texture_groups.insert(
            site_id.0,
            TextureGroup {
                name: name.clone(),
                texture: texture.clone(),
                group: Default::default(),
            },
        );
    }

    Ok(texture_groups)
}

fn generate_nav_graphs(
    world: &mut World,
    site: Entity,
) -> Result<BTreeMap<u32, NavGraph>, SiteGenerationError> {
    let mut state: SystemState<
        Query<
            (&NameInSite, &DisplayColor, &SiteID, &ChildOf),
            (With<NavGraphMarker>, Without<Pending>),
        >,
    > = SystemState::new(world);

    let q_nav_graphs = state.get(world);

    let mut nav_graphs = BTreeMap::new();
    for (name, color, id, child_of) in &q_nav_graphs {
        if child_of.parent() != site {
            continue;
        }

        nav_graphs.insert(
            id.0,
            NavGraph {
                name: name.clone(),
                color: color.clone(),
                marker: Default::default(),
            },
        );
    }

    return Ok(nav_graphs);
}

fn generate_mutex_groups(
    world: &mut World,
    parent: Entity,
) -> Result<BTreeMap<u32, MutexGroup>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<(&NameInSite, &SiteID), (With<Group>, With<MutexMarker>)>,
        Query<&Children>,
    )> = SystemState::new(world);

    let (q_groups, q_children) = state.get(world);

    let Ok(children) = q_children.get(parent) else {
        return Ok(BTreeMap::new());
    };

    let mut mutex_groups = BTreeMap::new();
    for child in children {
        let Ok((name, site_id)) = q_groups.get(*child) else {
            continue;
        };
        mutex_groups.insert(site_id.0, MutexGroup::new(name.clone()));
    }

    Ok(mutex_groups)
}

fn generate_lanes(
    world: &mut World,
    site: Entity,
) -> Result<BTreeMap<u32, Lane<u32>>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<
            (
                Entity,
                &Edge<Entity>,
                Option<&Original<Edge<Entity>>>,
                &Motion,
                &ReverseLane,
                &Affiliation<Entity>,
                &AssociatedGraphs<Entity>,
                &SiteID,
                &ChildOf,
            ),
            (With<LaneMarker>, Without<Pending>),
        >,
        Query<&SiteID, With<NavGraphMarker>>,
        Query<&SiteID, With<Anchor>>,
        Query<&SiteID, (With<Group>, Without<Pending>)>,
    )> = SystemState::new(world);

    let (q_lanes, q_nav_graphs, q_anchors, q_group_ids) = state.get(world);

    let get_anchor_id = |object, anchor| {
        let site_id = q_anchors
            .get(anchor)
            .map_err(|_| SiteGenerationError::BrokenAnchorReference { object, anchor })?;
        Ok(site_id.0)
    };

    let get_anchor_id_edge = |object, edge: &Edge<Entity>| {
        let left = get_anchor_id(object, edge.left())?;
        let right = get_anchor_id(object, edge.right())?;
        Ok(Edge::new(left, right))
    };

    let mut lanes = BTreeMap::new();
    for (e, edge, o_edge, forward, reverse, affiliation, graphs, lane_id, child_of) in &q_lanes {
        if child_of.parent() != site {
            continue;
        }

        let edge = o_edge.map(|x| &x.0).unwrap_or(edge);
        let edge = get_anchor_id_edge(e, edge)?;
        let graphs = graphs
            .to_u32(&q_nav_graphs)
            .map_err(|e| SiteGenerationError::BrokenNavGraphReference(e))?;

        let mutex = if let Some(group) = affiliation.0 {
            let group_id = q_group_ids
                .get(group)
                .map_err(|_| SiteGenerationError::BrokenAffiliation { object: e, group })?
                .0;
            Affiliation(Some(group_id))
        } else {
            Affiliation(None)
        };

        lanes.insert(
            lane_id.0,
            Lane {
                anchors: edge.clone(),
                forward: forward.clone(),
                reverse: reverse.clone(),
                mutex,
                graphs,
                marker: LaneMarker,
            },
        );
    }

    Ok(lanes)
}

fn generate_locations(
    world: &mut World,
    site: Entity,
) -> Result<BTreeMap<u32, Location<u32>>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<
            (
                Entity,
                &Point<Entity>,
                Option<&Original<Point<Entity>>>,
                &LocationTags,
                &NameInSite,
                &Affiliation<Entity>,
                &AssociatedGraphs<Entity>,
                &SiteID,
                &ChildOf,
            ),
            Without<Pending>,
        >,
        Query<&SiteID, With<NavGraphMarker>>,
        Query<&SiteID, With<Anchor>>,
        Query<&SiteID, (With<Group>, With<MutexMarker>)>,
    )> = SystemState::new(world);

    let (q_locations, q_nav_graphs, q_anchors, q_mutex_groups) = state.get(world);

    let get_anchor_id = |object, anchor| {
        let site_id = q_anchors
            .get(anchor)
            .map_err(|_| SiteGenerationError::BrokenAnchorReference { object, anchor })?;
        Ok(site_id.0)
    };

    let mut locations = BTreeMap::new();
    for (e, point, o_point, tags, name, mutex, graphs, location_id, child_of) in &q_locations {
        if child_of.parent() != site {
            continue;
        }

        let point = o_point.map(|x| &x.0).unwrap_or(point);
        let point = get_anchor_id(e, point.0)?;
        let graphs = graphs
            .to_u32(&q_nav_graphs)
            .map_err(|e| SiteGenerationError::BrokenNavGraphReference(e))?;
        let mutex = if let Some(mutex_group) = mutex.0 {
            let mutex_group_id = q_mutex_groups.get(mutex_group).map_err(|_| {
                SiteGenerationError::BrokenAffiliation {
                    object: e,
                    group: mutex_group,
                }
            })?;
            Affiliation(Some(mutex_group_id.0))
        } else {
            Affiliation(None)
        };

        locations.insert(
            location_id.0,
            Location {
                anchor: Point(point),
                tags: tags.clone(),
                name: name.clone(),
                mutex,
                graphs,
            },
        );
    }

    Ok(locations)
}

fn generate_graph_rankings(
    world: &mut World,
    site: Entity,
) -> Result<Vec<u32>, SiteGenerationError> {
    let mut state: SystemState<(Query<&RecencyRanking<NavGraphMarker>>, Query<&SiteID>)> =
        SystemState::new(world);

    let (rankings, site_id) = state.get(world);
    let ranking = match rankings.get(site) {
        Ok(r) => r,
        Err(_) => return Ok(Vec::new()),
    };

    ranking
        .entities()
        .iter()
        .map(|e| {
            site_id
                .get(*e)
                .map(|s| s.0)
                .map_err(|_| SiteGenerationError::BrokenNavGraphReference(*e))
        })
        .collect()
}

fn generate_site_properties(
    world: &mut World,
    site: Entity,
) -> Result<SiteProperties<u32>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<(
            &NameOfSite,
            &FilteredIssues<Entity>,
            &FilteredIssueKinds,
            &GeographicComponent,
            &SiteExtensionSettings,
        )>,
        Query<&SiteID>,
    )> = SystemState::new(world);

    let (q_properties, q_ids) = state.get(world);

    let Ok((name, issues, issue_kinds, geographic_offset, extension_settings)) =
        q_properties.get(site)
    else {
        return Err(SiteGenerationError::InvalidSiteEntity(site));
    };

    let mut converted_issues = BTreeSet::new();
    for issue in issues.iter() {
        let mut entities = BTreeSet::new();
        for e in issue.entities.iter() {
            let id = q_ids
                .get(*e)
                .map_err(|_| SiteGenerationError::BrokenIssueReference(*e))?;
            entities.insert(**id);
        }
        converted_issues.insert(IssueKey {
            entities,
            kind: issue.kind.clone(),
        });
    }

    Ok(SiteProperties {
        name: name.clone(),
        geographic_offset: geographic_offset.clone(),
        filtered_issues: FilteredIssues(converted_issues),
        filtered_issue_kinds: issue_kinds.clone(),
        extension_settings: extension_settings.clone(),
    })
}

fn migrate_relative_paths(
    site: Entity,
    new_path: &PathBuf,
    world: &mut World,
    // In((new_path, site)): In<(&PathBuf, Entity)>,
    // mut assets: Query<(Entity, &mut AssetSource)>,
    // mut default_files: Query<&mut DefaultFile>,
    // mut commands: Commands,
    // child_of: Query<&ChildOf>,
) {
    let old_path = if let Some(mut default_file) = world.get_mut::<DefaultFile>(site) {
        let old_path = default_file.0.clone();
        default_file.0 = new_path.clone();
        old_path
    } else {
        world.entity_mut(site).insert(DefaultFile(new_path.clone()));
        // If there was not already a default file then there is no way to
        // migrate relative paths because they had no reference path to actually
        // be relative to.
        return;
    };

    let mut state: SystemState<(Query<(Entity, &mut AssetSource)>, Query<&ChildOf>)> =
        SystemState::new(world);

    let (mut assets, child_of) = state.get_mut(world);

    for (mut e, mut source) in &mut assets {
        let asset_entity = e;
        if !source.is_local_relative() {
            continue;
        }

        loop {
            if e == site {
                if source.migrate_relative_path(&old_path, new_path).is_err() {
                    error!(
                        "Failed to migrate relative path for {asset_entity:?}: {:?}",
                        *source,
                    );
                    break;
                }
            }

            if let Ok(child_of) = child_of.get(e) {
                e = child_of.parent();
            } else {
                break;
            }
        }
    }
}

fn generate_model_descriptions(
    site: Entity,
    world: &mut World,
) -> Result<BTreeMap<u32, ModelDescriptionBundle>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<
            (
                &SiteID,
                &NameInSite,
                &ModelProperty<AssetSource>,
                &ModelProperty<IsStatic>,
                &ModelProperty<Scale>,
            ),
            (With<ModelMarker>, With<Group>, Without<Pending>),
        >,
        Query<&Children>,
    )> = SystemState::new(world);
    let (model_descriptions, children) = state.get(world);

    let mut res = BTreeMap::<u32, ModelDescriptionBundle>::new();
    if let Ok(children) = children.get(site) {
        for child in children.iter() {
            if let Ok((site_id, name, source, is_static, scale)) = model_descriptions.get(child) {
                let desc_bundle = ModelDescriptionBundle {
                    name: name.clone(),
                    source: source.clone(),
                    is_static: is_static.clone(),
                    scale: scale.clone(),
                    ..Default::default()
                };
                res.insert(site_id.0, desc_bundle);
            }
        }
    }
    Ok(res)
}

fn generate_robots(
    site: Entity,
    world: &mut World,
) -> Result<BTreeMap<u32, Robot>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<(&SiteID, &ModelProperty<Robot>), (With<ModelMarker>, With<Group>, Without<Pending>)>,
        Query<&Children>,
    )> = SystemState::new(world);
    let (robots, children) = state.get(world);

    let mut res = BTreeMap::<u32, Robot>::new();
    if let Ok(children) = children.get(site) {
        for child in children.iter() {
            if let Ok((site_id, robot_property)) = robots.get(child) {
                let mut robot = robot_property.0.clone();
                // Remove any invalid properties
                robot.properties.retain(|k, _| !k.is_empty());
                res.insert(site_id.0, robot);
            }
        }
    }
    Ok(res)
}

fn generate_model_instances(
    site: Entity,
    world: &mut World,
) -> Result<BTreeMap<u32, Parented<u32, ModelInstance<u32>>>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<(&SiteID, &ExportWith), (With<ModelMarker>, With<Group>, Without<Pending>)>,
        Query<
            (Entity, &SiteID, &NameInSite, &Pose, &Affiliation<Entity>),
            (With<ModelMarker>, Without<Group>, Without<Pending>),
        >,
        Query<(Entity, &SiteID), With<LevelElevation>>,
        Query<&ChildOf>,
    )> = SystemState::new(world);
    let (model_descriptions, model_instances, levels, child_of) = state.get(world);

    let mut site_levels_ids = HashMap::<Entity, u32>::new();
    for (level_entity, site_id) in levels.iter() {
        if child_of
            .get(level_entity)
            .is_ok_and(|co| co.parent() == site)
        {
            site_levels_ids.insert(level_entity, site_id.0);
        }
    }
    // Store model instance data in a HashMap for later access with mutable World
    let mut model_instances_data = HashMap::<
        Entity,
        (
            SiteID,
            NameInSite,
            Pose,
            u32,
            Option<SiteID>,
            HashMap<String, serde_json::Value>,
        ),
    >::new();
    for (instance_entity, instance_id, instance_name, instance_pose, instance_affiliation) in
        model_instances.iter()
    {
        let Some(level_id) = child_of
            .get(instance_entity)
            .ok()
            .map(|co| site_levels_ids.get(&co.parent()).copied())
            .flatten()
        else {
            error!("Unable to find parent for instance [{}]", instance_name.0);
            continue;
        };
        let (description_id, description_export) = instance_affiliation
            .0
            .and_then(|e| model_descriptions.get(e).ok())
            .unzip();

        model_instances_data.insert(
            instance_entity,
            (
                instance_id.clone(),
                instance_name.clone(),
                instance_pose.clone(),
                level_id.clone(),
                description_id.cloned(),
                description_export
                    .map(|e| e.0.clone())
                    .unwrap_or(HashMap::new()),
            ),
        );
    }

    let mut res = BTreeMap::<u32, Parented<u32, ModelInstance<u32>>>::new();
    for (entity, (id, name, pose, level_id, description_id, description_export)) in
        model_instances_data.iter()
    {
        let mut export_data = HashMap::<String, sdformat::XmlElement>::new();
        for (label, value) in description_export.iter() {
            if let Some(data) = world
                .resource_scope::<ExportHandlers, Option<sdformat::XmlElement>>(
                    move |world, mut export_handlers| {
                        if let Some(export_handler) = export_handlers.get_mut(label) {
                            export_handler.export(*entity, value.clone(), world)
                        } else {
                            None
                        }
                    },
                )
            {
                export_data.insert(label.clone(), data);
            }
        }
        let model_instance = ModelInstance::<u32> {
            name: name.clone(),
            pose: pose.clone(),
            description: Affiliation(description_id.map(|d| d.0)),
            export_data: ExportData(export_data),
            ..Default::default()
        };
        res.insert(
            id.0,
            Parented {
                parent: *level_id,
                bundle: model_instance,
            },
        );
    }
    Ok(res)
}

fn generate_scenarios(
    site: Entity,
    world: &mut World,
) -> Result<BTreeMap<u32, Scenario<u32>>, SiteGenerationError> {
    let mut state: SystemState<(
        Query<(
            Entity,
            &ScenarioModifiers<Entity>,
            &NameInSite,
            &SiteID,
            &Affiliation<Entity>,
        )>,
        Query<&SiteID, Without<Pending>>,
        Query<
            (
                Option<&Modifier<Pose>>,
                Option<&Modifier<Inclusion>>,
                Option<&Modifier<OnLevel<Entity>>>,
            ),
            With<Affiliation<Entity>>,
        >,
        Query<
            (Option<&Modifier<Inclusion>>, Option<&Modifier<TaskParams>>),
            With<Affiliation<Entity>>,
        >,
        Query<&Children>,
    )> = SystemState::new(world);
    let (scenarios, site_id, instance_modifiers, task_modifiers, children) = state.get(world);
    let mut res = BTreeMap::<u32, Scenario<u32>>::new();

    if let Ok(site_children) = children.get(site) {
        for site_child in site_children.iter() {
            if let Ok((entity, ..)) = scenarios.get(site_child) {
                let mut queue = vec![entity];

                while let Some(scenario) = queue.pop() {
                    if let Ok((_, scenario_modifiers, name, scenario_id, parent_scenario)) =
                        scenarios.get(scenario)
                    {
                        res.insert(
                            scenario_id.0,
                            Scenario {
                                instances: scenario_modifiers
                                    .iter()
                                    .filter_map(|(e_element, e_modifier)| {
                                        let Ok((pose, inclusion, on_level)) =
                                            instance_modifiers.get(*e_modifier)
                                        else {
                                            return Some(Err(SiteGenerationError::BrokenModifier(
                                                *e_modifier,
                                            )));
                                        };

                                        let on_level = match on_level
                                            .map(|l| **l)
                                            .and_then(|level| level.0)
                                        {
                                            Some(e) => Some({
                                                match site_id.get(e) {
                                                    Ok(id) => id.0,
                                                    Err(_) => return Some(Err(SiteGenerationError::BrokenLevelReference(e))),
                                                }
                                            }),
                                            None => None,
                                        };

                                        let modifier = InstanceModifier {
                                            pose: pose.map(|p| **p),
                                            inclusion: inclusion.map(|i| **i),
                                            on_level,
                                        };

                                        if modifier.is_default() {
                                            return None;
                                        }

                                        // Currently sub-assets such as visual and collision geometries
                                        // are automatically being assigned pose and inclusion modifiers
                                        // but we do not allow those to change, and we do not save them.
                                        // Those sub-assets do not have their own Site IDs, so we filter
                                        // them out here.
                                        //
                                        // TODO(@mxgrey): Figure out a setup that will prevent sub-assets
                                        // from having modifiers at all.
                                        let element_id =
                                            site_id.get(*e_element).map(|id| id.0).ok()?;

                                        Some(Ok((element_id, modifier)))
                                    })
                                    .collect::<Result<_, _>>()?,
                                tasks: scenario_modifiers
                                    .iter()
                                    .filter_map(|(e_element, e_modifier)| {
                                        let Ok((inclusion, task_params)) =
                                            task_modifiers.get(*e_modifier)
                                        else {
                                            return Some(Err(SiteGenerationError::BrokenModifier(
                                                *e_modifier,
                                            )));
                                        };

                                        if task_params.is_none() {
                                            // This is not a task modifier
                                            return None;
                                        }

                                        let modifier = TaskModifier {
                                            inclusion: inclusion.map(|i| **i),
                                            params: task_params.map(|p| (**p).clone()),
                                        };

                                        let Ok(id) = site_id.get(*e_element).map(|id| id.0) else {
                                            // Every task element must have a Site ID. If it is
                                            // missing, that implies an error has occurred.
                                            return Some(Err(SiteGenerationError::MissingSiteID(
                                                *e_element,
                                            )));
                                        };

                                        Some(Ok((id, modifier)))
                                    })
                                    .collect::<Result<_, _>>()?,
                                properties: ScenarioBundle {
                                    name: name.clone(),
                                    parent_scenario: match parent_scenario.0 {
                                        Some(parent) => {
                                            let parent_id = scenarios
                                                .get(parent)
                                                .map(|(_, _, _, id, _)| id.0)
                                                .map_err(|_| {
                                                    SiteGenerationError::MissingSiteID(parent)
                                                })?;
                                            Affiliation(Some(parent_id))
                                        }
                                        None => Affiliation(None),
                                    },
                                    // ScenarioModifiers are not serialized
                                    scenario_modifiers: ScenarioModifiers::default(),
                                },
                            },
                        );
                    }
                }
            }
        }
    }
    info!("Added scenarios: {:?}", res.len());
    Ok(res)
}

fn generate_tasks(
    site: Entity,
    world: &mut World,
) -> Result<BTreeMap<u32, Task>, SiteGenerationError> {
    let mut state: SystemState<(Query<(&SiteID, &Task), Without<Pending>>, Query<&Children>)> =
        SystemState::new(world);
    let (tasks, children) = state.get(world);
    let mut res = BTreeMap::<u32, Task>::new();
    if let Ok(children) = children.get(site) {
        for child in children.iter() {
            if let Ok((site_id, task)) = tasks.get(child) {
                res.insert(site_id.0, task.clone());
            }
        }
    }
    Ok(res)
}

pub fn generate_site(
    world: &mut World,
    site: Entity,
) -> Result<rmf_site_format::Site, SiteGenerationError> {
    assemble_edited_drawing(world);

    assign_site_ids(world, site)?;
    let anchors = collect_site_anchors(world, site);
    let levels = generate_levels(world, site)?;
    let lifts = generate_lifts(world, site)?;
    let fiducials = generate_fiducials(world, site)?;
    let fiducial_groups = generate_fiducial_groups(world, site)?;
    let textures = generate_texture_groups(world, site)?;
    let nav_graphs = generate_nav_graphs(world, site)?;
    let mutex_groups = generate_mutex_groups(world, site)?;
    let lanes = generate_lanes(world, site)?;
    let locations = generate_locations(world, site)?;
    let graph_ranking = generate_graph_rankings(world, site)?;
    let properties = generate_site_properties(world, site)?;
    let model_descriptions = generate_model_descriptions(site, world)?;
    let robots = generate_robots(site, world)?;
    let model_instances = generate_model_instances(site, world)?;
    let scenarios = generate_scenarios(site, world)?;
    let tasks = generate_tasks(site, world)?;

    let extensions = world.resource_scope::<ExtensionHooks, _>(|world, mut hooks| {
        let mut extensions = Extensions::default();
        for (extension, hook) in &mut hooks.hooks {
            let settings = properties
                .extension_settings
                .get(extension)
                .unwrap_or(&hook.default_settings);

            if settings.skip_during_save {
                continue;
            }

            if let Some(saving) = &mut hook.saving {
                let r = saving.run(SavingArgs { site }, world);
                saving.apply_deferred(world);

                match r {
                    Ok(data) => {
                        extensions.data.insert(Arc::clone(extension), data);
                    }
                    Err(error) => {
                        if settings.prevent_saving_on_error {
                            return Err(SiteGenerationError::ExtensionError {
                                extension: Arc::clone(extension),
                                error,
                            });
                        } else {
                            warn!("Error in extension [{extension}] while saving: {error}");
                        }
                    }
                }
            }
        }

        Ok(extensions)
    })?;

    disassemble_edited_drawing(world);
    return Ok(Site {
        format_version: rmf_site_format::SemVer::default(),
        anchors,
        properties,
        levels,
        lifts,
        fiducials,
        fiducial_groups,
        textures,
        navigation: Navigation {
            guided: Guided {
                graphs: nav_graphs,
                ranking: graph_ranking,
                lanes,
                locations,
                mutex_groups,
            },
        },
        model_descriptions,
        robots,
        model_instances,
        scenarios,
        tasks,
        extensions,
    });
}

pub fn save_site(world: &mut World) {
    let save_events: Vec<_> = world.resource_mut::<Events<SaveSite>>().drain().collect();
    for save_event in save_events {
        let mut new_path = save_event.to_location;
        let path_str = match new_path.to_str() {
            Some(s) => s,
            None => {
                error!("Unable to save file: Invalid path [{new_path:?}]");
                continue;
            }
        };
        match save_event.format {
            ExportFormat::Default => {
                if path_str.ends_with(".building.yaml") {
                    warn!("Detected old file format, converting to new format");
                    new_path = path_str.replace(".building.yaml", ".site.json").into();
                } else if !path_str.ends_with(".site.json") {
                    info!("Appending .site.json to {}", new_path.display());
                    new_path = new_path.with_extension("site.json");
                }

                info!("Saving to {}", new_path.display());
                let f = match std::fs::File::create(new_path.clone()) {
                    Ok(f) => f,
                    Err(err) => {
                        error!("Unable to save file: {err}");
                        continue;
                    }
                };

                let old_default_path = world.get::<DefaultFile>(save_event.site).cloned();
                migrate_relative_paths(save_event.site, &new_path, world);

                let site = match generate_site(world, save_event.site) {
                    Ok(site) => site,
                    Err(err) => {
                        error!("Unable to compile site: {err}");
                        continue;
                    }
                };

                match site.to_writer_json(f) {
                    Ok(()) => {
                        info!("Save successful");
                    }
                    Err(err) => {
                        if let Some(old_default_path) = old_default_path {
                            world.entity_mut(save_event.site).insert(old_default_path);
                        }
                        error!("Save failed: {err}");
                        continue;
                    }
                }

                // Indicate that the site has not changed since the last save.
                // Note that we will need to change this logic when we start
                // supporting multiple sites being open in one app.
                world.resource_mut::<SiteChanged>().0 = false;
            }
            ExportFormat::Sdf => {
                // TODO(luca) reduce code duplication with default exporting

                // Make sure to generate the site before anything else, because
                // generating the site will ensure that all items are assigned a
                // SiteID, and the SDF export process will not work correctly if
                // any are unassigned.
                let site = match generate_site(world, save_event.site) {
                    Ok(site) => site,
                    Err(err) => {
                        error!("Unable to compile site: {err}");
                        continue;
                    }
                };

                info!("Saving to {}", new_path.display());
                if !new_path.exists() {
                    if let Err(e) = std::fs::create_dir_all(&new_path) {
                        error!("Unable to create folder {}: {e}", new_path.display());
                        continue;
                    }
                } else {
                    if !new_path.is_dir() {
                        error!("SDF can only be exported to a folder");
                        continue;
                    }
                }
                let mut sdf_path = new_path.clone();
                sdf_path.push(&site.properties.name.0);
                sdf_path.set_extension("world");
                let f = match std::fs::File::create(&sdf_path) {
                    Ok(f) => f,
                    Err(err) => {
                        error!("Unable to save file {}: {err}", sdf_path.display());
                        continue;
                    }
                };

                let mut meshes_dir = new_path.clone();
                meshes_dir.push("meshes");
                if let Err(e) = std::fs::create_dir_all(&meshes_dir) {
                    error!("Unable to create folder {}: {e}", meshes_dir.display());
                    continue;
                }
                if let Err(e) = collect_site_meshes(world, save_event.site, &meshes_dir) {
                    error!("Unable to collect site meshes: {e}");
                    continue;
                }

                migrate_relative_paths(save_event.site, &sdf_path, world);
                let sdf = match site.to_sdf() {
                    Ok(sdf) => sdf,
                    Err(err) => {
                        error!("Unable to convert site to sdf: {err}");
                        continue;
                    }
                };
                let config = yaserde::ser::Config {
                    perform_indent: true,
                    write_document_declaration: true,
                    ..Default::default()
                };
                if let Err(e) = yaserde::ser::serialize_with_writer(&sdf, f, &config) {
                    error!("Failed serializing site to sdf: {e}");
                    continue;
                }
            }
            ExportFormat::NavGraph => {
                let site = match generate_site(world, save_event.site) {
                    Ok(site) => site,
                    Err(err) => {
                        error!("Unable to compile site: {err}");
                        continue;
                    }
                };

                for (name, nav_graph) in legacy::nav_graph::NavGraph::from_site(&site) {
                    let graph_file = new_path.clone().join(name + ".nav.yaml");
                    info!(
                        "Saving legacy nav graph to {}",
                        graph_file.to_str().unwrap_or("<failed to render??>")
                    );
                    let f = match std::fs::File::create(graph_file) {
                        Ok(f) => f,
                        Err(err) => {
                            error!("Unable to save nav graph: {err}");
                            continue;
                        }
                    };
                    if let Err(err) = serde_yaml::to_writer(f, &nav_graph) {
                        error!("Failed to save nav graph: {err}");
                    }
                }

                info!(
                    "Saving all site nav graphs to {}",
                    new_path.to_str().unwrap_or("<failed to render??>")
                );
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    use std::{path::Path, time::Duration};
    use testdir::testdir;

    #[test]
    #[cfg(not(target_arch = "wasm32"))]
    fn headless_load_and_save_roundtrip() {
        let target_test_dir = testdir!();
        let rmf_site_editor_manifest_dir_str = std::env::var("CARGO_MANIFEST_DIR").unwrap();
        // Go from crates/rmf_site_editor to workspace root directory
        let workspace_dir = Path::new(&rmf_site_editor_manifest_dir_str)
            .parent()
            .unwrap()
            .parent()
            .unwrap();

        let assets_dir = workspace_dir.join("assets");
        let source = assets_dir.join("demo_maps").join("test.site.json");

        let test_site_dir = "sites";
        let original = target_test_dir
            .join(test_site_dir)
            .join("test_original.site.json");
        let destination = target_test_dir
            .join(test_site_dir)
            .join("test_destination.site.json");

        std::fs::create_dir_all(target_test_dir.join(test_site_dir)).unwrap();

        // Copy the source file into the test directory.
        //
        // Later we will do a diff between the two files to make sure they are
        // exactly equal, but saving to a new folder can alter the relative
        // paths within the site. To make sure the exported copy is exactly the
        // same as the original, the copy must be saved to the same folder as
        // the original. However we do not want tests to produce files in a
        // source folder, so we copy the source file into the target directory
        // and then do a roundtrip into the same directory.
        std::fs::copy(&source, &original).unwrap();

        #[cfg(unix)]
        {
            // Create a symlink to avoid a slew of error log messages while loading.
            // We can ignore the result of this, because the test should still pass
            // even if the symlinking doesn't work, we'll just see some noisy error
            // logs in stdout.
            let _ = std::os::unix::fs::symlink(
                assets_dir.join("models"),
                target_test_dir.join("models"),
            );
            let _ = std::os::unix::fs::symlink(
                assets_dir.join("drawings"),
                target_test_dir.join("drawings"),
            );
        }

        let destination = destination.to_str().unwrap().to_owned();

        let mut app = App::new();
        app.insert_resource(Autoload::file(original.clone(), None))
            .add_plugins(SiteEditor::default().save_as_path(Some(destination.clone())))
            .add_plugins(TestTimeoutPlugin::new(Duration::from_secs(10)));

        // Run until the file is saved or until the timeout occurs
        app.run();

        assert!(std::fs::exists(&destination).unwrap());

        #[cfg(not(target_os = "windows"))]
        {
            // For non-windows we can just compare the new and old files directly
            let original = original.to_str().unwrap().to_owned();
            assert!(file_diff::diff(&original, &destination));

            let source = source.to_str().unwrap().to_owned();
            assert!(file_diff::diff(&source, &destination));
        }

        #[cfg(target_os = "windows")]
        {
            use std::os::windows::prelude::*;
            // Windows uses different characters to represent newlines and path
            // separators, so we cannot do a 1-to-1 comparison between the original
            // and the generated file. We will simply check whether the new file
            // is within 10% the size of the original file size since the differences
            // incurred by these format changes should not be too significant.
            let original_file_size = std::fs::metadata(&original).unwrap().file_size() as f64;
            let destination_file_size = std::fs::metadata(&destination).unwrap().file_size() as f64;
            let difference_ratio =
                f64::abs(original_file_size - destination_file_size) / original_file_size;

            assert!(
                difference_ratio <= 0.1,
                " - Original file size: {original_file_size} \
                \n - Destination file size: {destination_file_size} \
                \n - Destination file contents:\n{}",
                std::fs::read_to_string(&destination).unwrap(),
            );
        }

        // At the end of a successful test we should delete the testdir.
        // This will avoid accumulating disk space with each test run.
        let _ = std::fs::remove_dir_all(target_test_dir);
    }

    pub(crate) struct TestTimeoutPlugin {
        max_duration: Duration,
    }

    impl TestTimeoutPlugin {
        pub(crate) fn new(max_duration: Duration) -> Self {
            Self { max_duration }
        }
    }

    impl Default for TestTimeoutPlugin {
        fn default() -> Self {
            TestTimeoutPlugin {
                max_duration: Duration::from_secs(30),
            }
        }
    }

    impl Plugin for TestTimeoutPlugin {
        fn build(&self, app: &mut App) {
            app.insert_resource(TestTimeout {
                max_duration: self.max_duration,
            })
            .add_systems(Update, test_timeout);
        }
    }

    #[derive(Resource)]
    struct TestTimeout {
        max_duration: Duration,
    }

    fn test_timeout(time: Res<Time>, timeout: Res<TestTimeout>, mut exit: EventWriter<AppExit>) {
        if time.elapsed() > timeout.max_duration {
            exit.write(AppExit::error());
        }
    }
}