package vermilion;

import java.util.ArrayList;

/**
 * Stores one track (or column) of a pattern. This may be part of the pattern
 * or part of a track group that is part of a pattern. Basically the object
 * exists to store the cues for the track, but it also stores the name of the
 * track, the parameter it targets and for convenience the type of that parameter.
 */
public class PatternTrackNode extends UsefulFileObject
{
    /**
     * The name of this track or column as it will appear in the pattern table.
     */
    public String columnName;

    /**
     * The name of the parameter this track holds cues for. The ID of the
     * machine is found on the pattern node to which this track belongs.
     */
    public String paramName;

    /**
     * The type of parameter this track node holds cues for. Mainly this is a
     * convenience so we don't have to look up the parameter to determine what
     * type of column to show.
     */
    public int paramType;

    public int groupNumber;

    /**
     * The cues for this track, mapped by the row on which they appear.
     */
    public UsefulFileObjectIntMap commands;

    /**
     * Constructs a pattern track given a name for the track, the name of the
     * parameter the track is for, and the type of that parameter.
     */
    public PatternTrackNode(String colName, String parName, int parType, int groupNum)
    {
        columnName = colName;
        paramName = parName;
        paramType = parType;
        groupNumber = groupNum;
        commands = new UsefulFileObjectIntMap();
    }

    /**
     */
    //@Override
    //public String toString()
    //{
    //    return columnName;
    //}

    /**
     * The serialisation type for this class is 'COLUMN'.
     */
    @Override
    public String getType() { return "COLUMN"; }

    /**
     * The current serialisation version for this class is 1.
     * @return
     */
    @Override
    public int getVersion() { return 1; }

    /**
     * Reads a track and its cues from the document.
     */
    @Override
    public UsefulFileObject loadImpl(String str, int version)
    {
        PatternTrackNode node = new PatternTrackNode(null, null, 0, 0);

        ArrayList<String> data = stringToList(str);

        if (data.size() == 5)
        {
            node.columnName = data.get(0);
            node.paramName = data.get(1);
            node.paramType = Integer.parseInt(data.get(2));
            node.groupNumber = Integer.parseInt(data.get(3));
            node.commands = (UsefulFileObjectIntMap) UsefulFileObject.load(data.get(4));
        }

        return node;
    }

    /**
     * Saves the track and its cues to the document.
     */
    @Override
    public String saveImpl()
    {
        ArrayList<String> strs = new ArrayList<String>();

        strs.add(columnName);
        strs.add(paramName);
        strs.add(String.valueOf(paramType));
        strs.add(String.valueOf(groupNumber));
        strs.add(commands.save());

        return listToString(strs);
    }
}
