/**
   * m = nq + r (m-r) sections would get q bars r sections would get q+1 bars
   *
   * <p>m = the original number of bars n = split q = m/n r = m%n
   */
  public boolean nWaySplit(int index, int split) {
    int m;
    if (index + 1 < size()) m = getSectionMeasure(index + 1) - getSectionMeasure(index);
    else m = measures() - getSectionMeasure(index) + 1;
    // System.out.println("m = " + m + ", split = " + split);
    if (m < split) {
      return false;
    }
    int q = m / split;
    int r = m % split;

    SectionRecord delete = getSectionRecordByIndex(index);
    String styleName = delete.getStyleName();
    int newIndex = delete.getIndex(); // slots
    boolean isPhrase = delete.getIsPhrase();

    records.remove(index);

    for (int j = 0; j < split; j++) {
      if (j != 0) styleName = Style.USE_PREVIOUS_STYLE;
      records.add(index + j, new SectionRecord(styleName, newIndex, isPhrase));
      newIndex = measureToSlotIndex(slotIndexToMeasure(newIndex) + q);
    }

    return true;
  }
 public void reloadStyles() {
   ListIterator<SectionRecord> k = records.listIterator();
   while (k.hasNext()) {
     SectionRecord record = k.next();
     k.remove();
     k.add(new SectionRecord(record.getStyleName(), record.getIndex(), record.getIsPhrase()));
   }
 }
  public SectionInfo extract(int first, int last, ChordPart chords) {
    SectionInfo si = new SectionInfo(chords);

    si.records = new ArrayList<SectionRecord>();

    Iterator<SectionRecord> k = records.iterator();

    while (k.hasNext()) {
      SectionRecord record = k.next();
      String styleName = record.getStyleName();
      int index = record.getIndex() - first;
      if (index < 0) {
        si.records.add(new SectionRecord(styleName, 0, record.getIsPhrase()));
      } else if (index <= last - first) {
        si.records.add(new SectionRecord(styleName, index, record.getIsPhrase()));
      }
    }

    return si;
  }
 public int sectionAtSlot(int n) {
   Iterator<SectionRecord> k = records.listIterator();
   while (k.hasNext()) {
     SectionRecord record = k.next();
     if (record.getIndex() == n) {
       if (record.getIsPhrase()) {
         return Block.PHRASE_END;
       } else {
         return Block.SECTION_END;
       }
     }
   }
   return Block.NO_END;
 }
 @Override
 public String toString() {
   StringBuilder buffer = new StringBuilder();
   for (SectionRecord record : records) {
     buffer.append("(");
     buffer.append(record.getStyleName());
     buffer.append(" ");
     buffer.append(record.getIndex());
     buffer.append(" ");
     buffer.append(record.getIsPhrase());
     buffer.append(") ");
     buffer.append("\n");
   }
   return buffer.toString();
 }
  private void saveSectionInfo(BufferedWriter out, SectionRecord record) throws IOException {
    String styleName = record.getUsePreviousStyle() ? "" : " " + record.getStyleName();

    if (record.getIsPhrase()) {
      out.newLine();
      out.write("(phrase (style" + styleName + ")) ");
      out.newLine();
    } else {
      out.newLine();
      out.newLine();
      out.write("(section (style" + styleName + ")) ");
      out.newLine();
      out.newLine();
    }
  }
  public ArrayList<Block> toBlockList() {
    ArrayList<Block> blocks = new ArrayList<Block>();
    int chordsSize = chords.size();

    int endIndex;

    // m is a second iterator intended to stay one step ahead of k
    // so as to get the start of the next section

    ListIterator<SectionRecord> k = records.listIterator();
    ListIterator<SectionRecord> m = records.listIterator();
    if (m.hasNext()) {
      m.next();
    }

    while (k.hasNext()) // && (endLimitIndex == ENDSCORE || endIndex <= endLimitIndex) )
    {
      SectionRecord record = k.next();
      String styleName = record.getStyleName();
      int startIndex = record.getIndex();

      endIndex = m.hasNext() ? m.next().getIndex() : chordsSize;

      ChordBlock block = null;

      for (int slot = startIndex; slot < endIndex; slot++) {
        Chord chord = chords.getChord(slot);

        if (chord != null) {
          block = new ChordBlock(chord.getName(), chord.getRhythmValue());
          block.setStyleName(styleName);
          blocks.add(block);
        }
      }

      // For last block in section
      if (block != null) {
        block.setSectionEnd(record.getIsPhrase() ? Block.PHRASE_END : Block.SECTION_END);
      }
    }

    return blocks;
  }
  /**
   * Returns ArrayList of indices of section starts that are not phrases.
   *
   * @return
   */
  public ArrayList<Integer> getSectionStartIndices() {
    ArrayList<Integer> result = new ArrayList<Integer>();
    boolean isStart = true; // The first Section Record must be a Section Start

    for (SectionRecord record : records) {
      if (isStart == true) {
        result.add(record.getIndex());
      }
      if (!record.getIsPhrase()) {
        // Any Section Record that follows a Section Record that is
        // not a phrase must be a Section Start
        isStart = true;
      } else {
        // Otherwise, if a Section Record is a Phrase, i.e.,
        // ends with a breath mark as opposed to a double bar line,
        // the next Section Record cannot be a Section Start
        isStart = false;
      }
    }
    return result;
  }
  public void newSection(int index) {
    int measureLength = chords.getMeasureLength();

    SectionRecord record = records.get(index);
    int startIndex = record.getIndex();

    int endIndex = chords.size();

    if (index + 1 < size()) {
      SectionRecord nextRecord = records.get(index + 1);
      endIndex = nextRecord.getIndex();
    }

    int measure = (endIndex - startIndex) / measureLength;

    if (measure % 2 == 0) measure /= 2;
    else measure = measure / 2 + 1;

    addSection(
        Style.USE_PREVIOUS_STYLE, startIndex + measure * measureLength, record.getIsPhrase());
  }