/**
 * @(#)CountersControlPanel.java
 * @author JLMJ
 * @date 2001-10-14
 */
package minoids1.controls;

import minoids1.Minoid;
import minoids1.Box;
import minoids1.counter.MinoidsCounterEvent;
import minoids1.counter.MinoidsCounterListener;
import minoids1.counter.MinoidsCounterMotor;
import minoids1.viewer.MinoidsViewerPanel;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.Vector;

public class CounterControlDialog extends AbstractControlDialog
    implements ActionListener, MinoidsCounterListener
{
    private MinoidsHtmlReporter reporter;

    private MinoidsCounterMotor motor;
    private Box[] boxes;
    
    private CounterParamsPanel counterParamsPanel;
    private CounterSelectorsPanel counterSelectorsPanel;

    private Button startButton;
    private Button stopButton;
    private Label countingStateLabel;
    
    public CounterControlDialog(
        MinoidsViewerPanel viewer, Minoid[] minoids,
        Frame parent, Applet applet)
    {
        super(parent);
        if (applet != null && applet.getParameter("javascript").equals("yes")) {
            reporter = new MinoidsHtmlReporter(applet, this);
        }
        setTitle("Counter Control");
        
        counterParamsPanel = new CounterParamsPanel(this);
        counterParamsPanel.setEnabled(true);
        counterSelectorsPanel = new CounterSelectorsPanel(this, viewer);
        counterSelectorsPanel.setEnabled(true);
        
        setLayout(new BorderLayout());
        add(counterParamsPanel, BorderLayout.NORTH);
        add(getCenterPanel(), BorderLayout.CENTER);
        add(counterSelectorsPanel, BorderLayout.SOUTH);
        pack();
    }

    private Panel getCenterPanel() {
        startButton = new Button("Start");
        startButton.setActionCommand("start");
        startButton.addActionListener(this);
        stopButton = new Button("Stop");
        stopButton.setActionCommand("stop");
        stopButton.addActionListener(this);
        countingStateLabel = new Label("                     ");
        Panel p = new Panel();
        p.add(startButton);
        p.add(stopButton);
        p.add(countingStateLabel);
        return p;
    }

    public void actionPerformed(ActionEvent e) {
        String action = e.getActionCommand();
        if (action.equals("start")) {
            startCounting();
        } else if (action.equals("stop")) {
            motor.stop();
        }
    }
    
    private void startCounting() {
        counterSelectorsPanel.removeAllBoxes();
        counterParamsPanel.updateConnectionsTypeChoice(
            counterSelectorsPanel.getTypeChoice());
        motor = counterParamsPanel.getMotor();
        motor.addMinoidsCounterListener(this);
        motor.start();
    }
    
    private final String COUNTING_IN_PROGRESS_STATE = "Counting in Progress...";
    private final String COUNTING_ABORTED_STATE = "Counting Aborted";
    private final String COUNTING_COMPLETED_STATE = "Counting Completed";
    
    public void changeCountingState(String message, boolean countInProgress) {
        startButton.setEnabled(!countInProgress);
        countingStateLabel.setText(message);
        stopButton.setEnabled(countInProgress);
        counterParamsPanel.setEnabled(!countInProgress);
        pack();
        if (null != reporter)
            reporter.updateJavaScriptReport("<br>" + message + "<br>");
    }
    
    public void countingStarted() {
        changeCountingState(COUNTING_IN_PROGRESS_STATE, true);
    }
    
    public void anyMinoidsCountingFinished(MinoidsCounterEvent e) {
        changeCountingState(COUNTING_IN_PROGRESS_STATE, true);
    }
    
    public void hardMinoidsCountingFinished(MinoidsCounterEvent e) {
        changeCountingState(COUNTING_IN_PROGRESS_STATE, true);
    }
    
    public void softMinoidsCountingFinished(MinoidsCounterEvent e) {
        changeCountingState(COUNTING_IN_PROGRESS_STATE, true);
    }
    
    public void boxCountingFinished(Box box) {
        String s = counterParamsPanel.getReportChoiceSelection();
        if (s.equals(MinoidsCounterMotor.STORE_TRANSLATIONS_REPORT))
            counterSelectorsPanel.addBox(box);
        pack();
    }

    public void countingAborted() {
        changeCountingState(COUNTING_ABORTED_STATE, false);
    }
    
    public void countingFinished() {
        changeCountingState(COUNTING_COMPLETED_STATE, false);
        if (reporter != null)
            reporter.popupWindow();
    }
    
    public void updateMinoids() {
        //counterSelectorsPanel.updateMinoids();
    }

    public int getMinoidsOrder() {
        return counterParamsPanel.getMinoidsOrder();
    }
    
    public Box[] getBoxes() {
        return counterParamsPanel.getCountedBoxes();
    }
    
    public String getMinoidsType() {
        return counterParamsPanel.getMinoidsType();
    }
    
    public MinoidsCounterMotor getMinoidsCounterMotor() {
        return motor;
    }

    public MinoidsCounterEvent getMinoidsEvent(String boxShortString, String type) {
        if (motor == null)
            return null;
        Box[] boxes = getBoxes();
        for (int i=0; i < boxes.length; i++) {
            if (boxes[i].toShortString().equals(boxShortString)) {
                MinoidsCounterEvent mce = null;
                if (type.equals(MinoidsCounterMotor.ANY_CONNECTIONS_TYPE_STRING)) {
                    mce = motor.getAnyMinoidsCounterEvent(i);
                } else if (type.equals(MinoidsCounterMotor.HARD_CONNECTIONS_TYPE_STRING)) {
                    mce = motor.getHardMinoidsCounterEvent(i);
                } else if (type.equals(MinoidsCounterMotor.SOFT_CONNECTIONS_TYPE_STRING)) {
                    mce = motor.getSoftMinoidsCounterEvent(i);
                }
                return mce;
            }
        }
        return null;
    }
    
    public void setSelectorsPanelEnabled(boolean enabled) {
        counterSelectorsPanel.setEnabled(enabled);
    }
}





