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
use super::{ find_scale_chords, find_rooted_scale_chords };
use crate::theory::{ Note, Steps, ChordStyle, AsRelativeIntervals, ToIonianRelativeStringTry };
use crate::utils::{ to_roman_num, Intercalatable  };
use crate::libr::{ ionian, HeptatonicScaleNamer, get_all_scale_objs };

use std::fmt::Write;

/// Outputs the chords of the scale degrees with roman numerals as base string.
///
/// Example:
/// ```
/// use music_theory::{ theory::*, query::*, libr::* };
/// let style = ChordStyle::Std(MStyle::Long, EStyle::Long);
/// let chords = scale_chords_roman_printout(&ionian::steps(), 3, style);
/// assert_eq!(&chords[0], "Imaj");
/// assert_eq!(&chords[1], "IImin");
/// ```
pub fn scale_chords_roman_printout(steps: &Steps, size: usize, style: ChordStyle) -> Vec<String>{
    let chords = find_scale_chords(steps, size);
    let mut res = Vec::new();
    for (i, chord) in chords.iter().enumerate(){
        res.push(chord.quality(to_roman_num(i + 1), style));
    }
    res
}

/// Outputs the chords of the scale degrees with the chord root as base string.
///
/// Example:
/// ```
/// use music_theory::{ theory::*, query::*, libr::* };
/// let style = ChordStyle::Std(MStyle::Long, EStyle::Long);
/// let chords = scale_chords_root_printout(&ionian::steps(), Note::C1, 3, style);
/// assert_eq!(&chords[0], "Cmaj");
/// assert_eq!(&chords[6], "Bdim");
/// ```
pub fn scale_chords_root_printout(steps: &Steps, root: Note, size: usize, style: ChordStyle)
    -> Vec<String>
{
    let chords = find_rooted_scale_chords(steps, root, size);
    let mut res = Vec::new();
    for chord in chords{
        res.push(chord.as_string(style));
    }
    res
}

/// Outputs all triads and tetrads build upon scale degrees of the scale.
///
/// Example:
/// ```
/// use music_theory::{ theory::*, query::*, libr::* };
/// let style = ChordStyle::Std(MStyle::Long, EStyle::Long);
/// let po = step_chords_string(&ionian::steps(), Note::new(3), style);
/// assert_eq!(
///     po, concat!(
///         "Cmaj, Dmin, Emin, Fmaj, Gmaj, Amin, Bdim\n",
///         "Cmaj7, Dmin7, Emin7, Fmaj7, G7, Amin7, Bø\n"
///     )
/// );
/// ```
pub fn step_chords_string(steps: &Steps, root: Note, style: ChordStyle) -> String{
    let mut string = String::new();
    let triads = scale_chords_root_printout(steps, root, 3, style)
        .intercalate_with_end(", ".to_string(), "\n".to_string());
    string.push_str(&triads);
    let tetrads = scale_chords_root_printout(steps, root, 4, style)
        .intercalate_with_end(", ".to_string(), "\n".to_string());
    string.push_str(&tetrads);
    string
}

/// Outputs a list of scales and modes family name, name, mode number, Ionian relative string and
/// scale degree triads and tetrads.
///
/// Example:
/// ```
/// use music_theory::{ theory::*, query::* };
/// let style = ChordStyle::Std(MStyle::Long, EStyle::Long);
/// let snc = scales_and_chords_printout(style);
/// let lines = snc.lines().collect::<Vec<_>>();
/// assert_eq!(lines[3], "\tImaj, IImin, IIImin, IVmaj, Vmaj, VImin, VIIdim");
/// ```
/// Part of the output:
/// ```plain
/// Ionian
/// 0: Ionian
///     1 2 3 4 5 6 7
///     IΔ, II-, III-, IVΔ, VΔ, VI-, VII°
///     IΔ7, II-7, III-7, IVΔ7, V7, VI-7, VIIø
/// 1: Dorian
///     1 2 ♭3 4 5 6 ♭7
///     I-, II-, IIIΔ, IVΔ, V-, VI°, VIIΔ
///     I-7, II-7, IIIΔ7, IV7, V-7, VIø, VIIΔ7
/// 2: Phrygian
/// ...
/// ```
pub fn scales_and_chords_printout(style: ChordStyle) -> String{
    let namer = HeptatonicScaleNamer::new();
    let objs = get_all_scale_objs();
    let empty = String::from("");
    let mut res = String::new();
    for sobj in objs{
        let _ = writeln!(res, "{}", sobj.family_name());
        for mode in sobj.get_modes(){
            let mode_name = if mode.mode_name == empty{
                if let Some(n) = namer.name(&mode.steps) { n } else { continue; }
            } else {
                mode.mode_name
            };
            let relstring = if let Some(ints) = mode.steps.as_relative_intervals(&ionian::steps()){
                if let Some(rel) = ints.to_ionian_relative_string_try(true){rel} else { continue; }
            } else { continue; };
            let _ = writeln!(res, "{}: {}", mode.mode_nr, mode_name);
            let _ = writeln!(res, "\t{}", relstring);
            let c3 = scale_chords_roman_printout(&mode.steps, 3, style);
            let c4 = scale_chords_roman_printout(&mode.steps, 4, style);
            let _ = write!(res, "\t");
            let _ = write!(res, "{}", c3.intercalate_with_end(", ".to_string(), "\n".to_string()));
            let _ = write!(res, "\t");
            let _ = write!(res, "{}", c4.intercalate_with_end(", ".to_string(), "\n".to_string()));
        }
    }
    res
}

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

    #[test]
    fn test_scale_chords_roman_printout(){
        let style = ChordStyle::Std(MStyle::Long, EStyle::Long);
        let chords = scale_chords_roman_printout(&ionian::steps(), 3, style);
        assert_eq!(
            chords,
            vec![
                String::from("Imaj"),
                String::from("IImin"),
                String::from("IIImin"),
                String::from("IVmaj"),
                String::from("Vmaj"),
                String::from("VImin"),
                String::from("VIIdim"),
            ]
        );
    }

    #[test]
    fn test_scale_chords_root_printout(){
        let style = ChordStyle::Std(MStyle::Long, EStyle::Long);
        let chords = scale_chords_root_printout(&ionian::steps(), Note::C1, 3, style);
        assert_eq!(
            chords,
            vec![
                String::from("Cmaj"),
                String::from("Dmin"),
                String::from("Emin"),
                String::from("Fmaj"),
                String::from("Gmaj"),
                String::from("Amin"),
                String::from("Bdim"),
            ]
        );
    }

    #[test]
    fn test_step_chords_string(){
        let style = ChordStyle::Std(MStyle::Long, EStyle::Long);
        let po = step_chords_string(&ionian::steps(), Note(3), style);
        assert_eq!(
            po, concat!(
                "Cmaj, Dmin, Emin, Fmaj, Gmaj, Amin, Bdim\n",
                "Cmaj7, Dmin7, Emin7, Fmaj7, G7, Amin7, Bø\n"
            )
        );
    }

    #[test]
    fn test_scales_and_chords_printout(){
        let style = ChordStyle::Std(MStyle::Long, EStyle::Long);
        let snc = scales_and_chords_printout(style);
        let lines = snc.lines().collect::<Vec<_>>();
        assert_eq!(lines[3], "\tImaj, IImin, IIImin, IVmaj, Vmaj, VImin, VIIdim");
        assert_eq!(lines[36], "\tIdim, IIaug, IIImin, IVmaj, Vmaj, VIdim, VIImin");
        assert_eq!(lines[58], "Harmonic Major");
        assert_eq!(lines[287], "\t1 ♭2 ♭♭3 ♭4 5 ♭6 7");
    }
}