package tmcsim.cadsimulator.viewer;

import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.TestCase;
import tmcsim.cadsimulator.viewer.model.CADSimulatorModel;
import tmcsim.common.CADEnums;

/**
 *
 * @author jdalbey
 */
public class CADConsoleViewerTest extends TestCase
{

    CADConsoleViewer console;
    CADSimulatorModel cadmodel;
    StringWriter sw;

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

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        console = new CADConsoleViewer();
        cadmodel = new CADSimulatorModel();
        cadmodel.addObserver(console);
        sw = new StringWriter();
        console.setWriter(sw);
    }
    /**
     * compare StringWriter contents against expected
     */
    private void verify(String msg, String expect)
    {
        String result = sw.toString().trim();
        result = result.replaceAll("\n", ",");
        String fullExpect = expect.trim();
        fullExpect = fullExpect.replaceAll("\n", ",");
        assertTrue(msg + ": " + result, result.endsWith(fullExpect));
    }

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

    /**
     * Test of setVisible method, of class CADConsoleViewer.
     */
    public void testSetVisible()
    {
        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              : None\n"
                + "-- Info Messages --\n\n"
                + "-- Error Messages --";
        System.out.println("setVisible");
        console.setVisible(true);
        String out = sw.toString().trim();
        assertEquals(expected1, out);
    }

    public void testConnect()
    {
        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              : None\n"
                + "-- Info Messages --\n\n"
                + "-- Error Messages --\n\n";
        cadmodel.connectClient();
        verify("connected 1 terminal output incorrect: ", expected2);
    }

    public void testConnect2()
    {
        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              : None\n"
                + "-- Info Messages --\n\n"
                + "-- Error Messages --\n\n";
        cadmodel.connectClient();
        cadmodel.connectClient();
        verify("connected 2 terminals output incorrect: ", expected3);
    }

    public void testDisconnect()
    {
        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              : None\n"
                + "-- Info Messages --\n\n"
                + "-- Error Messages --\n\n";
        cadmodel.connectClient();
        cadmodel.connectClient();
        cadmodel.connectClient();
        cadmodel.disconnectClient();
        verify("disconnect terminal output incorrect: ", expected3);
    }

    public void testSimMgrConnect()
    {
        String expected4 =
                "--- CAD Simulator ---\n"
                + "Elapsed Simulation Time     : 0:00:00\n"
                + "Status                      : No Script\n"
                + "Connected CAD Terminals     : 0\n"
                + "Simulation Manager Connected: Yes\n"
                + "Connected to Paramics       : No\n"
                + "Network Loaded              : None\n"
                + "-- Info Messages --\n\n"
                + "-- Error Messages --\n\n";
        cadmodel.setSimManagerStatus(true);
        verify("sim mgr connected output incorrect: ", expected4);
    }

    public void testInfoPane()
    {
        String expected5 =
                "--- 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              : None\n"
                + "-- Info Messages --\n"
                + ". = Console Info Message.\n"
                + "-- Error Messages --\n\n";
        Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator");
        cadSimLogger.logp(Level.INFO, "", "", "Console Info Message.");
        verify("Info message output incorrect: ", expected5);
    }

    public void testErrorPane()
    {
        String expected7 =
                "--- 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              : None\n"
                + "-- Info Messages --\n\n"
                + "-- Error Messages --\n"
                + "Someclass.Somemethod = Something bad happened.\n";
        Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator");
        cadSimLogger.logp(Level.SEVERE, "Someclass", "Somemethod", "Something bad happened.");
        verify("Error message output incorrect: ", expected7);
    }

    public void testParamicsChg()
    {
        String expected6 =
                "--- 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       : Yes\n"
                + "Network Loaded              : None\n"
                + "-- Info Messages --\n\n"
                + "-- Error Messages --\n\n";
        cadmodel.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED);
        pause(500);
        verify("paramics connected should be yes", expected6);
    }

    public void testSimStatus()
    {
        String expected8 =
                "--- CAD Simulator ---\n"
                + "Elapsed Simulation Time     : 0:00:00\n"
                + "Status                      : Ready\n"
                + "Connected CAD Terminals     : 0\n"
                + "Simulation Manager Connected: No\n"
                + "Connected to Paramics       : No\n"
                + "Network Loaded              : None\n"
                + "-- Info Messages --\n\n"
                + "-- Error Messages --\n\n";
        cadmodel.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_STOPPED_NOT_STARTED);
        verify("sim status should be ready", expected8);
        cadmodel.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_RUNNING);
        String result = sw.toString();
        assertTrue("sim status should be running: " + result, result.contains("Running"));
    }

    public void testSimTime()
    {
        String expected9 =
                "--- CAD Simulator ---\n"
                + "Elapsed Simulation Time     : 0:01:01\n"
                + "Status                      : No Script\n"
                + "Connected CAD Terminals     : 0\n"
                + "Simulation Manager Connected: No\n"
                + "Connected to Paramics       : No\n"
                + "Network Loaded              : None\n"
                + "-- Info Messages --\n\n"
                + "-- Error Messages --\n\n";
        cadmodel.setTime(61L);
        verify("sim time should be 0:01:01", expected9);
    }

    public void testNetworkID()
    {
        String expected10 =
                "--- 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              : 17\n"
                + "-- Info Messages --\n\n"
                + "-- Error Messages --\n\n";
        // this will tell the model it has a new network ID
        cadmodel.setParamicsNetworkLoaded("17");
        cadmodel.setParamicsStatus(CADEnums.PARAMICS_STATUS.LOADED);
        pause(500);
        verify("network id should be 17", expected10);
    }
}
