/* Author: Jeff Dalton <J.Dalton@ed.ac.uk>
 * Updated: Mon Dec  3 04:08:44 2001 by Jeff Dalton
 * Copyright: (c) 2001, AIAI, University of Edinburgh
 */

package ix.ileed;

import javax.swing.*;

import java.util.*;
import java.io.*;

import ix.icore.*;
import ix.icore.domain.Domain;

import ix.iface.domain.DomainParser;

import ix.iface.util.LogoPanel;

import ix.ideel.IssueManager;
import ix.ideel.IdeelController;
import ix.ideel.IssueViewer;		// not used at present /\/
import ix.ideel.IssueViewingTable;

import ix.util.*;


/** 
 * The generic I-LEED class and application main program 
 */

public class BasicIleed extends IXAgent {

    Domain issueDomain = new Domain();

    IdeelController controller = new IdeelController(this);
 
    ProcessModel processModel = new ProcessModel();
    MessageInterpreter interpreter = new MessageInterpreter(processModel);

    ProcessViewer processViewer = new SimpleProcessViewer();

    IssueViewingTable issueViewer = new IleedIssueViewingTable(this);

    AutoTester autoTester = new AutoTester(this);

    public IleedFrame frame;

    public Object getAgentIPCName() { return ipcName; }
    public Object getAgentSymbolName() { return symbolName; }

    // Some customizable fields
    String displayName = "I-LEED";
    String symbolName = "I-LEED";
    String logoLine1 = "I-X Leader's Event and Execution Display";
    String logoLine2 = "Based on I-X Technology";
    String logoImage = "images/ip2-logo.gif";

    String ipcName = "ILEED";
    String domainName = null;

    public BasicIleed() {
	super();
    }

    /**
     * Main program.
     */
    public static void main(String[] argv) {

	Util.printGreeting("Basic I-LEED");

	new BasicIleed().mainStartup(argv);

    }

    /**
     * Command-line argument processing for arguments used by all
     * versions of I-LEED.
     */
    protected void processCommandLineArguments() {
	if (Parameters.haveParameter("ipc-name")) {
	    ipcName = Parameters.getParameter("ipc-name");
	    Debug.noteln("Using IPC name", ipcName);
	}

	// N.B. need to do this after setting ipcName, because
	// super method sets up IPC.
	super.processCommandLineArguments();

	autoTester.processParameters();

	displayName = Parameters.getParameter("display-name", displayName);
	symbolName  = Parameters.getParameter("symbol-name", symbolName);
	logoLine1   = Parameters.getParameter("logo-line-1", logoLine1);
	logoLine2   = Parameters.getParameter("logo-line-2", logoLine2);
	logoImage   = Parameters.getParameter("logo-image", logoImage);

	if (Parameters.haveParameter("domain"))
	    domainName = Parameters.getParameter("domain");

    }

    /**
     * Completes basic I-LEED setup and initialization.
     */
    public void startup() {

	super.startup();

	addIleedIssueHandlers();

	setupProcessModel();

	// Connect controller and issue-viewer
	issueViewer.setIssueManager(controller);
	controller.addControllerListener(issueViewer);
	controller.setDomain(issueDomain);
	readIssueDomain(issueDomain);

	addIleedInitialIssues();

	// Create the outer GUI
	frame = new IleedFrame(this);

	// Start displaying the monitoring results
	processModel.sendFullStateDescription();

	// Should do this last
	frame.pack();
	frame.setVisible(true);

    }

    protected void setupProcessModel() {
	// processModel.initFromFile("coax-process.lsp");

	// Connect model and viewer.
	// processModel.setViewer(processViewer);
	// processViewer.setProcessModel(processModel);
    }


    /**
     * Read in any default issue-domain description.
     */
    protected void readIssueDomain(Domain domain) {
	if (domainName != null) {
	    try {
		DomainParser.makeParser(domainName).readDomain(domain);
	    }
	    catch (Exception e) {
		Debug.noteException(e);
	    }
	}
    }

    /**
     * Makes the LogoPanel for the application's main frame.
     * This method is in this class to make it easier to define
     * versions that have a different logo panel.
     */
    protected JPanel makeLogoPanel() {
        return new LogoPanel(symbolName, logoLine1, logoLine2, logoImage);
    }

    /**
     * Sets up any issues that should be in the panel at the start.
     */
    protected void addIleedInitialIssues() {
    }


    /**
     * Called to restore the initial state.
     */
    public void reset() {
	// /\/: Things should register for reset instead of being known here.
	autoTester.stopAll(); 	// do this 1st
	processModel.reset();
	interpreter.reset();
	processViewer.reset();
	processModel.sendFullStateDescription();
	controller.reset();	// or should contoller tell issueViewer? /\/
	issueViewer.reset();
	frame.validate();
    }


    /**
     * Handles new issues from external sources.
     */
    public void handleNewIssue(Issue issue) {
        controller.addIssue(issue);
    }

    /**
     * Handles new reports from external sources.
     */
    public void handleNewReport(Report report) {
	controller.newReport(report);
    }


    /**
     * Install any generic issue handlers for issues it can
     * handle automatically.
     */ 
    void addIleedIssueHandlers() {
    }

}
