package vermilion;

import java.util.ArrayList;

/**
 * Stores a pattern and the events that make it up. Unlike machine and wire nodes
 * this class is not just a representation but is also the actual data we work
 * from at runtime. The class extends UsefulFileObject so it can be written to
 * the composition.
 */
public class PatternNode extends UsefulFileObject
{
    public String patternID;
    public int patternLength;
    public int numTracks;
    
    public PatternNode(String name)
    {
        this(name, 64);
    }

    PatternNode(String name, int length)
    {
        patternID = name;
        patternLength = length;
        numTracks = 2;
    }

    @Override
    public String toString()
    {
        return patternID;
    }

    @Override
    public String getType() { return "PATTERN"; }
    @Override
    public int getVersion() { return 1; }

    @Override
    public UsefulFileObject loadImpl(String str, int version)
    {
        PatternNode node = new PatternNode(null);

        ArrayList<String> data = stringToList(str);

        if (data.size() >= 3)
        {
            node.patternID = data.get(0);
            node.patternLength = Integer.parseInt(data.get(1));
            node.numTracks = Integer.parseInt(data.get(2));
        }

        return node;
    }

    @Override
    public String saveImpl()
    {
        ArrayList<String> strs = new ArrayList<String>();

        strs.add(patternID);
        strs.add(String.valueOf(patternLength));
        strs.add(String.valueOf(numTracks));

        return listToString(strs);
    }
}
