package tmcsim.cadsimulator;

import java.io.File;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import static org.mockito.Mockito.*;
import org.uispec4j.*;
import org.uispec4j.interception.WindowInterceptor;
import tmcsim.cadsimulator.managers.ParamicsSimulationManager;
import tmcsim.common.CADEnums;
import tmcsim.common.ScriptException;
import tmcsim.common.SimulationException;
import tmcsim.interfaces.CADClientInterface;
import tmcsim.interfaces.SimulationManagerInterface;

/**
 * Test of CADSimulator GUI
 *
 * @author jdalbey
 */
public class CADSimulatorGUITest extends UISpecTestCase
{

    static final String configData =
            "CADClientPort          = 4444 \n"
            + "CoordinatorRMIPort     = 4445 \n"
            + "CADRmiPort             = 4446 \n"
            + "UserInterface          = tmcsim.cadsimulator.viewer.CADSimulatorViewer\n"
            + "ParamicsProperties     = pconfig.txt\n"
            + "ATMSProperties         = empty.txt\n"
            + "MediaProperties        = empty.txt\n";

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

    @Override
    public void tearDown() throws java.io.IOException
    {
        File removeMe = new File("config.txt");
        removeMe.delete();
        removeMe = new File("pconfig.txt");
        removeMe.delete();
        removeMe = new File("empty.txt");
        removeMe.delete();
    }

    /**
     * Test of getCADTime method, of class CADSimulator.
     */
    public void testGetCADTime()
    {
        System.out.println("getCADTime");
        String result = CADSimulator.getCADTime();
        // Just test length for now
        assertEquals(4, result.length());
    }

    /**
     * Test of getCADDate method, of class CADSimulator.
     */
    public void testGetCADDate()
    {
        System.out.println("getCADDate");
        String result = CADSimulator.getCADDate();
        // Just test length for now
        assertEquals(6, result.length());
    }
    /**
     * Test of main method, of class CADSimulator.
     */
    CADSimulator app;

    public void testConstructor() throws SimulationException, RemoteException, ScriptException
    {
        CADSimulatorFixture.writeConfigData();
        CADSimulatorFixture.writedata("config.txt", configData);
        System.out.println("CADSimulator constructor");
        System.setProperty("CAD_SIM_PROPERTIES", "config.txt");
        Window cadwindow = null;
        if (System.getProperty("CAD_SIM_PROPERTIES") != null)
        {
            cadwindow = WindowInterceptor.run(new Trigger()
            {
                public void run()
                {
                    try
                    {
                        app = new CADSimulator(System.getProperty("CAD_SIM_PROPERTIES"));
                    } catch (Exception e)
                    {
                        fail("Couldn't launch CADSimulator" + e.getMessage());
                    }
                }
            });
        } else
        {
            fail("CAD_SIM_PROPERTIES system property not defined.");
        }
        assertEquals("CAD Simulator", cadwindow.getTitle());
        Panel mainPanel = cadwindow.getPanel("contentPane");
        TextBox txtStatus = mainPanel.getTextBox("simulationStatus");
        assertEquals("No Script", txtStatus.getText());
        TextBox terminals = mainPanel.getTextBox("termConnectedTF");
        assertEquals("0", terminals.getText().trim());
        assertEquals("No", mainPanel.getTextBox("managerConnectedTF").getText().trim());
        assertEquals("0:00:00", mainPanel.getTextBox("simulationClockLabel").getText());

        CADClientInterface ci = new FakeClient();
        app.theCoordinator.registerForCallback(ci);
        assertEquals("1", terminals.getText().trim());
        app.theCoordinator.registerForCallback(ci);
        assertEquals("2", terminals.getText().trim());

        SimulationManagerInterface si = mock(SimulationManagerInterface.class);
        app.theCoordinator.registerForCallback(si);
        assertEquals("Yes", mainPanel.getTextBox("managerConnectedTF").getText().trim());

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

        Panel infoPane = mainPanel.getPanel("infoMessagesPane");
        TextBox infoText = infoPane.getTextBox("infoMessagesTA");
        assertEquals(". = Sample Info Message.", infoText.getText().trim());

        cadSimLogger.logp(Level.SEVERE, "", "", "Sample Error Message.");

        Panel errPane = mainPanel.getPanel("errorMessagesPane");
        TextBox errText = errPane.getTextBox("errorMessagesTA");
        assertEquals(". = Sample Error Message.", errText.getText().trim());

        app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED);
        CADSimulatorFixture.pause(500);
        assertEquals("Yes", mainPanel.getTextBox("paramicsConnectedTF").getText().trim());
        assertEquals("None", mainPanel.getTextBox("networkLoadedTF").getText().trim());

//        app.theCoordinator.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_RUNNING);
//        assertEquals("Running", mainPanel.getTextBox("simulationStatus").getText().trim());

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

        app.theCoordinator.startSimulation();
        CADSimulatorFixture.pause(500);
        assertEquals("Running", mainPanel.getTextBox("simulationStatus").getText().trim());
        assertEquals("0:00:01", mainPanel.getTextBox("simulationClockLabel").getText());

        ParamicsSimulationManager psm = mock(ParamicsSimulationManager.class);
        when(psm.isConnected()).thenReturn(Boolean.TRUE);
        app.theParamicsSimMgr = psm;
        app.theCoordinator.loadParamicsNetwork(1);

//        app.theViewer.dispose();
//        Window confirmPopup = null;
//        confirmPopup = WindowInterceptor.run(new Trigger()
//        {
//            public void run()
//            {
//                app.theViewer.closeViewer();
//            }
//        });
//        confirmPopup.getButton("OK").click();
    }

    class FakeClient implements CADClientInterface
    {

        @Override
        public void refresh() throws RemoteException
        {
        }
    }
}
