screenplay-doc-parser-rs 0.1.10

Tools to parse Screenplay-formatted documents into semantically-typed structs.
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
use std::{
    collections::{HashMap, HashSet},
};

use crate::screenplay_document::{self, SPType};

// ------------ Get LOCATIONs...
// TODO: Filter Locations by Characters speaking in location...
// All "get location" or "filter location" funcs return a vec of &LocationID.

///
/// Returns an Optional Vec of &LocationID, which make up the path to this leaf node, in order from root to leaf.
///
pub fn get_full_location_path_for_leaf_node<'a>(
    screenplay_doc: &'a crate::screenplay_document::ScreenplayDocument,
    location: &'a screenplay_document::LocationID,
) -> Option<(Vec<&'a screenplay_document::LocationID>)> {
    let mut current_location_node_id: &screenplay_document::LocationID = location;
    let mut id_path: Vec<&screenplay_document::LocationID> = vec![current_location_node_id];
    loop {
        let Some(location_node) = screenplay_doc.locations.get(current_location_node_id) else {
            return None; // ERROR: BAD LOCATION ID!
        };
        let Some(superlocation) = &location_node.superlocation else {
            break; // done pushing to path vec
        };
        id_path.push(superlocation);
        current_location_node_id = superlocation;
    }

    if id_path.is_empty() {
        return None;
    };

    id_path.reverse();
    Some(id_path)
}

/// Returns the full Location path as a string (i.e. the full scene heading)
/// for a given Location leaf node.
pub fn get_full_string_for_location_path<'a>(
    screenplay_doc: &'a crate::screenplay_document::ScreenplayDocument,
    location_leaf_node: &'a screenplay_document::LocationID,
) -> Option<String> {
    let location_id_path =
        get_full_location_path_for_leaf_node(screenplay_doc, location_leaf_node)?;

    let mut path_string = String::new();
    for id in location_id_path {
        let Some(location_node) = screenplay_doc.locations.get(id) else {
            return None; // ERROR: BAD LOCATION ID!
        };
        if !path_string.is_empty() {
            path_string.push_str(" - ");
        }
        path_string.push_str(&location_node.string);
    }
    if path_string.is_empty() {
        return None; // ERROR: ???
    }

    Some(path_string)
}

/// Determines if a "location path" exists.
///
/// Returns `None` if nothing matches the root of the path.
///
/// Returns `Some((&LocationID, Vec<String>))` if a partial match is found.
/// The `&LocationID` is the last valid location the path matched.
/// The `Vec<String>` is the remaining subpath, which does not yet exist as `LocationNode`s.
///
/// If the full path exists, the `Vec<String>` will be empty.
///
/// The caller can afterwards handle creating the rest of the Location path,
/// and appending a new LocationID to the sublocations field
///
///
pub fn location_path_exists<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    path: &[String],
) -> Option<(&'a screenplay_document::LocationID, Vec<String>)> {
    if path.is_empty() {
        return None;
    }

    let path_root = &path[0];

    for (id, location) in &screenplay_document.locations {
        if location.string == *path_root {
            if path.len() == 1 {
                return Some((id, Vec::new()));
            }
            if path.len() > 1 && location.sublocations.is_empty() {
                return Some((id, Vec::from(&path[1..])));
            }

            return location.subpath_exists(&id, &path[1..], screenplay_document);
        }
    }

    None
}

pub fn get_locations_with_matching_str<'a>(
    screenplay_document: &'a screenplay_document::ScreenplayDocument,
    location_string: &'a String,
) -> Option<Vec<&'a screenplay_document::LocationID>> {
    let mut loc_id_vec: Vec<&screenplay_document::LocationID> = Vec::new();
    for (id, location) in &screenplay_document.locations {
        if location.string == *location_string {
            loc_id_vec.push(id);
        }
    }
    if loc_id_vec.is_empty() {
        return None;
    }
    Some(loc_id_vec)
}

/// Gets the Location ID of the root in this location path, from a given LocationID.
///
/// Returns `'None` if the LocationID is invalid.
pub fn get_location_root_for_node<'a>(
    screenplay_document: &'a screenplay_document::ScreenplayDocument,
    location_id: &'a screenplay_document::LocationID,
) -> Option<&'a screenplay_document::LocationID> {
    let location = screenplay_document.locations.get(location_id)?;
    let Some(superlocation) = &location.superlocation else {
        return Some(location_id);
    };
    return get_location_root_for_node(screenplay_document, &superlocation);
}

pub fn get_all_location_leafs<'a>(
    screenplay_document: &'a screenplay_document::ScreenplayDocument,
    location_id: &'a screenplay_document::LocationID,
) -> Option<Vec<&'a screenplay_document::LocationID>> {
    let Some(location) = screenplay_document.locations.get(location_id) else {
        return None;
    };
    if location.sublocations.is_empty() {
        return Some(vec![location_id]);
    }
    let mut location_id_set: HashSet<&screenplay_document::LocationID> = HashSet::new();
    for sublocation_id in &location.sublocations {
        let Some(subset) = get_all_location_leafs(screenplay_document, &sublocation_id) else {
            continue;
        };
        location_id_set.extend(subset);
    }
    if location_id_set.is_empty() {
        return None;
    }
    Some(location_id_set.iter().copied().collect())
}

pub fn filter_locations_by_character_speaking<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    locations_to_filter: Vec<&'a screenplay_document::LocationID>,
    character: &'a screenplay_document::Character,
) -> Option<Vec<&'a screenplay_document::LocationID>> {
    let scenes_with_character =
        get_all_scenes_with_character_speaking(screenplay_document, character)?;
    let mut filtered_locations: HashSet<&screenplay_document::LocationID> = HashSet::new();

    scenes_with_character
        .iter()
        .map(|(s, scn)| &scn.story_locations)
        .for_each(|lcv| {
            //TODO: filter this by ones that
            lcv.iter().for_each(|lc| {
                if locations_to_filter.contains(&lc) {
                    filtered_locations.insert(lc);
                }
            });
        });
    if filtered_locations.is_empty() {
        return None;
    }

    Some(filtered_locations.iter().copied().collect())
}

pub fn get_all_locations_with_character_speaking<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    character: &'a screenplay_document::Character,
) -> Option<Vec<&'a screenplay_document::LocationID>> {
    let all_locations = &screenplay_document.locations;
    let all_locations_vec: Vec<_> = all_locations.iter().map(|(id, scn)| id).collect();

    filter_locations_by_character_speaking(screenplay_document, all_locations_vec, character)
}

pub fn filter_locations_by_page_idx<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    locations: Vec<&'a screenplay_document::LocationID>,
    page: usize,
) -> Option<Vec<&'a screenplay_document::LocationID>> {
    let Some(locations_on_page) = get_all_locations_on_page_by_index(screenplay_document, page)
    else {
        return None;
    };

    let mut filtered_locations: Vec<&screenplay_document::LocationID> = Vec::new();

    for loc in locations {
        if locations_on_page.contains(&loc) {
            filtered_locations.push(loc);
        }
    }

    if filtered_locations.is_empty() {
        return None;
    }
    Some(filtered_locations)
}

pub fn get_all_locations_on_page_by_index(
    screenplay_document: &crate::screenplay_document::ScreenplayDocument,
    page: usize,
) -> Option<Vec<&screenplay_document::LocationID>> {
    let mut locations_on_page: HashSet<&screenplay_document::LocationID> = HashSet::new();
    let scenes = get_all_scenes_on_page_by_index(screenplay_document, page)?;
    scenes
        .iter()
        .filter_map(|(scene_id, scene)| {
            if scene.story_locations.is_empty() {
                return None;
            }

            Some(&scene.story_locations)
        })
        .for_each(|s| {
            s.iter().for_each(|l| {
                locations_on_page.insert(l);
            })
        });

    if locations_on_page.is_empty() {
        return None;
    }
    Some(locations_on_page.iter().copied().collect())
}

// ------------ Get LINEs...
// All "get_line..." or "filter_lines" funcs should return
// A) HashMap<ScreenplayCoordinate, &Line>
// B) Line

pub fn filter_lines_by_multiple_scenes<'a>(
    screenplay_document: &crate::screenplay_document::ScreenplayDocument,
    lines: &Vec<(screenplay_document::ScreenplayCoordinate, &'a screenplay_document::Line)>,
    scenes_filter: Vec<(&screenplay_document::SceneID, &screenplay_document::Scene)>,
) -> Option<Vec<(screenplay_document::ScreenplayCoordinate, &'a screenplay_document::Line)>> {
    if scenes_filter.is_empty() {
        //panic!("EMPTY FILTER!");
        return None;
    }

    let mut scn_filtered_lines: Vec<
        (screenplay_document::ScreenplayCoordinate,
        &screenplay_document::Line)
    > = Vec::new();

    lines
        .iter()
        .filter(|(coord, ln)| {
            let Some((scene_id, scene)) =
                get_scene_for_screenplay_coordinate(screenplay_document, &coord)
            else {
                return false;
            };
            for (fscn_id, _) in &scenes_filter {
                if *fscn_id == scene_id {
                    return true;
                }
            }
            false
        })
        .for_each(|(coord, line)| {
            scn_filtered_lines.push((coord.clone(), line));
        });

    if scn_filtered_lines.is_empty() {
        return None;
    }
    if scn_filtered_lines.len() == lines.len() {
        //panic!("DIDN'T FILTER ANY LINES AT ALL!!!");
    }

    //println!("\nDURATION TO FILTER BY SCENES: {:?}", bench_res);
    Some(scn_filtered_lines)
}

pub fn filter_lines_by_multiple_locations<'a>(
    screenplay_doc: &'a screenplay_document::ScreenplayDocument,
    lines: &Vec<(screenplay_document::ScreenplayCoordinate, &'a screenplay_document::Line)>,
    locations_filter: Vec<&screenplay_document::LocationID>,
) -> Option<Vec<(screenplay_document::ScreenplayCoordinate, &'a screenplay_document::Line)>> {
    let Some(scenes_ordered) = get_all_scenes_ordered(screenplay_doc) else {
        return None;
    };
    let Some(scenes_filtered) =
        filter_scenes_by_locations(screenplay_doc, scenes_ordered, locations_filter)
    else {
        return None;
    };
    return filter_lines_by_multiple_scenes(screenplay_doc, lines, scenes_filtered);
}



pub fn get_all_lines_of_dialogue_for_character<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    character: &'a screenplay_document::Character,
) -> Option<Vec<(screenplay_document::ScreenplayCoordinate, &'a screenplay_document::Line)>> {
    let mut lines_with_coords: Vec<(
        screenplay_document::ScreenplayCoordinate,
        &screenplay_document::Line)
    > = Vec::new();
    //let mut lines: Vec<&Line> = Vec::new();
    let mut is_dialogue = false;

    for (p_index, page) in screenplay_document.pages.iter().enumerate() {
        for (l_index, line) in page.lines.iter().enumerate() {
            if line.line_type != Some(SPType::SP_CHARACTER)
                && line.line_type != Some(SPType::SP_DUAL_CHARACTERS)
                && line.line_type != Some(SPType::SP_DIALOGUE)
                && line.line_type != Some(SPType::SP_DUAL_DIALOGUES)
            {
                continue;
            }
            //println!("MIGHT BE CHARACTER OR DIALOGUE");
            if character.is_line(line) {
                //println!("Oh boy!!!! | {:?} | {:?}", line.line_type, line.text_elements);
                is_dialogue = true;
                continue;
            }
            if !is_dialogue {
                continue;
            }
            match line.line_type {
                Some(SPType::SP_DIALOGUE) | Some(SPType::SP_DUAL_DIALOGUES) => {
                    lines_with_coords.push(
                        (screenplay_document::ScreenplayCoordinate {
                            page: p_index,
                            line: l_index,
                            element: None,
                        },
                        line)
                    );
                }
                _ => {
                    is_dialogue = false;
                    continue;
                }
            }
        }
    }
    if lines_with_coords.is_empty() {
        return None;
    }
    Some(lines_with_coords)
}

// ------------ Get CHARACTERS...
// All returns should be Vec<&Character>.
// TODO: Filter Characters by Scenes they speak in,
// Locations they speak in,

pub fn filter_characters_by_scene<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    characters: &Vec<&'a screenplay_document::Character>,
    scene_id: &screenplay_document::SceneID,
) -> Option<Vec<&'a screenplay_document::Character>> {
    let scn = screenplay_document.scenes.get(scene_id)?;
    let mut current_page = scn.start.page;
    let mut characters_in_scene: HashSet<&screenplay_document::Character> = HashSet::new();

    'seeking: loop {
        let Some(page) = screenplay_document.pages.get(current_page) else {
            break 'seeking;
        };
        'lines: for (l_idx, line) in page.lines.iter().enumerate() {
            if (current_page, l_idx) < (scn.start.page, scn.start.line) {
                continue 'lines;
            }
            if line.line_type
                == Some(SPType::SP_SCENE_HEADING(
                    screenplay_document::SceneHeadingElement::Line,
                ))
            {
                if (current_page, l_idx) > (scn.start.page, scn.start.line) {
                    break 'seeking;
                }
            }
            if line.line_type != Some(SPType::SP_CHARACTER) {
                continue 'lines;
            }
            if characters_in_scene.len() == characters.len() {
                break 'seeking;
            }
            for character in characters {
                if character.is_line(line) {
                    characters_in_scene.insert(&character);
                }
            }
        }
        current_page += 1;
    }
    if characters_in_scene.is_empty() {
        return None;
    }

    Some(characters_in_scene.iter().copied().collect())
}

pub fn get_characters_for_scene<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    scene_id: &screenplay_document::SceneID,
) -> Option<Vec<&'a screenplay_document::Character>> {
    let current_characters = &screenplay_document.characters.iter().collect();
    filter_characters_by_scene(screenplay_document, current_characters, scene_id)
}

pub fn filter_characters_by_page_index<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    characters_to_filter: Vec<&'a screenplay_document::Character>,
    page_index: usize,
) -> Option<Vec<&'a screenplay_document::Character>> {
    let mut characters_on_page: HashSet<&screenplay_document::Character> = HashSet::new();

    let Some(page) = screenplay_document.pages.get(page_index) else {
        return None;
    };
    'lines: for line in &page.lines {
        if line.line_type != Some(SPType::SP_CHARACTER) {
            continue 'lines;
        }
        if characters_on_page.len() == characters_to_filter.len() {
            break;
        }
        for character in &characters_to_filter {
            if character.is_line(line) {
                characters_on_page.insert(character);
            }
        }
    }

    if characters_on_page.is_empty() {
        return None;
    }

    Some(characters_on_page.iter().copied().collect())
}

pub fn get_all_characters_on_page_by_index(
    screenplay_document: &crate::screenplay_document::ScreenplayDocument,
    page_index: usize,
) -> Option<Vec<&screenplay_document::Character>> {
    let all_characters = screenplay_document.characters.iter().collect();
    //let characters_as_vec = all_characters
    filter_characters_by_page_index(&screenplay_document, all_characters, page_index)
}

// TODO: This function is wrong and bad???
// the test in the lib.rs just filters the scenes by location
// and then calls get_characters_for_scene,
// so that's what this function should do then...
pub fn filter_characters_by_location<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    characters_to_filter: Vec<&'a screenplay_document::Character>,
    location_id: &'a screenplay_document::LocationID,
) -> Option<Vec<&'a screenplay_document::Character>> {
    let mut characters_at_this_location: HashSet<&screenplay_document::Character> = HashSet::new();
    let Some(scenes_with_location) = get_scenes_with_location(screenplay_document, location_id)
    else {
        return None;
    };
    for (scene_id, _) in scenes_with_location {
        let Some(new_character_matches) =
            filter_characters_by_scene(screenplay_document, &characters_to_filter, scene_id)
        else {
            continue;
        };
        for character_match in new_character_matches {
            if characters_to_filter.contains(&character_match) {
                characters_at_this_location.insert(character_match);
            }
        }
    }
    if characters_at_this_location.is_empty() {
        return None;
    }
    Some(characters_at_this_location.iter().copied().collect())
}

pub fn get_characters_for_location<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    location_id: &'a screenplay_document::LocationID,
) -> Option<Vec<&'a screenplay_document::Character>> {
    let all_characters = screenplay_document.characters.iter().collect();
    let Some(thing) = filter_characters_by_location(
        screenplay_document,
        all_characters,
        location_id,
    ) else {
        return None;
    };
    if thing.is_empty() {
        return None;
    };
    Some(thing)
}

// ------------ Get SCENES...
// TODO: FILTER scenes by character speaking...

pub fn filter_scenes_by_locations<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    scenes_to_filter: Vec<(
        &'a screenplay_document::SceneID,
        &'a screenplay_document::Scene,
    )>,
    locations: Vec<&'a screenplay_document::LocationID>,
) -> Option<
    Vec<(
        &'a screenplay_document::SceneID,
        &'a screenplay_document::Scene,
    )>,
> {
    let mut filtered: Vec<(&screenplay_document::SceneID, &screenplay_document::Scene)> =
        Vec::new();
    for loc in locations {
        let Some(scenes_for_loc) = get_scenes_with_location(screenplay_document, loc) else {
            return None; // Could not find ANY scenes with this location...
        };

        for (scene, _) in scenes_for_loc {
            //println!("----- FILTERING BY LOCATION-SCENE....");
            for (scn_id, scn) in &filtered {
                if *scn_id == scene {
                    continue;
                }
            }
            for (id, scn) in &scenes_to_filter {
                if *id == scene {
                    filtered.push((id, scn));
                }
            }
        }
    }
    if filtered.is_empty() {
        return None;
    }
    Some(filtered)
}


/// Returns a Vec<(&SceneID, &Scene)>, in document order.
///
/// # Example
///
/// ```rust
/// use screenplay_doc_parser_rs::screenplay_document::*;
/// use screenplay_doc_parser_rs::reports;
/// let mut doc = ScreenplayDocument::new();
/// let scene1: Scene = Scene {
///     start: ScreenplayCoordinate {page: 0 as usize, line: 10 as usize, element: None},
///     number: None,
///     revised: false,
///     environment: Environment::Int,
///     story_locations: vec!(LocationID::default()),
///     story_time_of_day: None
/// };
/// let scene2: Scene = Scene {
///     start: ScreenplayCoordinate {page: 1 as usize, line: 5 as usize, element: None},
///     number: None,
///     revised: false,
///     environment: Environment::Int,
///     story_locations: vec!(LocationID::default()),
///     story_time_of_day: None
/// };
/// let id_1 = SceneID::new();
/// let id_2 = SceneID::new();
/// doc.scenes.insert(id_2.clone(), scene2);
/// doc.scenes.insert(id_1.clone(), scene1);
///
/// let sorted = reports::get_all_scenes_ordered(&doc).unwrap();
///
/// assert_eq!(*sorted.get(0).unwrap().0, id_1);
/// assert_eq!(*sorted.get(1).unwrap().0, id_2)
/// ```
///
pub fn get_all_scenes_ordered<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
) -> Option<
    Vec<(
        &'a screenplay_document::SceneID,
        &'a screenplay_document::Scene,
    )>,
> {
    if screenplay_document.scenes.len() == 0 {
        return None;
    }
    if screenplay_document.scenes.len() == 1 {
        return Some(
            screenplay_document
                .scenes
                .iter()
                .map(|(id, scn)| (id, scn))
                .collect(),
        );
    }
    let mut all_scenes: Vec<_> = screenplay_document
        .scenes
        .iter()
        .map(|(a, b)| (a, b))
        .collect();

    all_scenes.sort_by(|(a_id, a_scn), (b_id, b_scn)| {
        (a_scn.start.page, a_scn.start.line).cmp(&(b_scn.start.page, b_scn.start.line))
    });

    return Some(all_scenes);
}

pub fn get_scene_for_screenplay_coordinate<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    checked_coordinate: &screenplay_document::ScreenplayCoordinate,
) -> Option<(
    &'a screenplay_document::SceneID,
    &'a screenplay_document::Scene,
)> {
    let Some(page) = screenplay_document.pages.get(checked_coordinate.page) else {
        //println!("DUCK SAUCE 2: HELP!");
        return None;
    };

    // lines are properly enumerated FORWARDS, then ENTIRE ITERATOR is reversed
    for (line_index, line) in page.lines.iter().enumerate().rev() {
        // if this line is EQUAL or EARLIER THAN the coordinate...
        if line_index > checked_coordinate.line {
            continue;
        }
        let Some(SPType::SP_SCENE_HEADING(screenplay_document::SceneHeadingElement::Line)) =
            line.line_type
        else {
            continue;
        };
        let Some(scn_id) = &line.scene_id else {
            //println!("SCENE HEADING HAS NO SCENE ID!");
            //panic!();
            continue;
        };
        //println!("SUCCESS PART ONE...?");
        let Some(scene_obj) = screenplay_document.scenes.get(scn_id) else {
            return None; // ERROR: BAD SCENE ID!
        };
        return Some((scn_id, scene_obj));
    }
    // couldn't find the scene on this page. try the previous page...
    // recursively check all previous pages

    let Some(previous_page_idx) = checked_coordinate.page.checked_sub(1) else {
        return None;
    };
    let Some(previous_page) = screenplay_document.pages.get(previous_page_idx) else {
        return None;
    };
    let Some(last_line_idx) = previous_page.lines.len().checked_sub(1) else {
        return None;
    };
    let last_page_last_line_coord = screenplay_document::ScreenplayCoordinate {
        page: previous_page_idx,
        line: last_line_idx,
        element: None,
    };
    //println!("CHECKING RECURSIVELY FOR SCENE!");
    //println!("COORDS OF PREVIOUS_PAGE: {:?}", last_page_last_line_coord);

    let Some(scene) =
        get_scene_for_screenplay_coordinate(screenplay_document, &last_page_last_line_coord)
    else {
        //println!("RECURSIVE CHECK FAILED!");
        //panic!("DUCK SAUCE");
        return None;
    };
    //println!("SUCCESS PART TWO...?");
    return Some(scene);
}

// This may be a contender for optimization....
pub fn get_scenes_from_range<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    start: &screenplay_document::ScreenplayCoordinate,
    end: &screenplay_document::ScreenplayCoordinate,
) -> Option<
    Vec<(
        &'a screenplay_document::SceneID,
        &'a screenplay_document::Scene,
    )>,
> {
    if (screenplay_document.pages.get(start.page)).is_none() {
        return None;
    }
    if screenplay_document.pages.get(end.page).is_none() {
        return None;
    }

    let mut scenes_in_range: Vec<(&screenplay_document::SceneID, &screenplay_document::Scene)> =
        Vec::new();
    for page_index in start.page..=end.page {
        let Some(page) = screenplay_document.pages.get(page_index) else {
            continue;
        };

        for (l_idx, line) in page.lines.iter().enumerate() {
            if page_index == start.page && l_idx < start.line {
                continue;
            } else if page_index == end.page && l_idx > end.line {
                break;
            }
            // TODO: keep track of CURRENT line type and PREVIOUS linetype
            // if PREV line was a SCENE HEADING, just continue
            // if CURRENT LINE is NOT a scene heading, AND we already HAVE an entry in the scenes vec, CONTINUE
            if Some(SPType::SP_SCENE_HEADING(
                screenplay_document::SceneHeadingElement::Line,
            )) != line.line_type
                && !scenes_in_range.is_empty()
            {
                continue;
            }
            let Some((scene_id, scene)) = get_scene_for_screenplay_coordinate(
                screenplay_document,
                &screenplay_document::ScreenplayCoordinate {
                    page: page_index,
                    line: l_idx,
                    element: None,
                },
            ) else {
                continue;
            };
            for (scn_id, scn) in &scenes_in_range {
                if scene_id == *scn_id {
                    continue;
                }
            }

            scenes_in_range.push((scene_id, scene));
        }
    }
    if scenes_in_range.is_empty() {
        return None;
    }

    Some(scenes_in_range)
}

pub fn filter_scenes_by_page_index<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    scenes_to_filter: Vec<(
        &'a screenplay_document::SceneID,
        &'a screenplay_document::Scene,
    )>,
    page_index: usize,
) -> Option<
    Vec<(
        &'a screenplay_document::SceneID,
        &'a screenplay_document::Scene,
    )>,
> {
    let page = screenplay_document.pages.get(page_index)?;
    if page.lines.is_empty() {
        return None;
    }
    let last_line_idx = page.lines.len().checked_sub(1).unwrap_or(0);
    let start = screenplay_document::ScreenplayCoordinate {
        page: page_index,
        line: 0,
        element: None,
    };
    let end = screenplay_document::ScreenplayCoordinate {
        page: page_index,
        line: last_line_idx,
        element: None,
    };
    let Some(scenes_on_page) = get_scenes_from_range(screenplay_document, &start, &end) else {
        return None;
    };

    let filtered: Vec<_> = scenes_on_page
        .iter()
        .filter(|s| scenes_to_filter.contains(&s))
        .copied()
        .collect();

    Some(filtered)
}

pub fn get_all_scenes_on_page_by_index(
    screenplay_document: &crate::screenplay_document::ScreenplayDocument,
    page_index: usize,
) -> Option<Vec<(&screenplay_document::SceneID, &screenplay_document::Scene)>> {
    let Some(all_scenes_ordered) = get_all_scenes_ordered(screenplay_document) else {
        return None;
    };
    filter_scenes_by_page_index(screenplay_document, all_scenes_ordered, page_index)
}

// this one seems inefficient....
// could skip lines of loop if we already pushed the current valid scene, and skip
// to the first line of the next scene to check each time...
pub fn get_all_scenes_with_character_speaking<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    character: &screenplay_document::Character,
) -> Option<
    Vec<(
        &'a screenplay_document::SceneID,
        &'a screenplay_document::Scene,
    )>,
> {
    let all_scenes_sorted = get_all_scenes_ordered(screenplay_document)?;
    return filter_scenes_by_character_speaking(screenplay_document, all_scenes_sorted, character);
}

pub fn filter_scenes_by_character_speaking<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    mut scenes_to_filter: Vec<(
        &'a screenplay_document::SceneID,
        &'a screenplay_document::Scene,
    )>,
    character: &screenplay_document::Character,
) -> Option<
    Vec<(
        &'a screenplay_document::SceneID,
        &'a screenplay_document::Scene,
    )>,
> {
    if scenes_to_filter.is_empty() {
        return None;
    }
    scenes_to_filter.sort_by(|(_a, b), (_c, d)| {
        (b.start.page, b.start.line).cmp(&(d.start.page, d.start.line))
    });

    let mut scenes_vec: Vec<_> = Vec::new();

    // COULD OPTIMIZE
    // this just re-starts iteration for ALL PAGES every time
    // ... I tried to optimize it but I just broke it LMAO I'm gonna leave it as is...
    'scenes: for (scn_id, scn) in scenes_to_filter {
        'pages: for (p_idx, page) in screenplay_document.pages.iter().enumerate() {
            'lines: for (l_idx, line) in page.lines.iter().enumerate() {
                if p_idx < scn.start.page {
                    continue 'pages;
                }
                if l_idx < scn.start.line {
                    continue 'lines;
                }
                if line.line_type
                    == Some(SPType::SP_SCENE_HEADING(
                        screenplay_document::SceneHeadingElement::Line,
                    ))
                    && line.scene_id != Some(*scn_id)
                {
                    continue 'scenes;
                }
                if character.is_line(&line) {
                    for (existing_id, _) in &scenes_vec {
                        if *existing_id == scn_id {
                            continue 'lines;
                        }
                    }

                    scenes_vec.push((scn_id, scn));
                }
            }
        }
    }
    if scenes_vec.is_empty() {
        return None;
    } else {
        return Some(scenes_vec);
    }
}

pub fn get_scenes_with_location<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    location: &'a screenplay_document::LocationID,
) -> Option<
    Vec<(
        &'a screenplay_document::SceneID,
        &'a screenplay_document::Scene,
    )>,
> {
    let scenes: Vec<_> = screenplay_document
        .scenes
        .iter()
        .filter(|(_, scn)| scn.story_locations.contains(&location))
        .collect();

    if scenes.is_empty() {
        let location_opt = screenplay_document.locations.get(&location);
        println!("LOCATION: {:?}", location_opt);
        //panic!("COULDN'T FIND SCENES FOR LOCATION!");
        return None;
    }

    Some(scenes)
}

// ------------ Get PAGEs...
// TODO: Filter pages by location, character

pub fn get_all_pages_for_scene<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    scene_id: &'a screenplay_document::SceneID,
) -> Option<Vec<(usize, &'a screenplay_document::Page)>> {
    let checked_scene = screenplay_document.scenes.get(scene_id)?;

    let scenes_ordered = get_all_scenes_ordered(screenplay_document)?;
    //pages.push(checked_scene.start.page.clone());

    for (_this_id, scene_obj) in scenes_ordered {
        if scene_obj.start.page < checked_scene.start.page
            || (scene_obj.start.page == checked_scene.start.page
                && scene_obj.start.line < checked_scene.start.line)
        {
            continue;
        }
        let page_indecies: Vec<usize> =
            (checked_scene.start.page.clone()..=scene_obj.start.page.clone()).collect();

        let mut pages: Vec<(usize, &screenplay_document::Page)> = Vec::new();
        for pi in page_indecies {
            let Some(page) = screenplay_document.pages.get(pi) else {
                continue;
            };
            pages.push((pi.clone(), page));
        }
        if pages.is_empty() {
            return None;
        }
        return Some(pages);
    }

    None
}

pub fn get_all_pages_for_multiple_scenes<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    scene_ids: Vec<(&screenplay_document::SceneID, &screenplay_document::Scene)>,
) -> Option<Vec<(usize, &'a screenplay_document::Page)>> {
    let scenes_ordered = get_all_scenes_ordered(screenplay_document)?;

    let mut all_pages: Vec<(usize, &screenplay_document::Page)> = Vec::new();

    for (scene_id, checked_scene) in scene_ids {
        //pages.push(checked_scene.start.page.clone());

        for (_this_id, scene_obj) in &scenes_ordered {
            if scene_obj.start.page < checked_scene.start.page
                || (scene_obj.start.page == checked_scene.start.page
                    && scene_obj.start.line < checked_scene.start.line)
            {
                continue;
            }
            let page_indecies: Vec<usize> =
                (checked_scene.start.page.clone()..=scene_obj.start.page.clone()).collect();

            let mut pages: Vec<(usize, &screenplay_document::Page)> = Vec::new();
            for pi in page_indecies {
                let Some(page) = screenplay_document.pages.get(pi) else {
                    continue;
                };
                pages.push((pi.clone(), page));
            }
            if pages.is_empty() {
                continue;
            }
            for (idx, page) in pages {
                if !all_pages.contains(&(idx, page)) {
                    all_pages.push((idx, page));
                }
            }
        }
    }
    if all_pages.is_empty() {
        return None;
    }

    Some(all_pages)
}

pub fn get_all_pages_for_location<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    location: &'a screenplay_document::LocationID,
) -> Option<Vec<(usize, &'a screenplay_document::Page)>> {
    let Some(scenes_ordered) = get_all_scenes_ordered(screenplay_document) else {
        return None;
    };
    let scenes_filtered =
        filter_scenes_by_locations(screenplay_document, scenes_ordered, vec![location])?;

    return get_all_pages_for_multiple_scenes(screenplay_document, scenes_filtered);
}

pub fn get_all_pages_for_character_speaking<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    character: &'a screenplay_document::Character,
) -> Option<Vec<(usize, &'a screenplay_document::Page)>> {
    let mut pages: Vec<(usize, &screenplay_document::Page)> = Vec::new();

    'pages: for (idx, page) in screenplay_document.pages.iter().enumerate() {
        for ln in &page.lines {
            if character.is_line(ln) {
                pages.push((idx, page));
                continue 'pages;
            }
        }
    }

    if pages.is_empty() {
        return None;
    }

    Some(pages)
}

pub fn filter_pages_by_character_speaking<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    pages_range: (usize, usize),
    character: &'a screenplay_document::Character,
) -> Option<Vec<(usize, &'a screenplay_document::Page)>> {
    let mut filtered_pages: Vec<(usize, &screenplay_document::Page)> = Vec::new();

    'page_indices: for i in pages_range.0..=pages_range.1 {
        let Some(page) = screenplay_document.pages.get(i) else {
            continue;
        };

        for ln in &page.lines {
            if character.is_line(ln) {
                //panic!("WHAT?");
                filtered_pages.push((i, page));
                continue 'page_indices;
            }
        }
    }
    if filtered_pages.is_empty() {
        return None;
    }

    Some(filtered_pages)
}

pub fn filter_pages_by_multiple_characters_speaking<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    pages_range: (usize, usize),
    characters: Vec<&'a screenplay_document::Character>,
) -> Option<Vec<(usize, &'a screenplay_document::Page)>> {
    let mut filtered_pages: Vec<(usize, &screenplay_document::Page)> = Vec::new();

    'page_indices: for i in pages_range.0..=pages_range.1 {
        let Some(page) = screenplay_document.pages.get(i) else {
            continue;
        };

        for ln in &page.lines {
            for character in &characters {
                if character.is_line(ln) {
                    filtered_pages.push((i, page));
                    continue 'page_indices;
                }
            }
        }
    }
    if filtered_pages.is_empty() {
        return None;
    }

    Some(filtered_pages)
}

pub fn filter_pages_by_location<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    pages_range: (usize, usize),
    location: &'a screenplay_document::LocationID,
) -> Option<Vec<(usize, &'a screenplay_document::Page)>> {
    let Some(scenes_ordered) = get_all_scenes_ordered(screenplay_document) else {
        return None;
    };
    let scenes_within_page_range: Vec<(_)> = scenes_ordered
        .iter()
        .filter(|(scn_id, scn)| pages_range.0 < scn.start.page && scn.start.page < pages_range.1)
        .copied()
        .collect();
    let scenes_filtered = filter_scenes_by_locations(
        screenplay_document,
        scenes_within_page_range,
        vec![location],
    )?;

    return get_all_pages_for_multiple_scenes(screenplay_document, scenes_filtered);
}
pub fn filter_pages_by_multiple_locations<'a>(
    screenplay_document: &'a crate::screenplay_document::ScreenplayDocument,
    pages_range: (usize, usize),
    locations: Vec<&'a screenplay_document::LocationID>,
) -> Option<Vec<(usize, &'a screenplay_document::Page)>> {
    let Some(scenes_ordered) = get_all_scenes_ordered(screenplay_document) else {
        return None;
    };
    let scenes_within_page_range: Vec<(_)> = scenes_ordered
        .iter()
        .filter(|(scn_id, scn)| pages_range.0 < scn.start.page && scn.start.page < pages_range.1)
        .copied()
        .collect();
    let scenes_filtered =
        filter_scenes_by_locations(screenplay_document, scenes_within_page_range, locations)?;

    return get_all_pages_for_multiple_scenes(screenplay_document, scenes_filtered);
}