package vermilion;

import java.util.ArrayList;

/**
 * Represents a machine in the document, by storing all relevant information
 * about the machine. The class extends UsefulFileObject so it can be written
 * to the composition.
 */
public class MachineNode extends UsefulFileObject
{
    /**
     * The name (machine name) of the machine we are representing.
     */
    public String machineName;

    /**
     * The ID (machine ID) of the machine we are representing.
     */
    public String machineID;

    /**
     * The x location on screen of the machine we are representing.
     */
    public int xLoc;

    /**
     * The y location on screen of the machine we are representing.
     */
    public int yLoc;

    /**
     * The 'master' or 'mixer' flag of the machine we are representing.
     */
    public boolean isMaster;

    /**
     * The parameters on the machine, with last-known values.
     */
    //public UsefulFileObjectList parameters;

    /**
     * Constructs a new node representing the given machine.
     * @param m The machine we wish to represent in the document.
     */
    public MachineNode(MachineBase m)
    {
        if (m == null) return;

        // 'Serialise'
        machineName = m.getName();
        machineID = m.machineID;
        xLoc = m.xLoc;
        yLoc = m.yLoc;
        isMaster = m.isMaster;

        //parameters = new UsefulFileObjectList();
        //for (ParameterBase param : m.parameters)
        //{
            //parameters.add(param);
        //}
    }

    @Override
    public String toString()
    {
        return machineID + " (" + machineName + ")";
    }

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

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

    /**
     * Reads a machine and its last known parameter values from serialised form.
     */
    @Override
    public UsefulFileObject loadImpl(String str, int version)
    {
        MachineNode node = new MachineNode(null);

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

            if (data.size() >= 5)
            {
                node.machineName = data.get(0);
                node.machineID = data.get(1);
                node.xLoc = Integer.parseInt(data.get(2));
                node.yLoc = Integer.parseInt(data.get(3));
                if (data.get(4).equals("T")) node.isMaster = true;
            }
        }

        return node;
    }

    /**
     * Saves a machine and its current parameter values to serialised form.
     */
    public String saveImpl()
    {
        ArrayList<String> list = new ArrayList<String>();

        list.add(machineName);
        list.add(machineID);
        list.add(String.valueOf(xLoc));
        list.add(String.valueOf(yLoc));
        if (isMaster) list.add("T");
        else list.add("F");

        return listToString(list);
    }
}
