package tonality;

import jm.constants.Pitches;
import jm.music.data.Note;

public class Tonality {
    public enum Mode {
        MAJOR,
        MINOR,
        HARMONICMINOR,
        MELODICMINOR
    }

    private Integer tonic_;
    private Mode mode_;

    public Tonality(int tonic, Mode mode) {
        this.tonic_ = tonic;
        this.mode_ = mode;
    }

    public Mode getMode() {
        return this.mode_;
    }

    public Integer getTonic() {
        return this.tonic_;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (mode_ == Mode.HARMONICMINOR)
            sb.append("Harmonic ");
        else if (mode_ == Mode.MELODICMINOR)
            sb.append("Melodic ");

        sb.append(pitchToString(tonic_)).append(" ");
        if (mode_ == Mode.MAJOR)
            sb.append("Major");
        else
            sb.append("Minor");
        return sb.toString();
    }

    public static String pitchToString(int pitch) {
        pitch = pitch % 12;
        switch (pitch) {
            case Pitches.C0 % 12:
                return "C";
            case Pitches.CS0 % 12:
                return "C#";
            case Pitches.D0 % 12:
                return "D";
            case Pitches.DS0 % 12:
                return "D#";
            case Pitches.E0 % 12:
                return "E";
            case Pitches.F0 % 12:
                return "F";
            case Pitches.FS0 % 12:
                return "F#";
            case Pitches.G0 % 12:
                return "G";
            case Pitches.GS0 % 12:
                return "G#";
            case Pitches.A0 % 12:
                return "A";
            case Pitches.AS0 % 12:
                return "A#";
            case Pitches.B0 % 12:
                return "B";
        }
        return null;
    }
}

@@ -44,35 +44,8 @@
 hes;
-%0Aimport jm.music.data.Note;
 %0A%0Apu
@@ -221,16 +221,132 @@
 e mode_;
+%0A    private Boolean isSharp_; // Already applied on the tonic pitch. Just useful to distinguish C sharp from D flat
 %0A%0A    pu
@@ -379,16 +379,33 @@
 ode mode
+, boolean isSharp
 ) %7B%0A    
@@ -456,16 +456,49 @@
 = mode;%0A
+        this.isSharp_ = isSharp;%0A
     %7D%0A%0A 
@@ -900,16 +900,26 @@
 g(tonic_
+, isSharp_
 )).appen
@@ -1117,24 +1117,41 @@
 ng(int pitch
+, boolean isSharp
 ) %7B%0A        
@@ -1319,11 +1319,28 @@
 urn 
-%22C#
+isSharp ? %22C#%22 : %22Db
 %22;%0A 
@@ -1462,11 +1462,28 @@
 urn 
-%22D#
+isSharp ? %22D#%22 : %22Eb
 %22;%0A 
@@ -1667,11 +1667,28 @@
 urn 
-%22F#
+isSharp ? %22F#%22 : %22Gb
 %22;%0A 
@@ -1810,11 +1810,28 @@
 urn 
-%22G#
+isSharp ? %22G#%22 : %22Ab
 %22;%0A 
@@ -1953,11 +1953,28 @@
 urn 
-%22A#
+isSharp ? %22A#%22 : %22Bb
 %22;%0A 

handle flat and sharp in Tonality class