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

import java.util.ArrayList;

/**
 *
 * @author Tom
 */
public class Beat {
   private ArrayList<NoteData> notes = new ArrayList<>();
   private int instrument = 0;
   private int Part = 0;
   private int Phrase = 0;
   
   public Beat() {
   }
   
   public void addNote(NoteData note){
      notes.add(note);
   }
   
   public ArrayList<NoteData> getBeat(){
      return notes;
   }
   
   /**
    * gibt die Note an der uebergebenen Position zurueck
    * @param Pos Position der Note
    * @return die Note oder falls an dieser Position keine Note ist null
    */
   public NoteData getNote(int Pos){
       if (Pos < notes.size() && notes.size() > 0){
           return notes.get(Pos);
       } else {
           return null;
       }
   }

    public int getPart() {
        return Part;
    }

    public void setPart(int Part) {
        this.Part = Part;
    }

    public int getPhrase() {
        return Phrase;
    }

    public void setPhrase(int Phrase) {
        this.Phrase = Phrase;
    }

    public int getInstrument() {
        return instrument;
    }

    public void setInstrument(int instrument) {
        this.instrument = instrument;
    }
   
   public boolean isEmpty(){
      return notes.isEmpty();
   }
   
   /**
    * gibt die (belegte) quantisierte Dauer des aktuellen Takts zurueck
    * @return Dauer des belegten Platzes im Takt
    */
   public double getBeatSum(){
      double Sum = 0.0;
      for (NoteData d : notes){
         Sum += d.getQuantizedDuration();
      }
      return Sum;
   }
   
   /**
    * gibt die Anzahl der Noten in dem Takt zurueck
    * @return Anzahl der Noten im Takt
    */
   public int getNoteCount(){
       return notes.size();
   }
   
   @Override
   public String toString(){
       String s = "";
       for (NoteData d : notes){
         s += d.getNote() +d.getQuantizedDuration() +" ";
       }
       return s;
   }
   
   /**
    * liefert die hoechste Note des Takts
    * @return die hoechste Note
    */
   public NoteData getHighestNote(){
       NoteData max = null;
       for (NoteData d : notes){
           if (max == null){
               max = d;
           } else {
               if (max.getPitch() < d.getPitch()){
                   max = d;
               }
           }
       }
       return max;
   }
   
   /**
    * liefert die niedrigste Note des Takts
    * @return die niedrigste Note
    */
   public NoteData getLowestNote(){
       NoteData min = null;
       for (NoteData d : notes){
           if (min == null){
               min = d;
           } else {
               if (min.getPitch() > d.getPitch()){
                   min = d;
               }
           }
       }
       return min;
   }
}
