aboutsummaryrefslogtreecommitdiff
path: root/crates/cli/src/words.rs
blob: 212cddce28b07e7a77cb65736b33a0d1daae6d04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
use crate::inflection::print_full_inflection_for;
use crate::locale::current_locale;
use std::io::{stdin, IsTerminal};

use inquire::{Confirm, Editor, MultiSelect, Select, Text};
use mihi::cfg::Language;
use mihi::tag::{attach_tag_to_word, dettach_tags_from_word, select_tag_names, select_tags_for};
use mihi::word::*;
use std::vec::IntoIter;

static NEW_MESSAGE: &str = "New word";
static NEXT_MESSAGE: &str = "Skip this one!";
static QUIT_MESSAGE: &str = "Quit!";

// Documentation text which is prepended to editing flags.
static FLAGS_TEXT: &str = r#"# Write a JSON blob with the following allowed keys.
#
# => Boolean
#
# deponent:            This is a Latin deponent verb.
# onlysingular:        It only has singular forms.
# onlyplural:          It only has plural forms.
# contracted_root:     The root contracts for certain forms (e.g. '_liber_' vs '_libr_ī').
# nonpositive:         This is a non-positive word.
# compsup_prefix:      Comparative and superlative forms require a prefix.
# indeclinable:        It cannot be declined :-)
# irregularsup:        The superlative is irregular.
# nopassive:           Verb has no passive form.
# nosupine:            Verb has no supine form.
# noperfect:           Verb has no perfect forms.
# nogerundive:         Verb has no gerundive.
# impersonal:          Verb is impersonal (only third person available).
# impersonalpassive:   Verb is impersonal only on its passive forms.
# noimperative:        Verb has no imperative forms.
# noinfinitive:        Verb has no infinitive forms.
# shortimperative:     The imperative form is a short version.
# onlythirdpassive:    Verb has only forms on the third person of the passive voice.
# enclitic:            This is simply an enclitic.
# notcomparable:       There cannot be a comparable version for this word
# onlyperfect:         Only perfect forms are available.
# semideponent:        This is a Latin semi-deponent verb.
# contracted_vocative  The vocative contracts the root by one character.
#
# => More complex flags
#
# adds:                There are some cases that are to be added to existing ones.
# sets:                There are some cases which need to replace the existing ones.
#
# For example:
#
# {
#   "onlysingular": true,
#   "sets": {
#     "accusative": {
#       "singular": ["im"]
#     }
#   }
# }
#
# That is, this word only has singular forms and the accusative one should be
# '-im' instead of the regular form.
"#;

// Show the help message.
fn help(msg: Option<&str>) {
    if let Some(msg) = msg {
        println!("{}.\n", msg);
    }

    println!("mihi words: Manage words.\n");
    println!("usage: mihi words [OPTIONS] <subcommand>\n");

    println!("Options:");
    println!("   -h, --help\t\tPrint this message.");
    println!("   -t, --tag <NAME>\tFilter words which match the given tag NAME. Multiple tags can be provided to match words with any of the tags provided. This will only be accounted in the 'ls' command.");

    println!("\nSubcommands:");
    println!("   create\t\tCreate a new word. It accepts word enunciates given into a pipe (an enunciate per line), otherwise this command is interactive.");
    println!("   dup\t\t\tCreate a word which is an alternative of another one.");
    println!("   edit\t\t\tEdit information from a word.");
    println!("   ls\t\t\tList the words from the database.");
    println!("   poke\t\t\tUpdate the timestamp for a word.");
    println!("   rel\t\t\tEstablish a relationship between two words.");
    println!("   rm\t\t\tRemove a word from the database.");
    println!("   show\t\t\tShow information from a word.");
}

// Given an enunciated value, try to guess a word from it. If that's not
// possible then an empty word is given.
fn get_initial_guess(value: &str) -> Word {
    let parts = value.trim().split(',').collect::<Vec<_>>();

    if parts.len() == 2 {
        let first = parts.first().unwrap();
        let second = parts.last().unwrap();

        if first.ends_with('a') && second.ends_with("ae") {
            return Word::from(
                first[0..first.len() - 1].to_string(),
                Category::Noun,
                Some(Declension::First),
                None,
                Gender::Feminine,
                "a".to_string(),
            );
        } else if first.ends_with("us") && second.ends_with("ī") {
            return Word::from(
                first[0..first.len() - 2].to_string(),
                Category::Noun,
                Some(Declension::Second),
                None,
                Gender::Masculine,
                "us".to_string(),
            );
        } else if first.ends_with("um") && second.ends_with("ī") {
            return Word::from(
                first[0..first.len() - 2].to_string(),
                Category::Noun,
                Some(Declension::Second),
                None,
                Gender::Neuter,
                "um".to_string(),
            );
        } else if first.ends_with("us") && second.ends_with("ūs") {
            return Word::from(
                first[0..first.len() - 2].to_string(),
                Category::Noun,
                Some(Declension::Fourth),
                None,
                Gender::Masculine,
                "fus".to_string(),
            );
        } else if first.ends_with("ū") && second.ends_with("ūs") {
            return Word::from(
                first[0..first.len() - 1].to_string(),
                Category::Noun,
                Some(Declension::Fourth),
                None,
                Gender::Masculine,
                "fus".to_string(),
            );
        } else if first.ends_with("iēs") && second.ends_with("ēī") {
            return Word::from(
                first[0..first.len() - 3].to_string(),
                Category::Noun,
                Some(Declension::Fifth),
                None,
                Gender::Masculine,
                "ies".to_string(),
            );
        } else if first.ends_with("ēs") && second.ends_with("eī") {
            return Word::from(
                first[0..first.len() - 2].to_string(),
                Category::Noun,
                Some(Declension::Fifth),
                None,
                Gender::Masculine,
                "es".to_string(),
            );
        } else if second.ends_with("is") {
            return Word::from(
                second[0..second.len() - 2].to_string(),
                Category::Noun,
                Some(Declension::Third),
                None,
                Gender::Masculine,
                "is".to_string(),
            );
        }
    }

    Word::from(
        value.to_string(),
        Category::Unknown,
        None,
        None,
        Gender::None,
        String::from("-"),
    )
}

// Remove comments from the "flags" text that was provided.
fn trim_flags(given: String) -> String {
    let mut res = String::new();

    for line in given.lines() {
        let trimmed = line.trim();

        if !trimmed.starts_with('#') {
            res.push_str(trimmed);
        }
    }

    res
}

// Get the translation from `word.translated` which matches the given language
// `key`. If that cannot be found, or for some reason is not a String, then an
// error is returned.
fn get_translated<'a>(word: &'a Word, key: &'a str) -> Result<&'a String, String> {
    match word.translation.get(key) {
        Some(value) => match value {
            serde_json::Value::String(s) => Ok(s),
            _ => Err("unexpected key type".to_string()),
        },
        None => Err("key does not exist".to_string()),
    }
}

fn prompt_declension(cat: &Category, declension: Declension) -> Result<Declension, String> {
    let options;
    let idx;

    match cat {
        Category::Noun => {
            options = vec![
                Declension::First,
                Declension::Second,
                Declension::Third,
                Declension::Fourth,
                Declension::Fifth,
                Declension::Other,
            ];
            idx = declension as usize - 1;
        }
        Category::Adjective => {
            options = vec![Declension::First, Declension::Third];
            idx = if matches!(declension, Declension::Third) {
                1
            } else {
                0
            };
        }
        _ => panic!("bad parameter"),
    }

    let Ok(result) = Select::new("Declension:", options)
        .with_starting_cursor(idx)
        .prompt()
    else {
        return Err("abort!".to_string());
    };

    Ok(result)
}

fn prompt_conjugation(conjugation: Conjugation) -> Result<Conjugation, String> {
    let options = vec![
        Conjugation::First,
        Conjugation::Second,
        Conjugation::Third,
        Conjugation::ThirdIo,
        Conjugation::Fourth,
        Conjugation::Other,
    ];
    let idx = conjugation as usize - 1;

    let Ok(result) = Select::new("Conjugation:", options)
        .with_starting_cursor(idx)
        .prompt()
    else {
        return Err("abort!".to_string());
    };

    Ok(result)
}

// Interactively ask the user to provide information for a word by the given
// `enunciated`. The default values will be based on the given `word` parameter.
fn ask_for_word_based_on(enunciated: String, word: Word) -> Result<Word, String> {
    let Ok(particle) = Text::new("Particle:")
        .with_initial_value(&word.particle)
        .prompt()
    else {
        return Err("abort!".to_string());
    };
    let particle = particle.trim().to_string();

    let categories = vec![
        Category::Unknown,
        Category::Noun,
        Category::Adjective,
        Category::Verb,
        Category::Pronoun,
        Category::Adverb,
        Category::Preposition,
        Category::Conjunction,
        Category::Interjection,
        Category::Determiner,
    ];
    let Ok(category) = Select::new("Category:", categories)
        .with_starting_cursor((word.category as isize).try_into().unwrap())
        .prompt()
    else {
        return Err("abort!".to_string());
    };

    let genders = vec![
        Gender::Masculine,
        Gender::Feminine,
        Gender::MasculineOrFeminine,
        Gender::Neuter,
        Gender::None,
    ];
    let gender = match category {
        Category::Noun => {
            match Select::new("Gender:", genders)
                .with_starting_cursor((word.gender as isize).try_into().unwrap())
                .prompt()
            {
                Ok(selection) => selection,
                Err(_) => return Err("abort!".to_string()),
            }
        }
        _ => Gender::None,
    };

    let declension;
    let conjugation;
    match category {
        Category::Noun | Category::Adjective => {
            declension = Some(prompt_declension(
                &category,
                word.declension.clone().unwrap_or(Declension::First),
            )?);
            conjugation = None;
        }
        Category::Verb => {
            declension = None;
            conjugation = Some(prompt_conjugation(
                word.conjugation.clone().unwrap_or(Conjugation::First),
            )?);
        }
        _ => {
            declension = None;
            conjugation = None;
        }
    }

    let kind = match category {
        Category::Noun => {
            let options = match declension {
                Some(Declension::First) => vec!["a"],
                Some(Declension::Second) => vec!["us", "um", "ius", "er/ir"],
                Some(Declension::Third) => vec![
                    "is",
                    "istem",
                    "pureistem",
                    "one",
                    "onenonistem",
                    "two",
                    "three",
                    "visvis",
                    "sussuis",
                    "bosbovis",
                    "iuppiteriovis",
                ],
                Some(Declension::Fourth) => vec!["fus"],
                Some(Declension::Fifth) => vec!["ies", "es"],
                // NOTE: for the 'other' declension we only allow to enter
                // 'indeclinable' words, as that's the only thing that can be
                // added from now on (e.g. things like 'ego' have been manually
                // inserted).
                Some(Declension::Other) => vec!["indeclinable"],
                _ => panic!("shouldn't be here :D"),
            };
            if options.len() == 1 {
                options.first().unwrap().to_string()
            } else {
                match Select::new("Kind:", options).prompt() {
                    Ok(kind) => kind.to_string(),
                    Err(_) => return Err("abort!".to_string()),
                }
            }
        }
        Category::Adjective => {
            let options = match declension {
                Some(Declension::First) => vec!["us", "er/ir"],
                _ => vec!["one", "onenonistem", "two", "three"],
            };
            match Select::new("Kind:", options).prompt() {
                Ok(kind) => kind.to_string(),
                Err(_) => return Err("abort!".to_string()),
            }
        }
        Category::Verb => {
            if matches!(conjugation, Some(Conjugation::Other)) {
                let options = vec![
                    "sum", "possum", "eo", "volo", "nolo", "malo", "fero", "facio", "do", "inquam",
                    "aio",
                ];
                match Select::new("Kind:", options).prompt() {
                    Ok(kind) => kind.to_string(),
                    Err(_) => return Err("abort!".to_string()),
                }
            } else {
                "verb".to_string()
            }
        }
        _ => String::from("-"),
    };

    let regular = if matches!(
        category,
        Category::Noun | Category::Adjective | Category::Verb
    ) {
        let Ok(regular) = Confirm::new("Regular:").with_default(word.regular).prompt() else {
            return Err("abort!".to_string());
        };
        regular
    } else {
        true
    };

    let locative = if matches!(category, Category::Noun) {
        let Ok(locative) = Confirm::new("Locative:")
            .with_default(word.locative)
            .prompt()
        else {
            return Err("abort!".to_string());
        };
        locative
    } else {
        false
    };

    let Ok(raw_weight) = Text::new("Weight:")
        .with_initial_value(word.weight.to_string().as_str())
        .prompt()
    else {
        return Err("abort!".to_string());
    };
    let Ok(weight) = raw_weight.parse::<isize>() else {
        return Err("bad value".to_string());
    };
    if weight > 10 {
        return Err(format!(
            "weight has to be an integer between 0 and 10, but {weight} was given"
        ));
    }

    let raw_flags = serde_json::to_string(&word.flags).unwrap();

    let Ok(flags) = Editor::new("Flags:")
        .with_predefined_text(format!("{FLAGS_TEXT}\n{raw_flags}").as_str())
        .prompt()
    else {
        return Err("abort!".to_string());
    };
    let trimmed_flags = trim_flags(flags);

    let Ok(translation_en) = Text::new("Translation (english):")
        .with_initial_value(get_translated(&word, "en").unwrap_or(&String::from("")))
        .prompt()
    else {
        return Err("abort!".to_string());
    };
    let Ok(translation_ca) = Text::new("Translation (catalan):")
        .with_initial_value(get_translated(&word, "ca").unwrap_or(&String::from("")))
        .prompt()
    else {
        return Err("abort!".to_string());
    };

    Ok(Word {
        id: word.id,
        enunciated,
        particle,
        language: Language::Latin,
        declension: if matches!(category, Category::Verb) {
            None
        } else {
            declension
        },
        conjugation: if matches!(category, Category::Verb) {
            conjugation
        } else {
            None
        },
        kind,
        category,
        regular,
        locative,
        gender,
        suffix: None,
        translation: serde_json::from_str(
            format!(
                "{{\"en\":\"{}\", \"ca\":\"{}\"}}",
                translation_en.trim(),
                translation_ca.trim()
            )
            .as_str(),
        )
        .unwrap(),
        flags: serde_json::from_str(&trimmed_flags).unwrap(),
        succeeded: 0,
        steps: 0,
        weight,
    })
}

// Interactively ask the user for the given `enunciated`, build up a Word object
// from it, and insert it into the database.
fn do_create(enunciated: String) -> Result<(), String> {
    let mut guess = get_initial_guess(enunciated.as_str());
    guess.enunciated = enunciated.trim().to_string();

    let tags = select_tags_for(None)?;
    let word = ask_for_word_based_on(enunciated.clone(), guess)?;
    let Ok(selected_tags) = MultiSelect::new("Tags:", tags)
        .with_starting_cursor(0)
        .prompt()
    else {
        return Err("abort!".to_string());
    };

    match create_word(word) {
        Ok(word_id) => {
            for tag in selected_tags {
                if let Err(e) = attach_tag_to_word(tag.id as i64, word_id) {
                    println!("warning: words: {e}");
                }
            }
            println!("Word '{enunciated}' has been successfully created!");
            Ok(())
        }
        Err(e) => Err(e),
    }
}

fn create(args: IntoIter<String>) -> i32 {
    if args.len() > 0 {
        help(Some(
            "error: words: no arguments were expected for this command",
        ));
        return 1;
    }

    loop {
        // Grab the enunciate from the word that we want to create. If the stdin
        // is actually tied to a pipe, then try to read from it so we get the
        // initial value for our prompt. If no more input is given into the
        // pipe, then we quit altogether.
        let mut guess = String::new();
        let mut guess_str = "";
        if !stdin().is_terminal() {
            if stdin().read_line(&mut guess).unwrap_or(0) == 0 {
                // No more input, quit.
                return 0;
            }
            guess_str = guess.trim();

            // Blank line, quit as well.
            if guess_str.is_empty() {
                return 0;
            }
        }
        let Ok(enunciated) = Text::new("Enunciated:")
            .with_initial_value(guess_str)
            .prompt()
        else {
            return 1;
        };
        if enunciated.trim().is_empty() {
            return 0;
        }

        // Now we try to fetch whether the word already existed, by doing a
        // general search on the database.
        let mut words = match select_enunciated(Some(enunciated.clone()), &[]) {
            Ok(words) => words,
            Err(e) => {
                println!("error: words: {e}");
                return 1;
            }
        };
        words.push(NEW_MESSAGE.to_string());
        words.push(NEXT_MESSAGE.to_string());
        words.push(QUIT_MESSAGE.to_string());

        match words.len() {
            // Seems confusing, but we fill the "words" list with three default
            // "messages" which are part of the interface. Hence, if only three
            // "words" exist, then it's just the interface and we can go right
            // into creating the word.
            3 => {
                if let Err(e) = do_create(enunciated) {
                    println!("error: words: {e}");
                    return 1;
                }
            }
            _ => match Select::new("Is your word on this list?", words).prompt() {
                Ok(choice) => {
                    if choice == QUIT_MESSAGE {
                        return 0;
                    } else if choice == NEW_MESSAGE {
                        if let Err(e) = do_create(enunciated) {
                            println!("error: words: {e}");
                            return 1;
                        }
                    }
                }
                Err(_) => return 1,
            },
        };
    }
}

fn ls(mut args: IntoIter<String>, tags: &[String]) -> i32 {
    if args.len() > 1 {
        help(Some("error: words: too many filters"));
        return 1;
    }

    let words = match select_enunciated(args.next(), tags) {
        Ok(words) => words,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    for enunciated in words {
        println!("{enunciated}");
    }

    0
}

// Given a search parameter, returns the word that match the enunciate. If
// multiple words match the same search parameter, then the user is asked to
// select one from a list of candidates.
fn select_single_word(search: Option<String>) -> Result<String, String> {
    let words = select_enunciated(search, &[])?;

    match words.len() {
        0 => Err("not found".to_string()),
        1 => Ok(words.first().unwrap().to_owned()),
        _ => match Select::new("Which word?", words)
            .with_page_size(20)
            .prompt()
        {
            Ok(choice) => Ok(choice),
            Err(_) => Err("abort!".to_string()),
        },
    }
}

fn dup(mut args: IntoIter<String>) -> i32 {
    if args.len() > 1 {
        help(Some(
            "error: words: only one argument. If it's an enunciate, wrap it in double quotes",
        ));
        return 1;
    }

    // To duplicate a word, you need exactly one as a reference.
    let enunciated = match select_single_word(args.next()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    // Fetch the word object for it which will serve as the initial values.
    let word = match find_by(enunciated.as_str()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };
    let source_id = word.id as i64;

    // The enunciate should change, let's ask for it again. This way we get the
    // same experience as with the 'create' command.
    let Ok(enunciated) = Text::new("Enunciated:")
        .with_initial_value(&word.enunciated)
        .prompt()
    else {
        return 1;
    };
    let trimmed = enunciated.trim();
    if trimmed.is_empty() || trimmed == word.enunciated {
        println!("Nothing to do...");
        return 1;
    }

    // Select the tags for the current word.
    let tags = match select_tags_for(Some(word.id)) {
        Ok(tags) => tags,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };
    let all_tags = match select_tags_for(None) {
        Ok(tags) => tags,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    // And ask again column by column to check for changes.
    let updated = match ask_for_word_based_on(enunciated.clone(), word) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    // Ask for tags. The indeces on the UI do not match the ones on the
    // DB. Hence, we need to match the IDs from the DB to the ones displayed on
    // the DB. It's a bit cumbersome but there shouldn't be many tags for this
    // to become painfully slow.
    let mut default_indices = vec![];
    for t in &tags {
        for (idx, ta) in all_tags.iter().enumerate() {
            if t.id == ta.id {
                default_indices.push(idx);
            }
        }
    }
    let Ok(selected_tags) = MultiSelect::new("Tags:", all_tags)
        .with_starting_cursor(0)
        .with_default(&default_indices)
        .prompt()
    else {
        return 1;
    };

    // Create the word. If successful, then we move into relationships and tags.
    match create_word(updated) {
        Ok(word_id) => {
            // Set it as an alternative. This goes both ways, so two
            // relationships have to be inserted with both directions.
            if let Err(e) = add_word_relationship(source_id, word_id, RelationKind::Alternative) {
                println!("errors: words: {e}.");
                return 1;
            }
            if let Err(e) = add_word_relationship(word_id, source_id, RelationKind::Alternative) {
                println!("errors: words: {e}.");
                return 1;
            }

            // Attach tags.
            for tag in selected_tags {
                if let Err(e) = attach_tag_to_word(tag.id as i64, word_id) {
                    println!("warning: words: {e}.");
                }
            }
            println!("Word '{enunciated}' has been successfully created!");
            0
        }
        Err(e) => {
            println!("error: words: {e}.");
            1
        }
    }
}

fn edit(mut args: IntoIter<String>) -> i32 {
    if args.len() > 1 {
        help(Some(
            "error: words: only one argument. If it's an enunciate, wrap it in double quotes",
        ));
        return 1;
    }

    // Only one word can be modified at a time.
    let enunciated = match select_single_word(args.next()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    // Fetch the word object for it which will serve as the initial values.
    let word = match find_by(enunciated.as_str()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    // Preserve this value as it will be used at the end of this function.
    let word_id = word.id as i64;

    // The enunciate might change, let's ask for it again. This way we get the
    // same experience as with the 'create' command.
    let Ok(enunciated) = Text::new("Enunciated:")
        .with_initial_value(&word.enunciated)
        .prompt()
    else {
        return 1;
    };
    if enunciated.trim().is_empty() {
        return 0;
    }

    // Select the tags for the current word.
    let tags = match select_tags_for(Some(word.id)) {
        Ok(tags) => tags,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };
    let all_tags = match select_tags_for(None) {
        Ok(tags) => tags,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    // And ask again column by column to check for changes.
    let updated = match ask_for_word_based_on(enunciated.clone(), word) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    // Ask for tags. The indeces on the UI do not match the ones on the
    // DB. Hence, we need to match the IDs from the DB to the ones displayed on
    // the DB. It's a bit cumbersome but there shouldn't be many tags for this
    // to become painfully slow.
    let mut default_indices = vec![];
    for t in &tags {
        for (idx, ta) in all_tags.iter().enumerate() {
            if t.id == ta.id {
                default_indices.push(idx);
            }
        }
    }
    let Ok(selected_tags) = MultiSelect::new("Tags:", all_tags)
        .with_starting_cursor(0)
        .with_default(&default_indices)
        .prompt()
    else {
        return 1;
    };

    // Compute which tags to add and which to remove. This is, again, not the
    // most fun thing to do, but I think it's better/cleaner on the long run
    // than simply removing all tag associations and then bringing them
    // back. And as I said before, there shouldn't be too many tags for this to
    // become too slow.
    let mut tags_to_add = vec![];
    let mut tags_to_remove = vec![];
    for st in &selected_tags {
        if !tags.iter().any(|et| st.id == et.id) {
            tags_to_add.push(st.id);
        }
    }
    for et in &tags {
        if !selected_tags.iter().any(|st| st.id == et.id) {
            tags_to_remove.push(et.id);
        }
    }

    match update_word(updated) {
        Ok(_) => {
            // Add missing tags.
            for tag in tags_to_add {
                if let Err(e) = attach_tag_to_word(tag as i64, word_id) {
                    println!("warning: words: {e}");
                }
            }

            // Drop tags which are no longer needed.
            if let Err(e) = dettach_tags_from_word(&tags_to_remove, word_id) {
                println!("warning: words: {e}");
            }

            println!("Word '{enunciated}' has been updated!");
            0
        }
        Err(e) => {
            println!("error: words: {e}");
            1
        }
    }
}

// Returns a string with a more human-readable declension kind. If the kind is
// self-explanatory, then None is returned (e.g. "a" is the only kind for the
// first declension, so it's redundant).
fn humanize_kind(kind: &str) -> Option<&str> {
    match kind {
        // Noun
        "a" => None,
        "us" => Some("regular -us"),
        "er/ir" => Some("-er/-ir"),
        "um" => Some("neuter -um"),
        "ius" => Some("-ius; like 'fīlius'"),
        "is" => Some("regular -is"),
        "istem" => Some("i-stem; '-i-' also in the genitive plural"),
        "pureistem" => Some("pure i-stem; '-i-' also in the ablative singular"),
        "visvis" => Some("irregular 'vīs, vīs'"),
        "sussuis" => Some("irregular 'sūs, suis'"),
        "bosbovis" => Some("irregular 'bōs, bovis'"),
        "iuppiteriovis" => Some("irregular 'Iuppiter, Iovis'"),
        "fus" => None,
        "domusdomus" => Some("irregular 'domus, domūs/domī'"),
        "ies" => Some("-iēs; like 'diēs, diēī'"),
        "es" => Some("-ēs; like 'rēs, reī'"),
        "indeclinable" => Some("indeclinable"),

        // Adjective
        "one" => Some("one termination adjective"),
        "onenonistem" => Some("one termination adjective; non i-stem like 'melior, melius'"),
        "two" => Some("two termination adjective"),
        "three" => Some("three termination adjective"),
        "unusnauta" => Some("'ūnus nauta' like 'ūnus, ūna, ūnum'"),
        "unusnautaer/ir" => Some("'ūnus nauta' like 'neuter, neutra, neutrum'"),
        "duo" => Some("number 'duo, duae, duo'"),
        "tres" => Some("number 'trēs, trēs, tria'"),
        "mille" => Some("number 'mīlle, mīlle'"),

        // Others
        "egonos" => Some("'ego, nōs'"),
        "demonstrative-weak" => Some("weak demonstrative"),
        "demonstrative-proximal" => Some("proximal demonstrative"),
        "demonstrative-distal" => Some("distal demonstrative"),
        "demonstrative-medial" => Some("medial demonstrative"),
        "demonstrative-idem" => Some("'īdem, eadem, idem' demonstrative"),
        "tuvos" => Some("'tū, vōs'"),
        "sesui" => Some("'sē, suī'"),

        _ => Some(kind),
    }
}

fn humanize_flag(s: &str) -> String {
    match s {
        "deponent" => "deponent",
        "semideponent" => "semi-deponent",
        "onlysingular" => "only singular forms",
        "onlyplural" => "only plural forms",
        "compsup_prefix" => "comparative and superlative forms require a prefix",
        "indeclinable" => "indeclinable",
        "irregularsup" => "irregular superlative",
        "nopassive" => "no passive forms",
        "nosupine" => "no supine form",
        "noperfect" => "no perfect forms",
        "nogerundive" => "no gerundive",
        "impersonal" => "impersonal",
        "impersonalpassive" => "impersonal only on its passive forms",
        "noimperative" => "no imperative forms",
        "noinfinitive" => "no infinitive forms",
        "shortimperative" => "irregular short imperative",
        "onlythirdpassive" => "only forms on the third person of the passive voice",
        "notcomparable" => "not comparable",
        "onlyperfect" => "only perfect forms",
        "contracted_vocative" => "contracted vocative, as in filī, not filiī*",
        _ => "",
    }
    .to_string()
}

fn humanize_flags(word: &Word) -> String {
    let mut flags = vec![];

    if let Some(obj) = word.flags.as_object() {
        for (key, value) in obj {
            if value.as_bool().unwrap_or_default() {
                flags.push(humanize_flag(key));
            }
        }
    }

    flags.join("; ")
}

fn title_for_word(word: &Word) -> String {
    let s = match word.gender {
        Gender::None => format!("{} ({}", word.enunciated, word.category),
        _ => format!(
            "{} ({} {}",
            word.enunciated,
            word.gender.abbrev(),
            word.category
        ),
    };

    let flags = humanize_flags(word);
    if flags.is_empty() {
        return format!("{})", s);
    }
    format!("{}; {})", s, flags)
}

fn show_info(word: Word) -> Result<(), String> {
    // Title.
    println!("Word: {}", title_for_word(&word));

    // Conjugation, declension + kind.
    match word.conjugation {
        Some(ref conjugation) => {
            println!("Conjugation: {}", conjugation.display_with_kind(&word.kind))
        }
        None => {
            if let Some(ref d) = word.declension {
                if matches!(d, Declension::Other) {
                    println!("Declension: {}.", humanize_kind(&word.kind).unwrap_or("-"));
                } else {
                    match humanize_kind(&word.kind) {
                        Some(k) => println!("Declension: {}; kind: {}.", d, k),
                        None => println!("Declension: {}", d),
                    }
                }
            }
        }
    };

    // Show relationships with other words.

    let related = select_related_words(&word)?;

    if matches!(word.category, Category::Adjective) {
        print!(
            "Comparative: {} || ",
            comparative(&word, &related[RelationKind::Comparative as usize - 1])
        );
        print!(
            "Superlative: {} || ",
            superlative(&word, &related[RelationKind::Superlative as usize - 1])
        );
        println!(
            "Adverb: {}",
            adverb(&word, &related[RelationKind::Adverb as usize - 1])
        );
    }

    let alternatives = &related[RelationKind::Alternative as usize - 1];
    match alternatives.len() {
        0 => {}
        1 => println!("Alternative: {}", joint_related_words(alternatives)),
        _ => println!("Alternatives: {}", joint_related_words(alternatives)),
    }
    let gendered = &related[RelationKind::Gendered as usize - 1];
    let g = if matches!(word.gender, Gender::Masculine) {
        "Feminine"
    } else {
        "Masculine"
    };
    match gendered.len() {
        0 => {}
        1 => println!("{g} alternative: {}", joint_related_words(gendered)),
        _ => println!("{g} alternatives: {}", joint_related_words(gendered)),
    }

    // Show translation if available.
    let locale = current_locale();
    if let Some(translation) = word.translation.get(locale.to_code()) {
        let s = translation.as_str().unwrap_or("");
        if !s.is_empty() {
            println!("Translation ({}): {}.", locale.to_code(), s);
        }
    }

    print_full_inflection_for(word)?;

    Ok(())
}

fn poke(mut args: IntoIter<String>) -> i32 {
    if args.len() > 1 {
        help(Some(
            "error: words: only one argument. If it's an enunciate, wrap it in double quotes",
        ));
        return 1;
    }

    let enunciated = match select_single_word(args.next()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}.");
            return 1;
        }
    };

    if update_timestamp(enunciated.as_str()).is_ok() {
        0
    } else {
        1
    }
}

fn rel(args: IntoIter<String>) -> i32 {
    if args.len() > 0 {
        help(Some(
            "error: words: no arguments were expected for this command",
        ));
        return 1;
    }

    println!("The word:");
    let source_enunciate = match select_single_word(None) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}.");
            return 1;
        }
    };
    let source = match find_by(source_enunciate.as_str()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    let kinds = vec![
        RelationKind::Comparative,
        RelationKind::Superlative,
        RelationKind::Adverb,
        RelationKind::Alternative,
        RelationKind::Gendered,
    ];
    let Ok(relation) = Select::new("has a...", kinds).prompt() else {
        return 1;
    };

    println!("which is the word:");
    let dest_enunciate = match select_single_word(None) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}.");
            return 1;
        }
    };
    let dest = match find_by(dest_enunciate.as_str()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    match add_word_relationship(source.id as i64, dest.id as i64, relation.clone()) {
        Ok(_) => {
            // If the relation was actually an alternative word, then the
            // relationship goes both ways. Add the same relationship but with
            // the direction changed.
            if matches!(relation, RelationKind::Alternative | RelationKind::Gendered) {
                if let Err(e) =
                    add_word_relationship(dest.id as i64, source.id as i64, relation.clone())
                {
                    println!("errors: words: {e}");
                    return 1;
                }
            }
            println!(
                "Success: '{}' has now been marked as '{relation}' to '{}'",
                dest.enunciated, source.enunciated
            );
            0
        }
        Err(e) => {
            println!("errors: words: {e}");
            1
        }
    }
}

fn show(mut args: IntoIter<String>) -> i32 {
    if args.len() > 1 {
        help(Some(
            "error: words: only one argument. If it's an enunciate, wrap it in double quotes",
        ));
        return 1;
    }

    let enunciated = match select_single_word(args.next()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}.");
            return 1;
        }
    };

    let word = match find_by(enunciated.as_str()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}.");
            return 1;
        }
    };

    if let Err(e) = show_info(word) {
        println!("error: words: {e}.");
        return 1;
    }

    0
}

fn rm(mut args: IntoIter<String>) -> i32 {
    if args.len() > 1 {
        help(Some("error: words: too many filters"));
        return 1;
    }

    let selection = match select_single_word(args.next()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    // Fetch the word object for it which will serve as the initial values.
    let word = match find_by(selection.as_str()) {
        Ok(word) => word,
        Err(e) => {
            println!("error: words: {e}");
            return 1;
        }
    };

    let ans = Confirm::new(
        format!("Do you really want to remove '{selection}' from the database?").as_str(),
    )
    .with_default(false)
    .prompt();

    match ans {
        Ok(true) => match delete_word(&word) {
            Ok(_) => println!("Removed '{selection}' from the database!"),
            Err(e) => {
                println!("error: words: {e}");
                return 1;
            }
        },
        Ok(false) => {
            println!("Doing nothing...");
        }
        Err(_) => return 1,
    }

    0
}

pub fn run(args: Vec<String>) {
    if args.is_empty() {
        help(Some(
            "error: words: you have to provide at least a subcommand",
        ));
        std::process::exit(1);
    }

    let mut it = args.into_iter();
    let mut do_ls = false;
    let mut tags = vec![];

    while let Some(first) = it.next() {
        match first.as_str() {
            "-h" | "--help" => {
                help(None);
                std::process::exit(0);
            }
            "-t" | "--tag" => match it.next() {
                Some(t) => {
                    let name = t.trim().to_string();
                    if let Ok(results) = select_tag_names(&Some(name.clone())) {
                        if results.is_empty() {
                            println!("warning: words: the tag '{}' does not exist.", name);
                        } else {
                            tags.push(name)
                        }
                    }
                }
                None => {
                    help(Some("error: words: you have to provide a tag name"));
                    std::process::exit(1);
                }
            },
            "create" => {
                std::process::exit(create(it));
            }
            "dup" => {
                std::process::exit(dup(it));
            }
            "edit" => {
                std::process::exit(edit(it));
            }
            "ls" => {
                // 'ls' cannot be executed directly as it might receive extra
                // parameters to it.
                do_ls = true;
            }
            "poke" => {
                std::process::exit(poke(it));
            }
            "rel" => {
                std::process::exit(rel(it));
            }
            "rm" => {
                std::process::exit(rm(it));
            }
            "show" => {
                std::process::exit(show(it));
            }
            _ => {
                help(Some(
                    format!("error: words: unknown flag or command '{first}'").as_str(),
                ));
                std::process::exit(1);
            }
        }
    }

    // If 'ls' was asked, do it now as we potentially have all the tags that
    // were provided by the user. Otherwise, the above loop did not result in a
    // valid subcommand (it was not even provided).
    if do_ls {
        std::process::exit(ls(it, &tags));
    } else {
        help(Some(
            "error: words: you need to provide a command"
                .to_string()
                .as_str(),
        ));
        std::process::exit(1);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Returns a string with the format "{comparative form}-{superlative
    // form}-{adverbial form}-{alternatives}-{gendered alternatives}".
    fn related_for(enunciated: &str) -> String {
        let word = find_by(enunciated).unwrap();
        let related = select_related_words(&word).unwrap();
        let alternatives = &related[RelationKind::Alternative as usize - 1];
        let gendered = &related[RelationKind::Gendered as usize - 1];

        let first = if matches!(word.category, Category::Adjective) {
            format!(
                "{}-{}-{}",
                comparative(&word, &related[RelationKind::Comparative as usize - 1]),
                superlative(&word, &related[RelationKind::Superlative as usize - 1]),
                adverb(&word, &related[RelationKind::Adverb as usize - 1])
            )
        } else {
            "--".to_string()
        };

        format!(
            "{}-{}-{}",
            first,
            joint_related_words(alternatives),
            joint_related_words(gendered)
        )
    }

    #[test]
    fn related() {
        assert_eq!(
            related_for("parvus, parva, parvum"),
            "minor, minus-minimus, minima, minimum-parvē--"
        );
        assert_eq!(
            related_for("versō, versāre, versāvī, versātum"),
            "---vorsō, vorsāre, vorsāvī, vorsātum-"
        );
        assert_eq!(related_for("victor, victōris"), "----victrīx, victrīcis");
    }
}