package vermilion;

import java.util.ArrayList;

/**
 *
 */
public class UtilTrackHandler
{
    private ArrayList<PlaybackEvent> changes = new ArrayList<PlaybackEvent>();

    void handle(PlaybackEvent param)
    {
        changes.add(param);
    }

    ArrayList<ArrayList<PlaybackEvent>> getTrackEvents()
    {
        ArrayList<ArrayList<PlaybackEvent>> trackEvents = new ArrayList<ArrayList<PlaybackEvent>>();

        // Unpack the events.
        for (PlaybackEvent param : changes)
        {
            sortEvent(param, trackEvents);
        }

        changes.clear();

        return trackEvents;
    }

    private void sortEvent(PlaybackEvent param, ArrayList<ArrayList<PlaybackEvent>> trackEvents)
    {
        int targetTrackNum = param.trackNumber;
        boolean trackFound = false;

        for (ArrayList<PlaybackEvent> tEvt : trackEvents)
        {
            int thisTrackNumber = tEvt.get(0).trackNumber;

            if (thisTrackNumber == targetTrackNum)
            {
                trackFound = true;
                tEvt.add(param);
                break;
            }
        }

        if (trackFound == false)
        {
            ArrayList<PlaybackEvent> tEvt = new ArrayList<PlaybackEvent>();
            tEvt.add(param);
            trackEvents.add(tEvt);
        }
    }

}
