package vermilion;

import java.util.ArrayList;

/**
 * Represents a wire in the document, by storing all relevant information
 * about the wire. The class extends UsefulFileObject so it can be written to
 * the document.
 */
public class WireNode extends UsefulFileObject
{
    /**
     * The name of the machine the wire we are representing 'reads from'.
     */
    public String input;

    /**
     * The name of the machine the wire we are representing 'writes to'.
     */
    public String output;

    /**
     * The channel selection of the wire we are representing.
     */
    public int channel = 0;

    /**
     * The left volume of the wire we are representing.
     */
    public float amplitudeL = 1.0f;

    /**
     * The right volume of the wire we are representing.
     */
    public float amplitudeR = 1.0f;

    /**
     * Constructs a new node representing the given wire.
     * @param w The wire we wish to represent in the document.
     */
    public WireNode(SimpleWire w)
    {
        if (w == null) return;

        // 'Serialise'
        input = w.input.machineID;
        output = w.output.machineID;
        channel = w.channel;
        amplitudeL = w.amplitudeL;
        amplitudeR = w.amplitudeR;
    }

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

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

    /**
     * Loads a WireNode from the document.
     */
    @Override
    public UsefulFileObject loadImpl(String str, int version)
    {
        WireNode node = new WireNode(null);

        if (version == 1)
        {
            ArrayList<String> data = stringToList(str);

            if (data.size() >= 5)
            {
                node.input = data.get(0);
                node.output = data.get(1);
                node.amplitudeL = Float.parseFloat(data.get(2));
                node.amplitudeR = Float.parseFloat(data.get(3));
                node.channel = Integer.parseInt(data.get((4)));
            }
        }

        return node;
    }

    /**
     * Saves a WireNode to the document.
     */
    @Override
    public String saveImpl()
    {
        ArrayList<String> strs = new ArrayList<String>();

        strs.add(input);
        strs.add(output);
        strs.add(String.valueOf(amplitudeL));
        strs.add(String.valueOf(amplitudeR));
        strs.add(String.valueOf(channel));

        return listToString(strs);
    }
}
