/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

/**
 *
 * @author Tom
 */
public class MultiBeat {
    private int instrument = 0;
    private String key = "";
    private boolean major = true;
    private NoteData[][] beat = null;

    public MultiBeat() {
    }
    
    public MultiBeat(NoteData[][] beat) {
        this.beat = beat;
    }

    public int getInstrument() {
        return instrument;
    }

    public void setInstrument(int instrument) {
        this.instrument = instrument;
    }

    public NoteData[][] getBeat() {
        sort();
        return beat;
    }

    public void setBeat(NoteData[][] beat) {
        this.beat = beat;
    }
    
    public void setBeat(int Phrase, int Note, NoteData nd) {
        beat[Phrase][Note] = nd;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public boolean isMajor() {
        return major;
    }

    public void setMajor(boolean major) {
        this.major = major;
    }
    
    @Override
    public String toString(){
        String s = "";
        if (beat.length > 0){
            for (int i=0; i<beat[0].length; i++){
                s += "(";
                for (int j=0; j<beat.length; j++){
                    if (beat[j][i].getPitch() == Integer.MIN_VALUE){
                        s += "R" +beat[j][i].getQuantizedDuration() +",";
                    } else {
                        s += beat[j][i].getNote() +beat[j][i].getQuantizedDuration() +",";
                    }
                }
                s = s.substring(0, s.length()-1);
                s += ")";
            }
        }
        return s;
    }
    
    /**
     * sortiert das zweidimensionale Array nach Pitches absteigend
     * zB.:
     * 60 65 62    60 76 63
     * 58 67 63 => 59 65 62
     * 59 64 60    58 64 60
     */
    public void sort(){
        if (beat.length > 0){
            //alle Stellen mit null auf Rest setzen
            for (int i=0; i<beat.length; i++){
                for (int j=0; j<beat[i].length; j++){
                    if (beat[i][j] == null){
                        beat[i][j] = new NoteData(Integer.MIN_VALUE);
                    }
                }
            }
            //fuer jede Spalte
            for (int i=0; i<beat[0].length; i++){
                bubbleSort(i);
            }
        }
    }
    
    /**
     * Implementierung des BubbleSort-Algorithmus (verbessert)
     * @param Column ID der Spalte, die sortiert werden soll
     */
    private void bubbleSort(int Column){
        int n = beat.length;
        while (n>1){
            int newn = 1;
            for (int i=0; i<n-1; ++i){
                if (beat[i][Column].getPitch() < beat[i+1][Column].getPitch()){
                    NoteData nd = beat[i][Column];
                    beat[i][Column] = beat[i+1][Column];
                    beat[i+1][Column] = nd;
                    newn = i+1;
                }
            }
            n = newn;
        }
    }
    
    /**
    * gibt die Anzahl der Noten in dem Takt zurueck
    * @return Anzahl der Noten im Takt
    */
   public int getNoteCount(){
       if (beat.length > 0){
           return beat[0].length;
       }
       return 0;
   }
   
   /**
    * erzeugt eine Kopie des MultiBeats
    * @return Kopie des MultiBeats
    */
    @Override
   public MultiBeat clone(){
       MultiBeat clone = new MultiBeat();
       NoteData[][] nd = new NoteData[beat.length][beat[0].length];
       for (int i=0; i<beat.length; i++){
           for (int j=0; j<beat[i].length; j++){
               nd[i][j] = beat[i][j];
           }
       }
       clone.setBeat(nd);
       clone.setInstrument(instrument);
       clone.setKey(key);
       clone.setMajor(major);
       return clone;
   }
}
