package net.mkonrad.synes.data;


public class BeatGraph extends AbstractGraph
{
  /** Specifies an opened graph that has a beginning and an end like a chain */ 
  public final static int STATE_OPENED = 0;
  
  /** Specifies a closed graph that is like a ring */
  public final static int STATE_CLOSED = 1;
  
  public BeatGraph()
  {
    super(STATE_OPENED);
  }

  public Beat getBeat(int index)
  {
    return (Beat)get(index);
  }
  
  /**
   * Generate an array of OSC messages to create / update the pattern for this
   * BeatGraph
   * @param groupID Group-ID of the SC group node. If this is 0, do not add the
   *                beat, but update it!
   *                
   * @return  Array of OSC messages
   */
  public Object[] generateOSCData(int groupID)
  {
    int extra = 0;
    int len = size();
    String msgHeader = (groupID <= 0 ? "updateBeat" : "addBeat") + ";" + _id + ";" + groupID + ";";
        
    if (getState() == STATE_OPENED)
    {
      extra = (len > 2) ? len - 2 : 0;
    }
    
    Object[] data = new Object[len + extra];
    
    int i = 0;
    for (i = 0; i < len; i++)
    {
      data[i] = msgHeader + i + ";" + this.getBeat(i).constructOSCMsgPart();
    }
    
    if (getState() == STATE_OPENED)
    {
      if (len > 2)  //construct the "back way" of the graph
      {
        int c = 0;
        for (int ib = len - 2; ib > 0; ib--)
        {
          data[i+c] = msgHeader + (i+c) + ";" + this.getBeat(ib).constructOSCMsgPart(this.getBeat(ib - 1).getDur());
          c++;
        }
      }
    }
    
    return data;
  }
}
