/*
ECE649 Spring 2010
Group 7
Justin Ray/justinr2
(other names would go here)
 */
package simulator.elevatorcontrol;

import jSimPack.SimTime;
import jSimPack.SimTime.SimTimeUnit;
import simulator.framework.*;

import java.util.*;
import java.text.ParseException;

/* This was the header of the old version:
 *
 * @author
 * Charles Shelton
 *
 * REVISION DATES:
 * 01/16/2006 Jen Morris
 * 10/24/2004 Nick Zamora
 * 11/07/2002 Beth Latronico
 * 04/01/2008 Kenny Stauffer
 *
 * This class defines the controller periods for instantiating the whole elevator
 * (makeAll) and also a reflection interface that is used by the simulator to
 * process configuration (.cf) files.
 *
 * Parameters:
 * boolean verbose - whether or not verbose output is printed from the controllers.
 *
 */
public class Control implements Parser {

    private final boolean verbose;
    private final ReflectionFactory factory;

    private Control() {
        verbose = false;
        factory = null;
    }

    private Control(boolean verbose) {
        this.verbose = verbose;
        this.factory = new ReflectionFactory(verbose);
    }

    public void parse(String[] args) throws ParseException {
        if(verbose) Harness.log("Control", "parsing ",Arrays.toString(args));
        args[0] = "simulator.elevatorcontrol." + args[0];
        factory.instantiate(Arrays.asList(args));
    }

    /**
     * Instantiates the controllers.
     *
     * Descriptions of the objects are read from a file.  Each line of
     * the file is expected to contain all of the arguments expected by the
     * Control object's constructor, in the order the Constructor expects them.
     * The first word on each line must be the name of the class of the
     * Controller.  So, one could instantiate a DriveControl object by including
     * a line like this:
     *
     * <pre>
     *      DriveControl 8 100000 true
     * </pre>
     *
     * The effect will be the same as calling the constructor:
     *
     * <pre>
     *      DriveControl(8,100000,true);
     * </pre>
     *
     * This method uses Java reflection, so you need Java 1.5 or later to use it.
     */
    public static void makeFromFile(String filename, boolean verbose) {
        Control control = new Control(verbose);

        FileTokenizer fp = new FileTokenizer(filename, verbose, control);

        fp.parseFile();
    }
    public final static SimTime BUTTON_CONTROL_PERIOD = new SimTime(100, SimTimeUnit.MILLISECOND);
    public final static SimTime COIN_CONTROL_PERIOD = new SimTime(100, SimTimeUnit.MILLISECOND);
    public final static SimTime VEND_POSITION_CONTROL_PERIOD = new SimTime(50, SimTimeUnit.MILLISECOND);
    public final static SimTime VEND_CONTROL_PERIOD = new SimTime(50, SimTimeUnit.MILLISECOND);

    public static void makeAll(boolean verbose) {
        new CoinControl(COIN_CONTROL_PERIOD, verbose);
        new VendControl(VEND_CONTROL_PERIOD, verbose);
        new VendPositionControl(VEND_POSITION_CONTROL_PERIOD, verbose);

        for (int i=0; i < SodaMachine.NUM_CHUTES; i++) {
            int index = i+1;
            new ButtonControl(index, BUTTON_CONTROL_PERIOD, verbose);
        }
    }
}
