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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use serde_json::Value;

use crate::{
    defaults::DefaultsMerger,
    error::{FMLError, Result},
    frontend::{
        ExampleBlock, FeatureAdditionChoices, FeatureAdditions, ImportBlock, InlineExampleBlock,
        ManifestFrontEnd, PartialExampleBlock, PathOnly, Types,
    },
    intermediate_representation::{FeatureManifest, ModuleId, TypeRef},
    util::loaders::{FileLoader, FilePath},
};

fn parse_typeref_string(input: String) -> Result<(String, Option<String>)> {
    // Split the string into the TypeRef and the name
    let mut object_type_iter = input.split(&['<', '>'][..]);

    // This should be the TypeRef type (except for )
    let type_ref_name = object_type_iter.next().unwrap().trim();

    if ["String", "Int", "Boolean"].contains(&type_ref_name) {
        return Ok((type_ref_name.to_string(), None));
    }

    // This should be the name or type of the Object
    match object_type_iter.next() {
        Some(object_type_name) => Ok((
            type_ref_name.to_string(),
            Some(object_type_name.to_string()),
        )),
        None => Ok((type_ref_name.to_string(), None)),
    }
}

pub(crate) fn get_typeref_from_string(
    input: String,
    types: &HashMap<String, TypeRef>,
) -> Result<TypeRef, FMLError> {
    let (type_ref, type_name) = parse_typeref_string(input)?;

    Ok(match type_ref.as_str() {
        "String" => TypeRef::String,
        "Int" => TypeRef::Int,
        "Boolean" => TypeRef::Boolean,
        "BundleText" | "Text" => TypeRef::BundleText,
        "BundleImage" | "Drawable" | "Image" => TypeRef::BundleImage,
        "Enum" => TypeRef::Enum(type_name.unwrap()),
        "Object" => TypeRef::Object(type_name.unwrap()),
        "List" => TypeRef::List(Box::new(get_typeref_from_string(
            type_name.unwrap(),
            types,
        )?)),
        "Option" => TypeRef::Option(Box::new(get_typeref_from_string(
            type_name.unwrap(),
            types,
        )?)),
        "Map" => {
            // Maps take a little extra massaging to get the key and value types
            let type_name = type_name.unwrap();
            let mut map_type_info_iter = type_name.split(',');

            let key_type = map_type_info_iter.next().unwrap().to_string();
            let value_type = map_type_info_iter.next().unwrap().trim().to_string();

            if key_type.eq("String") {
                TypeRef::StringMap(Box::new(get_typeref_from_string(value_type, types)?))
            } else {
                TypeRef::EnumMap(
                    Box::new(get_typeref_from_string(key_type, types)?),
                    Box::new(get_typeref_from_string(value_type, types)?),
                )
            }
        }
        type_name => types.get(type_name).cloned().ok_or_else(|| {
            FMLError::TypeParsingError(format!("{type_name} is not a recognized FML type"))
        })?,
    })
}

#[derive(Debug)]
pub struct Parser {
    files: FileLoader,
    source: FilePath,
}

impl Parser {
    pub fn new(files: FileLoader, source: FilePath) -> Result<Parser> {
        Ok(Parser { source, files })
    }

    pub fn load_frontend(files: FileLoader, source: &str) -> Result<ManifestFrontEnd> {
        let source = files.file_path(source)?;
        let parser: Parser = Parser::new(files, source)?;
        let mut loading = HashSet::new();
        parser.load_manifest(&parser.source, &mut loading)
    }

    // This method loads a manifest, including resolving the includes and merging the included files
    // into this top level one.
    // It recursively calls itself and then calls `merge_manifest`.
    pub fn load_manifest(
        &self,
        path: &FilePath,
        loading: &mut HashSet<ModuleId>,
    ) -> Result<ManifestFrontEnd> {
        let id: ModuleId = path.try_into()?;
        let files = &self.files;

        let mut parent = files
            .read::<ManifestFrontEnd>(path)
            .map_err(|e| FMLError::FMLModuleError(id.clone(), e.to_string()))?;

        // We canonicalize the paths to the import files really soon after the loading so when we merge
        // other included files, we cam match up the files that _they_ import, the concatenate the default
        // blocks for their features.
        self.canonicalize_import_paths(path, &mut parent.imports)
            .map_err(|e| FMLError::FMLModuleError(id.clone(), e.to_string()))?;

        self.inline_manifest_resources(path, &mut parent)?;

        loading.insert(id.clone());
        parent
            .includes()
            .iter()
            .try_fold(parent, |parent: ManifestFrontEnd, f| {
                let src_path = files.join(path, f)?;
                let child_id = ModuleId::try_from(&src_path)?;
                Ok(if !loading.contains(&child_id) {
                    let manifest = self.load_manifest(&src_path, loading)?;
                    self.merge_manifest(&src_path, parent, &src_path, manifest)
                        .map_err(|e| FMLError::FMLModuleError(id.clone(), e.to_string()))?
                } else {
                    parent
                })
            })
    }

    // Attempts to merge two manifests: a child into a parent.
    // The `child_path` is needed to report errors.
    fn merge_manifest(
        &self,
        parent_path: &FilePath,
        parent: ManifestFrontEnd,
        child_path: &FilePath,
        child: ManifestFrontEnd,
    ) -> Result<ManifestFrontEnd> {
        self.check_can_merge_manifest(parent_path, &parent, child_path, &child)?;

        // Child must not specify any features, objects or enums that the parent has.
        let features = merge_map(
            &parent.features,
            &child.features,
            "Features",
            "features",
            child_path,
        )?;

        let p_types = &parent.legacy_types.unwrap_or(parent.types);
        let c_types = &child.legacy_types.unwrap_or(child.types);

        let objects = merge_map(
            &c_types.objects,
            &p_types.objects,
            "Objects",
            "objects",
            child_path,
        )?;
        let enums = merge_map(&c_types.enums, &p_types.enums, "Enums", "enums", child_path)?;

        let imports = self.merge_import_block_list(&parent.imports, &child.imports)?;

        let merged = ManifestFrontEnd {
            features,
            types: Types { enums, objects },
            legacy_types: None,
            imports,
            ..parent
        };

        Ok(merged)
    }

    fn inline_manifest_resources(
        &self,
        path: &FilePath,
        manifest: &mut ManifestFrontEnd,
    ) -> Result<()> {
        for feature in manifest.features.values_mut() {
            let as_typed = &feature.examples;
            let mut inlined = Vec::with_capacity(as_typed.len());
            for example in as_typed {
                inlined.push(example.inline(&self.files, path)?);
            }
            feature.examples = inlined;
        }

        for import in &mut manifest.imports {
            let mut features: BTreeMap<String, FeatureAdditionChoices> = Default::default();
            for (feature_id, additions) in &import.features {
                let additions: FeatureAdditions = additions.clone().into();
                features.insert(
                    feature_id.clone(),
                    additions.inline(&self.files, path)?.into(),
                );
            }
            import.features = features;
        }

        Ok(())
    }

    /// Load a manifest and all its imports, recursively if necessary.
    ///
    /// We populate a map of `FileId` to `FeatureManifest`s, so to avoid unnecessary clones,
    /// we return a `FileId` even when the file has already been imported.
    fn load_imports(
        &self,
        current: &FilePath,
        channel: Option<&str>,
        imports: &mut BTreeMap<ModuleId, FeatureManifest>,
        // includes: &mut HashSet<ModuleId>,
    ) -> Result<ModuleId> {
        let id = current.try_into()?;
        if imports.contains_key(&id) {
            return Ok(id);
        }
        // We put a terminus in here, to make sure we don't try and load more than once.
        imports.insert(id.clone(), Default::default());

        // This loads the manifest in its frontend format (i.e. direct from YAML via serde), including
        // all the `includes` for this manifest.
        let frontend = self.load_manifest(current, &mut HashSet::new())?;

        // Aside: tiny quality of life improvement. In the case where only one channel is supported,
        // we use it. This helps with globbing directories where the app wants to keep the feature definition
        // away from the feature configuration.
        let channel = if frontend.channels.len() == 1 {
            frontend.channels.first().map(String::as_str)
        } else {
            channel
        };

        let mut manifest = frontend.get_intermediate_representation(&id, channel)?;

        // We're now going to go through all the imports in the manifest YAML.
        // Each of the import blocks will have a path, and a Map<FeatureId, List<DefaultBlock>>
        // This loop does the work of merging the default blocks back into the imported manifests.
        // We'll then attach all the manifests to the root (i.e. the one we're generating code for today), in `imports`.
        // We associate only the feature ids with the manifest we're loading in this method.
        let mut imported_feature_id_map = BTreeMap::new();

        for block in &frontend.imports {
            // 1. Load the imported manifests in to the hash map.
            let path = self.files.join(current, &block.path)?;
            // The channel comes from the importer, rather than the command or the imported file.
            let child_id = self.load_imports(&path, Some(&block.channel), imports)?;
            let child_manifest = imports.get_mut(&child_id).expect("just loaded this file");

            // We detect that there are no name collisions after the loading has finished, with `check_can_import_manifest`.
            // We can't do it greedily, because of transitive imports may cause collisions, but we'll check here for better error
            // messages.
            check_can_import_manifest(&manifest, child_manifest)?;

            // We detect that the imported files have language specific files in `validate_manifest_for_lang()`.
            // We can't do it now because we don't yet know what this run is going to generate.

            // 2. We'll build a set of feature names that this manifest imports from the child manifest.
            // This will be the only thing we add directly to the manifest we load in this method.
            let mut feature_ids = BTreeSet::new();

            // 3. For each of the features in each of the imported files, the user can specify new defaults that should
            //    merge into/overwrite the defaults specified in the imported file. Let's do that now:
            // a. Prepare a DefaultsMerger, with an object map.
            let merger = DefaultsMerger::new(
                &child_manifest.obj_defs,
                frontend.channels.clone(),
                channel.map(str::to_string),
            );

            // b. Prepare a feature map that we'll alter in place.
            //    EXP- 2540 If we want to support re-exporting/encapsulating features then we will need to change
            //    this to be a more recursive look up. e.g. change `FeatureManifest.feature_defs` to be a `BTreeMap`.
            let feature_map = &mut child_manifest.feature_defs;

            // c. Iterate over the features we want to add to the original feature:
            //    - by adding to the list of examples.
            //    - by overriding default values.
            for (f, feature_additions) in &block.features {
                let feature_def = feature_map.get_mut(f).ok_or_else(|| {
                    FMLError::FMLModuleError(
                        id.clone(),
                        format!("Cannot override defaults for `{f}` feature from {child_id}"),
                    )
                })?;
                // FeatureAdditions holds the extra examples and defaults for this feature.
                let additions: FeatureAdditions = feature_additions.clone().into();

                // d.i) Append the imported list of examples to the original feature examples and…
                feature_def
                    .examples
                    .extend(additions.examples.iter().map(Into::into));

                // d.ii) Merge the overrides in place into the FeatureDefs
                merger
                    .merge_feature_defaults(feature_def, &Some(additions.defaults))
                    .map_err(|e| FMLError::FMLModuleError(child_id.clone(), e.to_string()))?;

                feature_ids.insert(f.clone());
            }

            // 4. Associate the imports as children of this manifest.
            imported_feature_id_map.insert(child_id.clone(), feature_ids);
        }

        manifest.imported_features = imported_feature_id_map;
        imports.insert(id.clone(), manifest);

        Ok(id)
    }

    pub fn get_intermediate_representation(
        &self,
        channel: Option<&str>,
    ) -> Result<FeatureManifest, FMLError> {
        let mut manifests = BTreeMap::new();
        let id = self.load_imports(&self.source, channel, &mut manifests)?;
        let mut fm = manifests
            .remove(&id)
            .expect("Top level manifest should always be present");

        for child in manifests.values() {
            check_can_import_manifest(&fm, child)?;
        }

        fm.all_imports = manifests;

        Ok(fm)
    }
}

impl Parser {
    fn check_can_merge_manifest(
        &self,
        parent_path: &FilePath,
        parent: &ManifestFrontEnd,
        child_path: &FilePath,
        child: &ManifestFrontEnd,
    ) -> Result<()> {
        if !child.channels.is_empty() {
            let child = &child.channels;
            let child = child.iter().collect::<HashSet<&String>>();
            let parent = &parent.channels;
            let parent = parent.iter().collect::<HashSet<&String>>();
            if !child.is_subset(&parent) {
                return Err(FMLError::ValidationError(
                    "channels".to_string(),
                    format!(
                        "Included manifest should not define its own channels: {}",
                        child_path
                    ),
                ));
            }
        }

        if let Some(about) = &child.about {
            if !about.is_includable() {
                return Err(FMLError::ValidationError(
                "about".to_string(),
                format!("Only files that don't already correspond to generated files may be included: file has a `class` and `package`/`module` name: {}", child_path),
            ));
            }
        }

        let mut map = Default::default();
        self.check_can_merge_imports(parent_path, &parent.imports, &mut map)?;
        self.check_can_merge_imports(child_path, &child.imports, &mut map)?;

        Ok(())
    }

    fn canonicalize_import_paths(
        &self,
        path: &FilePath,
        blocks: &mut Vec<ImportBlock>,
    ) -> Result<()> {
        for ib in blocks {
            let p = &self.files.join(path, &ib.path)?;
            ib.path = p.canonicalize()?.to_string();
        }
        Ok(())
    }

    fn check_can_merge_imports(
        &self,
        path: &FilePath,
        blocks: &Vec<ImportBlock>,
        map: &mut HashMap<String, String>,
    ) -> Result<()> {
        for b in blocks {
            let id = &b.path;
            let channel = &b.channel;
            let existing = map.insert(id.clone(), channel.clone());
            if let Some(v) = existing {
                if &v != channel {
                    return Err(FMLError::FMLModuleError(
                        path.try_into()?,
                        format!(
                            "File {} is imported with two different channels: {} and {}",
                            id, v, &channel
                        ),
                    ));
                }
            }
        }
        Ok(())
    }

    fn merge_import_block_list(
        &self,
        parent: &[ImportBlock],
        child: &[ImportBlock],
    ) -> Result<Vec<ImportBlock>> {
        let mut map = parent
            .iter()
            .map(|im| (im.path.clone(), im.clone()))
            .collect::<HashMap<_, _>>();

        for cib in child {
            let path = &cib.path;
            if let Some(pib) = map.get(path) {
                // We'll define an ordering here: the parent will come after the child
                // so the top-level one will override the lower level ones.
                // In practice, this shouldn't make a difference.
                let merged = merge_import_block(cib, pib)?;
                map.insert(path.clone(), merged);
            } else {
                map.insert(path.clone(), cib.clone());
            }
        }

        Ok(map.values().map(|b| b.to_owned()).collect::<Vec<_>>())
    }
}

fn merge_map<T: Clone>(
    a: &BTreeMap<String, T>,
    b: &BTreeMap<String, T>,
    display_key: &str,
    key: &str,
    child_path: &FilePath,
) -> Result<BTreeMap<String, T>> {
    let mut set = HashSet::new();

    let (a, b) = if a.len() < b.len() { (a, b) } else { (b, a) };

    let mut map = b.clone();

    for (k, v) in a {
        if map.contains_key(k) {
            set.insert(k.clone());
        } else {
            map.insert(k.clone(), v.clone());
        }
    }

    if set.is_empty() {
        Ok(map)
    } else {
        Err(FMLError::ValidationError(
            format!("{}/{:?}", key, set),
            format!(
                "{} cannot be defined twice, overloaded definition detected at {}",
                display_key, child_path,
            ),
        ))
    }
}

fn merge_import_block(a: &ImportBlock, b: &ImportBlock) -> Result<ImportBlock> {
    let mut block = a.clone();

    for (id, additions) in &b.features {
        block
            .features
            .entry(id.clone())
            .and_modify(|existing| existing.merge(additions))
            .or_insert(additions.clone());
    }
    Ok(block)
}

/// Check if this parent can import this child.
fn check_can_import_manifest(parent: &FeatureManifest, child: &FeatureManifest) -> Result<()> {
    check_can_import_list(parent, child, "enum", |fm: &FeatureManifest| {
        fm.enum_defs.keys().collect()
    })?;
    check_can_import_list(parent, child, "objects", |fm: &FeatureManifest| {
        fm.obj_defs.keys().collect()
    })?;
    check_can_import_list(parent, child, "features", |fm: &FeatureManifest| {
        fm.feature_defs.keys().collect()
    })?;

    Ok(())
}

fn check_can_import_list(
    parent: &FeatureManifest,
    child: &FeatureManifest,
    key: &str,
    f: fn(&FeatureManifest) -> HashSet<&String>,
) -> Result<()> {
    let p = f(parent);
    let c = f(child);
    let intersection = p.intersection(&c).collect::<HashSet<_>>();
    if !intersection.is_empty() {
        Err(FMLError::ValidationError(
            key.to_string(),
            format!(
                "`{}` types {:?} conflict when {} imports {}",
                key, &intersection, &parent.id, &child.id
            ),
        ))
    } else {
        Ok(())
    }
}

impl ExampleBlock {
    fn inline(&self, files: &FileLoader, root: &FilePath) -> Result<Self> {
        Ok(match self {
            Self::Inline(_) => self.clone(),
            Self::Partial(PartialExampleBlock { metadata, path }) => {
                let file = files.join(root, path)?;
                let value: Value = files.read(&file)?;
                Self::Inline(InlineExampleBlock {
                    metadata: metadata.to_owned(),
                    value,
                })
            }
            Self::BarePath(path) | Self::Path(PathOnly { path }) => {
                let file = files.join(root, path)?;
                let value: InlineExampleBlock = files.read(&file)?;
                Self::Inline(value)
            }
        })
    }
}

impl FeatureAdditionChoices {
    fn merge(&mut self, other: &Self) {
        match (self, other) {
            (Self::FeatureAdditions(a), Self::FeatureAdditions(b)) => a.merge(b),
            _ => unreachable!("FeatureAdditionChoices should have been rationalized already. This is a bug in nimbus-fml"),
        };
    }
}

impl FeatureAdditions {
    fn inline(&self, files: &FileLoader, root: &FilePath) -> Result<Self> {
        let examples = self
            .examples
            .iter()
            .map(|ex| ex.inline(files, root))
            .collect::<Result<_>>()?;
        Ok(Self {
            examples,
            defaults: self.defaults.clone(),
        })
    }

    fn merge(&mut self, other: &Self) {
        self.examples.extend(other.examples.clone());
        self.defaults.extend(other.defaults.clone());
    }
}

#[cfg(test)]
mod unit_tests {

    use std::{
        path::{Path, PathBuf},
        vec,
    };

    use serde_json::json;

    use super::*;
    use crate::{
        error::Result,
        frontend::ImportBlock,
        intermediate_representation::{PropDef, VariantDef},
        util::{join, pkg_dir},
    };

    #[test]
    fn test_parse_from_front_end_representation() -> Result<()> {
        let path = join(pkg_dir(), "fixtures/fe/nimbus_features.yaml");
        let path = Path::new(&path);
        let files = FileLoader::default()?;
        let parser = Parser::new(files, path.into())?;
        let ir = parser.get_intermediate_representation(Some("release"))?;

        // Validate parsed enums
        assert!(ir.enum_defs.len() == 1);
        let enum_def = &ir.enum_defs["PlayerProfile"];
        assert!(enum_def.name == *"PlayerProfile");
        assert!(enum_def.doc == *"This is an enum type");
        assert!(enum_def.variants.contains(&VariantDef {
            name: "adult".to_string(),
            doc: "This represents an adult player profile".to_string()
        }));
        assert!(enum_def.variants.contains(&VariantDef {
            name: "child".to_string(),
            doc: "This represents a child player profile".to_string()
        }));

        // Validate parsed objects
        assert!(ir.obj_defs.len() == 1);
        let obj_def = &ir.obj_defs["Button"];
        assert!(obj_def.name == *"Button");
        assert!(obj_def.doc == *"This is a button object");
        assert!(obj_def.props.contains(&PropDef::with_doc(
            "label",
            "This is the label for the button",
            &TypeRef::String,
            &serde_json::json!("REQUIRED FIELD")
        )));
        assert!(obj_def.props.contains(&PropDef::with_doc(
            "color",
            "This is the color of the button",
            &TypeRef::Option(Box::new(TypeRef::String)),
            &serde_json::Value::Null
        )));

        // Validate parsed features
        assert!(ir.feature_defs.len() == 1);
        let feature_def = ir.get_feature("dialog-appearance").unwrap();
        assert!(feature_def.name == *"dialog-appearance");
        assert!(feature_def.doc() == *"This is the appearance of the dialog");
        let positive_button = feature_def
            .props
            .iter()
            .find(|x| x.name == "positive")
            .unwrap();
        assert!(positive_button.name == *"positive");
        assert!(positive_button.doc == *"This is a positive button");
        assert!(positive_button.typ == TypeRef::Object("Button".to_string()));
        // We verify that the label, which came from the field default is "Ok then"
        // and the color default, which came from the feature default is "green"
        assert!(positive_button.default.get("label").unwrap().as_str() == Some("Ok then"));
        assert!(positive_button.default.get("color").unwrap().as_str() == Some("green"));
        let negative_button = feature_def
            .props
            .iter()
            .find(|x| x.name == "negative")
            .unwrap();
        assert!(negative_button.name == *"negative");
        assert!(negative_button.doc == *"This is a negative button");
        assert!(negative_button.typ == TypeRef::Object("Button".to_string()));
        assert!(negative_button.default.get("label").unwrap().as_str() == Some("Not this time"));
        assert!(negative_button.default.get("color").unwrap().as_str() == Some("red"));
        let background_color = feature_def
            .props
            .iter()
            .find(|x| x.name == "background-color")
            .unwrap();
        assert!(background_color.name == *"background-color");
        assert!(background_color.doc == *"This is the background color");
        assert!(background_color.typ == TypeRef::String);
        assert!(background_color.default.as_str() == Some("white"));
        let player_mapping = feature_def
            .props
            .iter()
            .find(|x| x.name == "player-mapping")
            .unwrap();
        assert!(player_mapping.name == *"player-mapping");
        assert!(player_mapping.doc == *"This is the map of the player type to a button");
        assert!(
            player_mapping.typ
                == TypeRef::EnumMap(
                    Box::new(TypeRef::Enum("PlayerProfile".to_string())),
                    Box::new(TypeRef::Object("Button".to_string()))
                )
        );
        assert!(
            player_mapping.default
                == json!({
                    "child": {
                        "label": "Play game!",
                        "color": "green"
                    },
                    "adult": {
                        "label": "Play game!",
                        "color": "blue",
                    }
                })
        );

        Ok(())
    }

    #[test]
    fn test_merging_defaults() -> Result<()> {
        let path = join(pkg_dir(), "fixtures/fe/default_merging.yaml");
        let path = Path::new(&path);
        let files = FileLoader::default()?;
        let parser = Parser::new(files, path.into())?;
        let ir = parser.get_intermediate_representation(Some("release"))?;
        let feature_def = ir.get_feature("dialog-appearance").unwrap();
        let positive_button = feature_def
            .props
            .iter()
            .find(|x| x.name == "positive")
            .unwrap();
        // We validate that the no-channel feature level default got merged back
        assert_eq!(
            positive_button
                .default
                .get("alt-text")
                .unwrap()
                .as_str()
                .unwrap(),
            "Go Ahead!"
        );
        // We validate that the original field level default don't get lost if no
        // feature level default with the same name exists
        assert_eq!(
            positive_button
                .default
                .get("label")
                .unwrap()
                .as_str()
                .unwrap(),
            "Ok then"
        );
        // We validate that feature level default overwrite field level defaults if one exists
        // in the field level, it's blue, but on the feature level it's green
        assert_eq!(
            positive_button
                .default
                .get("color")
                .unwrap()
                .as_str()
                .unwrap(),
            "green"
        );
        // We now re-run this, but merge back the nightly channel instead
        let files = FileLoader::default()?;
        let parser = Parser::new(files, path.into())?;
        let ir = parser.get_intermediate_representation(Some("nightly"))?;
        let feature_def = ir.get_feature("dialog-appearance").unwrap();
        let positive_button = feature_def
            .props
            .iter()
            .find(|x| x.name == "positive")
            .unwrap();
        // We validate that feature level default overwrite field level defaults if one exists
        // in the field level, it's blue, but on the feature level it's bright-red
        // note that it's bright-red because we merged back the `nightly`
        // channel, instead of the `release` channel that merges back
        // by default
        assert_eq!(
            positive_button
                .default
                .get("color")
                .unwrap()
                .as_str()
                .unwrap(),
            "bright-red"
        );
        // We against validate that regardless
        // of the channel, the no-channel feature level default got merged back
        assert_eq!(
            positive_button
                .default
                .get("alt-text")
                .unwrap()
                .as_str()
                .unwrap(),
            "Go Ahead!"
        );
        Ok(())
    }

    #[test]
    fn test_convert_to_typeref_string() -> Result<()> {
        // Testing converting to TypeRef::String
        let types = Default::default();
        assert_eq!(
            get_typeref_from_string("String".to_string(), &types).unwrap(),
            TypeRef::String
        );
        get_typeref_from_string("string".to_string(), &types).unwrap_err();
        get_typeref_from_string("str".to_string(), &types).unwrap_err();

        Ok(())
    }

    #[test]
    fn test_convert_to_typeref_int() -> Result<()> {
        // Testing converting to TypeRef::Int
        let types = Default::default();
        assert_eq!(
            get_typeref_from_string("Int".to_string(), &types).unwrap(),
            TypeRef::Int
        );
        get_typeref_from_string("integer".to_string(), &types).unwrap_err();
        get_typeref_from_string("int".to_string(), &types).unwrap_err();

        Ok(())
    }

    #[test]
    fn test_convert_to_typeref_boolean() -> Result<()> {
        // Testing converting to TypeRef::Boolean
        let types = Default::default();
        assert_eq!(
            get_typeref_from_string("Boolean".to_string(), &types).unwrap(),
            TypeRef::Boolean
        );
        get_typeref_from_string("boolean".to_string(), &types).unwrap_err();
        get_typeref_from_string("bool".to_string(), &types).unwrap_err();

        Ok(())
    }

    #[test]
    fn test_convert_to_typeref_bundletext() -> Result<()> {
        // Testing converting to TypeRef::BundleText
        let types = Default::default();
        get_typeref_from_string("bundletext(something)".to_string(), &types).unwrap_err();
        get_typeref_from_string("BundleText()".to_string(), &types).unwrap_err();

        // The commented out lines below represent areas we need better
        // type checking on, but are ignored for now

        // get_typeref_from_string("BundleText".to_string()).unwrap_err();
        // get_typeref_from_string("BundleText<>".to_string()).unwrap_err();
        // get_typeref_from_string("BundleText<21>".to_string()).unwrap_err();

        Ok(())
    }

    #[test]
    fn test_convert_to_typeref_bundleimage() -> Result<()> {
        // Testing converting to TypeRef::BundleImage
        let types = Default::default();
        assert_eq!(
            get_typeref_from_string("BundleImage<test_name>".to_string(), &types).unwrap(),
            TypeRef::BundleImage
        );
        get_typeref_from_string("bundleimage(something)".to_string(), &types).unwrap_err();
        get_typeref_from_string("BundleImage()".to_string(), &types).unwrap_err();

        // The commented out lines below represent areas we need better
        // type checking on, but are ignored for now

        // get_typeref_from_string("BundleImage".to_string()).unwrap_err();
        // get_typeref_from_string("BundleImage<>".to_string()).unwrap_err();
        // get_typeref_from_string("BundleImage<21>".to_string()).unwrap_err();

        Ok(())
    }

    #[test]
    fn test_convert_to_typeref_enum() -> Result<()> {
        // Testing converting to TypeRef::Enum
        let types = Default::default();
        assert_eq!(
            get_typeref_from_string("Enum<test_name>".to_string(), &types).unwrap(),
            TypeRef::Enum("test_name".to_string())
        );
        get_typeref_from_string("enum(something)".to_string(), &types).unwrap_err();
        get_typeref_from_string("Enum()".to_string(), &types).unwrap_err();

        // The commented out lines below represent areas we need better
        // type checking on, but are ignored for now

        // get_typeref_from_string("Enum".to_string()).unwrap_err();
        // get_typeref_from_string("Enum<>".to_string()).unwrap_err();
        // get_typeref_from_string("Enum<21>".to_string()).unwrap_err();

        Ok(())
    }

    #[test]
    fn test_convert_to_typeref_object() -> Result<()> {
        // Testing converting to TypeRef::Object
        let types = Default::default();
        assert_eq!(
            get_typeref_from_string("Object<test_name>".to_string(), &types).unwrap(),
            TypeRef::Object("test_name".to_string())
        );
        get_typeref_from_string("object(something)".to_string(), &types).unwrap_err();
        get_typeref_from_string("Object()".to_string(), &types).unwrap_err();

        // The commented out lines below represent areas we need better
        // type checking on, but are ignored for now

        // get_typeref_from_string("Object".to_string()).unwrap_err();
        // get_typeref_from_string("Object<>".to_string()).unwrap_err();
        // get_typeref_from_string("Object<21>".to_string()).unwrap_err();

        Ok(())
    }

    #[test]
    fn test_convert_to_typeref_list() -> Result<()> {
        // Testing converting to TypeRef::List
        let types = Default::default();
        assert_eq!(
            get_typeref_from_string("List<String>".to_string(), &types).unwrap(),
            TypeRef::List(Box::new(TypeRef::String))
        );
        assert_eq!(
            get_typeref_from_string("List<Int>".to_string(), &types).unwrap(),
            TypeRef::List(Box::new(TypeRef::Int))
        );
        assert_eq!(
            get_typeref_from_string("List<Boolean>".to_string(), &types).unwrap(),
            TypeRef::List(Box::new(TypeRef::Boolean))
        );

        // Generate a list of user types to validate use of them in a list
        let mut types: HashMap<_, _> = Default::default();
        types.insert(
            "TestEnum".to_string(),
            TypeRef::Enum("TestEnum".to_string()),
        );
        types.insert(
            "TestObject".to_string(),
            TypeRef::Object("TestObject".to_string()),
        );

        assert_eq!(
            get_typeref_from_string("List<TestEnum>".to_string(), &types).unwrap(),
            TypeRef::List(Box::new(TypeRef::Enum("TestEnum".to_string())))
        );
        assert_eq!(
            get_typeref_from_string("List<TestObject>".to_string(), &types).unwrap(),
            TypeRef::List(Box::new(TypeRef::Object("TestObject".to_string())))
        );

        get_typeref_from_string("list(something)".to_string(), &types).unwrap_err();
        get_typeref_from_string("List()".to_string(), &types).unwrap_err();

        // The commented out lines below represent areas we need better
        // type checking on, but are ignored for now

        // get_typeref_from_string("List".to_string()).unwrap_err();
        // get_typeref_from_string("List<>".to_string()).unwrap_err();
        // get_typeref_from_string("List<21>".to_string()).unwrap_err();

        Ok(())
    }

    #[test]
    fn test_convert_to_typeref_option() -> Result<()> {
        // Testing converting to TypeRef::Option
        let types = Default::default();
        assert_eq!(
            get_typeref_from_string("Option<String>".to_string(), &types).unwrap(),
            TypeRef::Option(Box::new(TypeRef::String))
        );
        assert_eq!(
            get_typeref_from_string("Option<Int>".to_string(), &types).unwrap(),
            TypeRef::Option(Box::new(TypeRef::Int))
        );
        assert_eq!(
            get_typeref_from_string("Option<Boolean>".to_string(), &types).unwrap(),
            TypeRef::Option(Box::new(TypeRef::Boolean))
        );

        // Generate a list of user types to validate use of them as Options
        let mut types = HashMap::new();
        types.insert(
            "TestEnum".to_string(),
            TypeRef::Enum("TestEnum".to_string()),
        );
        types.insert(
            "TestObject".to_string(),
            TypeRef::Object("TestObject".to_string()),
        );
        assert_eq!(
            get_typeref_from_string("Option<TestEnum>".to_string(), &types).unwrap(),
            TypeRef::Option(Box::new(TypeRef::Enum("TestEnum".to_string())))
        );
        assert_eq!(
            get_typeref_from_string("Option<TestObject>".to_string(), &types).unwrap(),
            TypeRef::Option(Box::new(TypeRef::Object("TestObject".to_string())))
        );

        get_typeref_from_string("option(something)".to_string(), &types).unwrap_err();
        get_typeref_from_string("Option(Something)".to_string(), &types).unwrap_err();

        // The commented out lines below represent areas we need better
        // type checking on, but are ignored for now

        // get_typeref_from_string("Option".to_string()).unwrap_err();
        // get_typeref_from_string("Option<>".to_string()).unwrap_err();
        // get_typeref_from_string("Option<21>".to_string()).unwrap_err();

        Ok(())
    }

    #[test]
    fn test_convert_to_typeref_map() -> Result<()> {
        // Testing converting to TypeRef::Map
        let types = Default::default();
        assert_eq!(
            get_typeref_from_string("Map<String, String>".to_string(), &types).unwrap(),
            TypeRef::StringMap(Box::new(TypeRef::String))
        );
        assert_eq!(
            get_typeref_from_string("Map<String, Int>".to_string(), &types).unwrap(),
            TypeRef::StringMap(Box::new(TypeRef::Int))
        );
        assert_eq!(
            get_typeref_from_string("Map<String, Boolean>".to_string(), &types).unwrap(),
            TypeRef::StringMap(Box::new(TypeRef::Boolean))
        );

        // Generate a list of user types to validate use of them in a list
        let mut types = HashMap::new();
        types.insert(
            "TestEnum".to_string(),
            TypeRef::Enum("TestEnum".to_string()),
        );
        types.insert(
            "TestObject".to_string(),
            TypeRef::Object("TestObject".to_string()),
        );
        assert_eq!(
            get_typeref_from_string("Map<String, TestEnum>".to_string(), &types).unwrap(),
            TypeRef::StringMap(Box::new(TypeRef::Enum("TestEnum".to_string())))
        );
        assert_eq!(
            get_typeref_from_string("Map<String, TestObject>".to_string(), &types).unwrap(),
            TypeRef::StringMap(Box::new(TypeRef::Object("TestObject".to_string())))
        );
        assert_eq!(
            get_typeref_from_string("Map<TestEnum, String>".to_string(), &types).unwrap(),
            TypeRef::EnumMap(
                Box::new(TypeRef::Enum("TestEnum".to_string())),
                Box::new(TypeRef::String)
            )
        );
        assert_eq!(
            get_typeref_from_string("Map<TestEnum, TestObject>".to_string(), &types).unwrap(),
            TypeRef::EnumMap(
                Box::new(TypeRef::Enum("TestEnum".to_string())),
                Box::new(TypeRef::Object("TestObject".to_string()))
            )
        );

        get_typeref_from_string("map(something)".to_string(), &Default::default()).unwrap_err();
        get_typeref_from_string("Map(Something)".to_string(), &Default::default()).unwrap_err();

        // The commented out lines below represent areas we need better
        // type checking on, but are ignored for now

        // get_typeref_from_string("Map".to_string()).unwrap_err();
        // get_typeref_from_string("Map<>".to_string()).unwrap_err();
        // get_typeref_from_string("Map<21>".to_string()).unwrap_err();

        Ok(())
    }

    #[test]
    fn test_include_check_can_merge_manifest() -> Result<()> {
        let files = FileLoader::default()?;
        let parser = Parser::new(files, std::env::temp_dir().as_path().into())?;
        let parent_path: FilePath = std::env::temp_dir().as_path().into();
        let child_path = parent_path.join("http://not-needed.com")?;
        let parent = ManifestFrontEnd {
            channels: vec!["alice".to_string(), "bob".to_string()],
            ..Default::default()
        };
        let child = ManifestFrontEnd {
            channels: vec!["alice".to_string(), "bob".to_string()],
            ..Default::default()
        };

        assert!(parser
            .check_can_merge_manifest(&parent_path, &parent, &child_path, &child)
            .is_ok());

        let child = ManifestFrontEnd {
            channels: vec!["eve".to_string()],
            ..Default::default()
        };

        assert!(parser
            .check_can_merge_manifest(&parent_path, &parent, &child_path, &child)
            .is_err());

        Ok(())
    }

    #[test]
    fn test_include_check_can_merge_manifest_with_imports() -> Result<()> {
        let files = FileLoader::default()?;
        let parser = Parser::new(files, std::env::temp_dir().as_path().into())?;
        let parent_path: FilePath = std::env::temp_dir().as_path().into();
        let child_path = parent_path.join("http://child")?;
        let parent = ManifestFrontEnd {
            channels: vec!["alice".to_string(), "bob".to_string()],
            imports: vec![ImportBlock {
                path: "absolute_path".to_string(),
                channel: "one_channel".to_string(),
                features: Default::default(),
            }],
            ..Default::default()
        };
        let child = ManifestFrontEnd {
            channels: vec!["alice".to_string(), "bob".to_string()],
            imports: vec![ImportBlock {
                path: "absolute_path".to_string(),
                channel: "another_channel".to_string(),
                features: Default::default(),
            }],
            ..Default::default()
        };

        let mut map = Default::default();
        let res = parser.check_can_merge_imports(&parent_path, &parent.imports, &mut map);
        assert!(res.is_ok());
        assert_eq!(map.get("absolute_path").unwrap(), "one_channel");

        let err_msg = "Problem with http://child/: File absolute_path is imported with two different channels: one_channel and another_channel";
        let res = parser.check_can_merge_imports(&child_path, &child.imports, &mut map);
        assert!(res.is_err());
        assert_eq!(res.unwrap_err().to_string(), err_msg.to_string());

        let res = parser.check_can_merge_manifest(&parent_path, &parent, &child_path, &child);
        assert!(res.is_err());
        assert_eq!(res.unwrap_err().to_string(), err_msg.to_string());

        Ok(())
    }

    #[test]
    fn test_include_circular_includes() -> Result<()> {
        use crate::util::pkg_dir;
        // snake.yaml includes tail.yaml, which includes snake.yaml
        let path = PathBuf::from(pkg_dir()).join("fixtures/fe/including/circular/snake.yaml");

        let files = FileLoader::default()?;
        let parser = Parser::new(files, path.as_path().into())?;
        let ir = parser.get_intermediate_representation(Some("release"));
        assert!(ir.is_ok());

        Ok(())
    }

    #[test]
    fn test_include_deeply_nested_includes() -> Result<()> {
        use crate::util::pkg_dir;
        // Deeply nested includes, which start at 00-head.yaml, and then recursively includes all the
        // way down to 06-toe.yaml
        let path_buf = PathBuf::from(pkg_dir()).join("fixtures/fe/including/deep/00-head.yaml");

        let files = FileLoader::default()?;
        let parser = Parser::new(files, path_buf.as_path().into())?;

        let ir = parser.get_intermediate_representation(Some("release"))?;
        assert_eq!(ir.feature_defs.len(), 1);

        Ok(())
    }
}