package tmcsim.simulationmanager;

import java.awt.Component;
import java.io.File;
import javax.swing.JButton;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import org.uispec4j.Panel;
import org.uispec4j.TextBox;
import org.uispec4j.Trigger;
import org.uispec4j.UISpecTestCase;
import org.uispec4j.Window;
import org.uispec4j.interception.WindowInterceptor;
import tmcsim.cadsimulator.CADSimulator;
import tmcsim.common.ScriptException;
import tmcsim.common.SimulationException;

/**
 * Smoke Test of system. Start the CAD server, Start the Sim Mgr, see if the Sim
 * Mgr view shows "No Script".
 *
 * @author jdalbey
 */
public class SimulationManagerSmokeTest extends UISpecTestCase
{

    SimulationManager simMgrApp;
    CADSimulator engine;

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

    /**
     * Call constructors for both classes
     */
    public void testBothGUIs() throws ScriptException, SimulationException
    {
        System.out.println("Smoke Test Sim Mgr & CADSimulator");

        Window cadwindow = null;
        System.setProperty("CAD_SIM_PROPERTIES", "config/cad_simulator_smoketest_config.properties");
        if (System.getProperty("CAD_SIM_PROPERTIES") != null)
        {
            cadwindow = WindowInterceptor.run(new Trigger()
            {
                public void run()
                {
                    try
                    {
                        engine = new CADSimulator(System.getProperty("CAD_SIM_PROPERTIES"));
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                        fail("Couldn't launch CADSimulator");
                    }
                }
            });
        }
        else
        {
            fail("CAD_SIM_PROPERTIES system property not defined.");
        }

        // Check CAD Simulator appears with no script and nothing connected
        assertTrue("Window title incorrect", cadwindow.getTitle().startsWith("CAD Simulator"));
        Panel mainPanel = cadwindow.getPanel("contentPane");
        TextBox txtStatus = mainPanel.getTextBox("simulationStatus");
        assertEquals("No Script", txtStatus.getText());
        TextBox terms = mainPanel.getTextBox("termConnectedTF");
        assertEquals("0", terms.getText().trim());
        assertEquals("No", mainPanel.getTextBox("managerConnectedTF").getText().trim());

        Window simMgrWindow = null;
        System.setProperty("SIM_MGR_PROPERTIES", "config/sim_manager_smoketest_config.properties");
        if (System.getProperty("SIM_MGR_PROPERTIES") != null)
        {
            simMgrWindow = WindowInterceptor.run(new Trigger()
            {
                public void run()
                {
                    try
                    {
                        simMgrApp = new SimulationManager(System.getProperty("SIM_MGR_PROPERTIES"));
                    } catch (Exception ex)
                    {
                        fail("Couldn't launch Simulation Manager");
                    }
                }
            });
        }
        else
        {
            fail("SIM_MGR_PROPERTIES system property not defined.");
        }

        // Check that the Sim Mgr GUI appears without a script loaded yet
        SimulationManagerView view = simMgrApp.theSimManagerView;
        assertFalse(view.isSimulationStarted());
        Window win = new Window(view);
        Panel contentPanel = win.getPanel("contentPane");
        TextBox txtSimStatus = contentPanel.getTextBox("simulationStatusText");
        assertEquals("No Script", txtSimStatus.getText());

        assertEquals("Yes", mainPanel.getTextBox("managerConnectedTF").getText().trim());

        // Load a script file
        String autoloadScriptname = "scripts/practice_script_2016.xml";
        SimulationManagerModel simMgrModel = simMgrApp.theSimManagerModel;
        simMgrModel.loadScript(new File(autoloadScriptname));
        // The status should now say Ready
        assertEquals("Ready", txtSimStatus.getText());

        // Click "Start"
        win.getButton("Start").click();
        try
        {
            Thread.sleep(200);
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
        assertEquals("Running", txtSimStatus.getText());

        //List all available buttons
        Component[] buttons = win.getSwingComponents(JButton.class);
        for (Component comp : buttons)
        {
            System.out.println(comp);
        }
        win.getButton("Load Script");
        win.getButton("Pause");
        win.getButton("Disconnect from Paramics");
        win.getButton("Reset");
        win.getButton("Load Network");
        // Quit
        engine = null;
        win.getMenuBar().getMenu("File").getSubMenu("Exit").click();
        //simMgrApp = null;
    }
}
