package tmcsim.cadsimulator.viewer;

import java.io.PrintWriter;
import java.io.Writer;
import java.util.Observable;
import tmcsim.cadsimulator.viewer.model.CADSimulatorModel;
import tmcsim.cadsimulator.viewer.model.CADSimulatorStatus;
import tmcsim.common.CADEnums.PARAMICS_STATUS;
import static tmcsim.common.CADEnums.SCRIPT_STATUS.ATMS_SYNCHRONIZATION;
import static tmcsim.common.CADEnums.SCRIPT_STATUS.NO_SCRIPT;
import static tmcsim.common.CADEnums.SCRIPT_STATUS.SCRIPT_PAUSED_STARTED;
import static tmcsim.common.CADEnums.SCRIPT_STATUS.SCRIPT_RUNNING;
import static tmcsim.common.CADEnums.SCRIPT_STATUS.SCRIPT_STOPPED_NOT_STARTED;
import tmcsim.interfaces.CADViewer;

/**
 * This class provides a console to view current status information for the CAD
 * Simulator.
 *
 * @see SimulationStatusPanel
 * @see MediaStatusPanel
 * @author Matthew Cechini
 * @version $Revision: 1.3 $ $Date: 2006/06/06 20:46:41 $
 */
@SuppressWarnings("serial")
public class CADConsoleViewer implements CADViewer
{

    /**
     * Simulation model
     */
    private CADSimulatorModel cadstatus;
    private PrintWriter display;

    /**
     * Constructor.
     */
    public CADConsoleViewer()
    {
        display = new PrintWriter(System.out, true);
    }

    public void setWriter(Writer writer)
    {
        display = new PrintWriter(writer, true);
    }

    public void setVisible(boolean state)
    {
        display.print("--- CAD Simulator ---\n");
        display.print("Elapsed Simulation Time     : 0:00:00\n");
        display.print("Status                      : No Script\n");
        display.print("Connected CAD Terminals     : 0\n");
        display.print("Simulation Manager Connected: No\n");
        display.print("Connected to Paramics       : No\n");
        display.print("Network Loaded              : None\n");
        display.print("-- Info Messages --\n\n");
        display.println("-- Error Messages --\n");
    }

    /**
     * Method calls the processEvent() method with a WINDOW_CLOSING WindowEvent
     * to start the application closing process.
     */
    public void closeViewer()
    {
        System.exit(0);
    }

    @Override
    public void update(Observable obs, Object obj)
    {
        if (obs instanceof CADSimulatorStatus)
        {
            CADSimulatorStatus cadstatus = (CADSimulatorStatus) obs;
            display.println("--- CAD Simulator ---");
            display.println("Elapsed Simulation Time     : " + cadstatus.getCurrentTime());
            String statusTxt = "No Script";
            switch (cadstatus.getScriptStatus())
            {
                case NO_SCRIPT:
                    statusTxt = "No Script";
                    break;
                case SCRIPT_STOPPED_NOT_STARTED:
                    statusTxt = "Ready";
                    break;
                case SCRIPT_PAUSED_STARTED:
                    statusTxt = "Paused";
                    break;
                case SCRIPT_RUNNING:
                    statusTxt = "Running";
                    break;
                case ATMS_SYNCHRONIZATION:
                    statusTxt = "Synchronizing";
                    break;
            }
            display.println("Status                      : " + statusTxt);
            display.println("Connected CAD Terminals     : " + cadstatus.getNumClients());
            String yesno = "No";
            if (cadstatus.isSimManagerConnected())
            {
                yesno = "Yes";
            }
            display.println("Simulation Manager Connected: " + yesno);
            statusTxt = "No";
            if (cadstatus.getParamicsStatus().equals(PARAMICS_STATUS.CONNECTED))
            {
                statusTxt = "Yes";
            }
            display.println("Connected to Paramics       : " + statusTxt);
            String idTxt = "None";
            if (cadstatus.getParmicsNetworkID().length() > 0)
            {
                idTxt = cadstatus.getParmicsNetworkID();
            }
            display.println("Network Loaded              : " + idTxt);
            display.println("-- Info Messages --");
            display.println(cadstatus.getInfoMessages());
            display.println("-- Error Messages --");
            display.println(cadstatus.getErrorMessages());
        }
//        if (obs instanceof CADMediaStatus)
//        {
//            mediaPanel.refresh(obs);
//        }
    }
}
