package tmcsim.cadsimulator;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import org.uispec4j.*;
import org.uispec4j.interception.WindowInterceptor;
import tmcsim.common.ScriptException;
import tmcsim.common.SimulationException;
import tmcsim.paramicscommunicator.ParamicsCommunicator;
import tmcsim.paramicscommunicator.gui.ParamicsCommunicatorGUI;
import tmcsim.simulationmanager.SimulationManager;

/**
 * Requires manual starting CADSimulator before running this test. System test
 * with emulated Paramics Modeler NO ATMS server.
 *
 * @author jdalbey
 */
public class VisibleSystemDemoDriver extends UISpecTestCase
{

    SimulationManager simMgrApp;

    public VisibleSystemDemoDriver(String testName)
    {
        super(testName);
    }

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
    }

    /**
     * Test of run method, of class ParamicsCommunicator.
     */
    public void testRun() throws ScriptException, SimulationException, RemoteException
    {
        System.out.println("Visible System Test starting.");
        // NOTE: CADSimulator must be started prior to running this test,
        // because it displays a GUI, and if we try to do it inside this test,
        // UISpec will complain about an uncaught window appearing.

        ParamicsCommunicator pc = null;
        pc = new ParamicsCommunicator("config/testConfig/paramics_communicator_config.properties");
        ParamicsCommunicatorGUI theGUI = new ParamicsCommunicatorGUI();
        pc.setGUI(theGUI);
        // Note: Don't set visible ANY windows during UISpec test

        // expect pcomm to say "sleeping"

        Window simMgrWindow = null;
        simMgrWindow = WindowInterceptor.run(new Trigger()
        {
            public void run()
            {
                try
                {
                    simMgrApp = new SimulationManager("config/sim_manager_systest_config.properties");
                } catch (Exception ex)
                {
                    fail("Couldn't launch Simulation Manager, perhaps CADSimulator isn't running.");
                }
            }
        });

        // Check that the Sim Mgr GUI appears without a script loaded yet
        Panel contentPanel = simMgrWindow.getPanel("contentPane");
        TextBox txtSimStatus = contentPanel.getTextBox("simulationStatusText");
        assertEquals("No Script", txtSimStatus.getText());
        TextBox txtParamStatus = contentPanel.getTextBox("paramicsStatusInfoLabel");
        assertEquals("Unknown", txtParamStatus.getText());
        Button loadNetworkBtn = simMgrWindow.getButton("Load Network");
        assertFalse(loadNetworkBtn.isEnabled());
        // Begin actions
        Button paramicsBtn = simMgrWindow.getButton("Connect to Paramics");
        paramicsBtn.click();
        try
        {
            Thread.sleep(200);
        } catch (Exception ex)
        {
        }
        assertEquals("Disconnect from Paramics", paramicsBtn.getLabel());

        assertEquals("Connected", txtParamStatus.getText());
        pc.startReading();

        loadNetworkBtn.click();
        try
        {
            Thread.sleep(200);
        } catch (Exception ex)
        {
        }
        assertEquals("Sending Network ID", txtParamStatus.getText());
        System.out.println("Sending Network ID");
        assertFalse(loadNetworkBtn.isEnabled());
        String warmingXML = "<Paramics>\n"
                + "<Network_Status>WARMING</Network_Status>\n"
                + "<Network_ID>1</Network_ID>\n"
                + "</Paramics>";
        writedata("paramics_status.xml", warmingXML);
        try
        {
            Thread.sleep(2100);
        } catch (Exception ex)
        {
        }
        assertEquals("Warming Up", txtParamStatus.getText());
        System.out.println("Warming Up Passed");

        try
        {
            Thread.sleep(2100);
        } catch (Exception ex)
        {
        }
        String loadedXML = "<Paramics>\n"
                + "<Network_Status>LOADED</Network_Status>"
                + "<Network_ID>1</Network_ID>"
                + "</Paramics>";
        writedata("paramics_status.xml", loadedXML);
        try
        {
            Thread.sleep(2100);
        } catch (Exception ex)
        {
        }
        assertEquals("Network 1 Loaded", txtParamStatus.getText());
        System.out.println("Network Loaded Passed");

        // Load a script file
        String autoloadScriptname = "scripts/audio_systest.xml";
        simMgrApp.loadScript(new File(autoloadScriptname));
        try
        {
            Thread.sleep(500);
        } catch (Exception ex)
        {
        }

        // The status should now say Ready
        assertEquals("Ready", txtSimStatus.getText());

        // Click "Start"
        simMgrWindow.getButton("Start").click();
        try
        {
            Thread.sleep(200);
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
        assertEquals("Running", txtSimStatus.getText());
        System.out.println("Running Passed");
        try
        {
            Thread.sleep(9000);
        } catch (InterruptedException ex)
        {
        }
        simMgrWindow.getButton("Pause").click();
        try
        {
            Thread.sleep(3000);
        } catch (InterruptedException ex)
        {
        }
        simMgrWindow.getButton("Start").click();
        try
        {
            Thread.sleep(3000);
        } catch (InterruptedException ex)
        {
        }
        simMgrWindow.getButton("Pause").click();
        simMgrWindow.getMenuBar().getMenu("File").getSubMenu("Exit").click();
        System.out.println("Exiting.");
    }

    // Write the test data to a file
    public static void writedata(String filename, String data)
    {
//        System.out.println("writedata called for " + filename);
        PrintWriter writer = null;
        try
        {
            writer = new PrintWriter(new FileWriter(filename));
            writer.println(data);
            writer.close();
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}
