package tmcsim.cadsimulator;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.TestCase;
import static org.mockito.Mockito.mock;
import static tmcsim.cadsimulator.CADSimulatorFixture.writedata;
import tmcsim.common.CADEnums;
import tmcsim.common.ScriptException;
import tmcsim.common.SimulationException;
import tmcsim.interfaces.CADClientInterface;
import tmcsim.interfaces.SimulationManagerInterface;

/**
 * Driver for CADSimulator Console - output to stdout.
 *
 * @author jdalbey
 */
public class CADSimulatorConsoleDriver extends TestCase
{

    static CADSimulator app;
    static final String configData =
            "CADClientPort          = 4444 \n"
            + "CoordinatorRMIPort     = 4445 \n"
            + "CADRmiPort             = 4446 \n"
            + "UserInterface          = tmcsim.cadsimulator.viewer.CADConsoleViewer\n"
            + "ParamicsProperties     = pconfig.txt\n"
            + "ATMSProperties         = empty.txt\n"
            + "MediaProperties        = empty.txt\n";
    static final String paramicsData = "ParamicsCommHost       = 192.168.251.45\n"
            + "ParamicsCommPort       = 4450\n"
            + "IncidentUpdateInterval = 30\n"
            + "IncidentUpdateFile     = exchange.xml\n"
            + "ParamicsStatusInterval = 15\n"
            + "ParamicsStatusFile     = paramics_status.xml\n"
            + "CameraStatusInterval   = 30\n"
            + "CameraStatusFile       = camera_status.xml\n";
    /*
     * Creating instance of app must be done only once or you get registry
     * bind problems, and code Written in Constructor is Executed
     * before each Test Method
     */

    public static void main(String[] args) throws IOException, SimulationException, ScriptException
    {
        writedata("config.txt", configData);
        writedata("pconfig.txt", paramicsData);
        writedata("empty.txt", "");
        CADSimulatorFixture.writeScriptfiles();
        CADSimulatorFixture.startCADSim();
        (new CADSimulatorConsoleDriver()).testAll();
        System.exit(0);
    }
    // Write the test data to a file

    private void testAll() throws java.io.IOException, SimulationException, ScriptException
    {

        CADClientInterface ci = mock(CADClientInterface.class); //new tmcsim.cadsimulator.CADSimulatorDriver.FakeClient();
        app.theCoordinator.registerForCallback(ci);
        // Convert the output stream into a string we can test.

        app.theCoordinator.registerForCallback(ci);

        SimulationManagerInterface si = mock(SimulationManagerInterface.class);

        app.theCoordinator.registerForCallback(si);

        Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator");
        cadSimLogger.logp(Level.INFO, "", "", "Sample Info Message.");

        app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED);
        CADSimulatorFixture.pause(500);

        // Show loaded, and ALSO update the current network id
        app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.LOADED);
        CADSimulatorFixture.pause(500);

        app.theCoordinator.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_RUNNING);
        CADSimulatorFixture.pause(500);

        cadSimLogger.logp(Level.SEVERE, "Someclass", "Somemethod", "Something bad happened.");

        // Load a script file
        String autoloadScriptname = "scripts/one-incident.xml";
        app.theCoordinator.loadScriptFile(new File(autoloadScriptname));
        // The status should now say Ready


//        ParamicsSimulationManager psm = mock(ParamicsSimulationManager.class);
//        when(psm.isConnected()).thenReturn(Boolean.TRUE, Boolean.FALSE);
//        app.theParamicsSimMgr = psm;
//        app.theCoordinator.connectToParamics();
//        app.theCoordinator.loadParamicsNetwork(1);
//        CADSimulatorFixture.pause(500);
//        app.theCoordinator.disconnectFromParamics();
//        CADSimulatorFixture.pause(500);

        app.theCoordinator.startSimulation();
        CADSimulatorFixture.pause(1500);
        app.theCoordinator.pauseSimulation();
        app = null;
        cleanup();
    }

    private void cleanup()
    {
        File removeMe = new File("config.txt");
        removeMe.delete();
        removeMe = new File("pconfig.txt");
        removeMe.delete();
        removeMe = new File("empty.txt");
        removeMe.delete();
    }
    String expected1 =
            "--- CAD Simulator ---\n"
            + "Elapsed Simulation Time     : 0:00:00\n"
            + "Status                      : No Script\n"
            + "Connected CAD Terminals     : 0\n"
            + "Simulation Manager Connected: No\n"
            + "Connected to Paramics       : No\n"
            + "Network Loaded              : \n"
            + "-- Info Messages --\n\n"
            + "-- Error Messages --\n\n";
    String expected2 =
            "--- CAD Simulator ---\n"
            + "Elapsed Simulation Time     : 0:00:00\n"
            + "Status                      : No Script\n"
            + "Connected CAD Terminals     : 1\n"
            + "Simulation Manager Connected: No\n"
            + "Connected to Paramics       : No\n"
            + "Network Loaded              : \n"
            + "-- Info Messages --\n\n"
            + "-- Error Messages --\n\n";
    String expected3 =
            "--- CAD Simulator ---\n"
            + "Elapsed Simulation Time     : 0:00:00\n"
            + "Status                      : No Script\n"
            + "Connected CAD Terminals     : 2\n"
            + "Simulation Manager Connected: No\n"
            + "Connected to Paramics       : No\n"
            + "Network Loaded              : \n"
            + "-- Info Messages --\n\n"
            + "-- Error Messages --\n\n";
    String expected4 =
            "--- CAD Simulator ---\n"
            + "Elapsed Simulation Time     : 0:00:00\n"
            + "Status                      : No Script\n"
            + "Connected CAD Terminals     : 2\n"
            + "Simulation Manager Connected: Yes\n"
            + "Connected to Paramics       : No\n"
            + "Network Loaded              : \n"
            + "-- Info Messages --\n\n"
            + "-- Error Messages --\n\n";
    String expected5 =
            "--- CAD Simulator ---\n"
            + "Elapsed Simulation Time     : 0:00:00\n"
            + "Status                      : No Script\n"
            + "Connected CAD Terminals     : 2\n"
            + "Simulation Manager Connected: Yes\n"
            + "Connected to Paramics       : No\n"
            + "Network Loaded              : \n"
            + "-- Info Messages --\n"
            + ". = Sample Info Message.\n"
            + "-- Error Messages --\n\n";
    String expected6 =
            "--- CAD Simulator ---\n"
            + "Elapsed Simulation Time     : 0:00:00\n"
            + "Status                      : No Script\n"
            + "Connected CAD Terminals     : 2\n"
            + "Simulation Manager Connected: Yes\n"
            + "Connected to Paramics       : Yes\n"
            + "Network Loaded              : \n"
            + "-- Info Messages --\n"
            + ". = Sample Info Message.\n"
            + "-- Error Messages --\n\n";
    String expected7 =
            "--- CAD Simulator ---\n"
            + "Elapsed Simulation Time     : 0:00:00\n"
            + "Status                      : No Script\n"
            + "Connected CAD Terminals     : 2\n"
            + "Simulation Manager Connected: Yes\n"
            + "Connected to Paramics       : Yes\n"
            + "Network Loaded              : 3\n"
            + "-- Info Messages --\n"
            + ". = Sample Info Message.\n"
            + "-- Error Messages --\n\n";
}
