package tmcsim.cadsimulator.viewer;

import java.util.logging.Level;
import java.util.logging.Logger;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import org.uispec4j.*;
import tmcsim.cadsimulator.viewer.model.CADSimulatorModel;
import tmcsim.common.CADEnums;

/**
 *
 * @author jdalbey
 */
public class CADSimulatorViewModelTest extends junit.framework.TestCase //extends UISpecTestCase
{

    CADSimulatorViewer viewer;
    Window cadwindow;
    CADSimulatorModel model;
    Panel mainPanel;

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

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        viewer = new CADSimulatorViewer();
        cadwindow = new Window(viewer);
        model = new CADSimulatorModel();
        model.addObserver(viewer);
        mainPanel = cadwindow.getPanel("contentPane");
    }

    public void testInitialView()
    {
        String actual;
        assertTrue("Title bar incorrect", cadwindow.getTitle().trim().startsWith("CAD Simulator"));
        TextBox txtStatus = mainPanel.getTextBox("simulationStatus");
        actual = txtStatus.getText().trim();
        assertEquals("simulation status should say No Script", "No Script", actual);
        TextBox terminals = mainPanel.getTextBox("termConnectedTF");
        actual = terminals.getText().trim();
        assertEquals("should be 0 terminals", "0", actual);
        actual = mainPanel.getTextBox("managerConnectedTF").getText().trim();
        assertEquals("mgr connected should be no", "No", actual);
        actual = mainPanel.getTextBox("paramicsConnectedTF").getText().trim();
        assertEquals("paramics connected should be no", "No", actual);
        actual = mainPanel.getTextBox("simulationClockLabel").getText().trim();
        assertEquals("initial time should be 0:00:00", "0:00:00", actual);
        actual = mainPanel.getTextBox("networkLoadedTF").getText().trim();
        assertEquals("network id should be None", "None", actual);
    }

    public void testClients()
    {
        String actual;
        model.connectClient();
        TextBox terminals = mainPanel.getTextBox("termConnectedTF");
        assertEquals(1, model.getNumClients());
        assertEquals("should be 1 terminal", "1", terminals.getText().trim());
        model.connectClient();
        assertEquals("should be 2 terminals", "2", terminals.getText().trim());
        model.disconnectClient();
        assertEquals("should be 1 terminal after disconnect", "1", terminals.getText().trim());
    }

    public void testSimMgr()
    {
        String actual;
        model.setSimManagerStatus(true);
        actual = mainPanel.getTextBox("managerConnectedTF").getText().trim();
        assertEquals("mgr connected should be yes", "Yes", actual);
        assertTrue(model.isSimManagerConnected());
    }

    public void testMessagePanes()
    {
        String actual;
        System.out.println("Tests for message panes.");
        Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator");
        cadSimLogger.logp(Level.INFO, "", "", "Sample Info Message.");
        Panel infoPane = mainPanel.getPanel("infoMessagesPane");
        TextBox infoText = infoPane.getTextBox("infoMessagesTA");
        actual = infoText.getText().trim();
        assertEquals("wrong info msg text", ". = Sample Info Message.", actual);

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

        Panel errPane = mainPanel.getPanel("errorMessagesPane");
        TextBox errText = errPane.getTextBox("errorMessagesTA");
        actual = errText.getText().trim();
        assertEquals("wrong error msg text", ". = Sample Error Message.", actual);
    }

    public void testStatusChg()
    {
        String actual;
        System.out.println("Tests for status changes.");
        model.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED);
        pause(500);
        actual = mainPanel.getTextBox("paramicsConnectedTF").getText().trim();
        assertEquals("paramics connected should be yes", "Yes", actual);
        actual = mainPanel.getTextBox("networkLoadedTF").getText().trim();
        assertEquals("network id should be None", "None", actual);

        model.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_STOPPED_NOT_STARTED);

        // The status should now say Ready
        actual = mainPanel.getTextBox("simulationStatus").getText().trim();
        assertEquals("sim status should be ready", "Ready", actual);

        model.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_RUNNING);
        model.setTime(61L);
        actual = mainPanel.getTextBox("simulationStatus").getText().trim();
        assertEquals("sim status should be running", "Running", actual);
        actual = mainPanel.getTextBox("simulationClockLabel").getText().trim();
        assertEquals("sim time should be 0:01:01", "0:01:01", actual);
    }

    public void testNetworkID()
    {
        String actual;
        // this will tell the model it has a new network ID
        model.setParamicsNetworkLoaded("1");
        model.setParamicsStatus(CADEnums.PARAMICS_STATUS.LOADED);
        pause(500);
        actual = mainPanel.getTextBox("networkLoadedTF").getText().trim();
        assertEquals("network id should be 1", "1", actual);

        //  model.updateDVDStatus(new DVDStatusUpdate("1", false, new Throwable()));

    }

    public static void pause(int millis)
    {
        try
        {
            Thread.sleep(millis);
        } catch (InterruptedException ex)
        {
        }
    }
}
