/**
 * @(#)CounterParamPanel.java
 * @author JLMJ
 * @date 2001-10-14
 */
package minoids1.controls;

import minoids1.Box;
import minoids1.counter.MinoidsCounterMotor;
import java.awt.*;
import java.awt.event.*;


public class CounterParamsPanel extends Panel implements ItemListener
{
    private CounterControlDialog dialog;
    
    private Label minoidsTypeLabel;
    private Choice minoidsTypeChoice;
    private Label minoidsOrderLabel;
    private Choice minoidsOrderChoice;
    private Label boxesForCountLabel;
    private Choice boxesForCountChoice;
    private Label reportLabel;
    private Choice reportChoice;
    
    private Box[] allBoxes;
    private Box onlyThisBox;
    
    public CounterParamsPanel(CounterControlDialog dialog) {
        this.dialog = dialog;
        minoidsTypeLabel = new Label("Type");
        minoidsTypeChoice = new Choice();
        minoidsTypeChoice.addItem(MinoidsCounterMotor.POLYOMINOES_TYPE_STRING);
        minoidsTypeChoice.addItem(MinoidsCounterMotor.POLYOMINOIDS_TYPE_STRING);
        minoidsTypeChoice.addItemListener(this);
        
        minoidsOrderLabel = new Label("Order");
        minoidsOrderChoice = new Choice();
        for (int i=0; i < 10; i++)
            minoidsOrderChoice.addItem(i + "");
        minoidsOrderChoice.addItemListener(this);

        boxesForCountLabel = new Label("Boxes");
        boxesForCountChoice = new Choice();
        boxesForCountChoice.addItemListener(this);
        
        reportLabel = new Label("Report");
        reportChoice = new Choice();
        reportChoice.addItem(MinoidsCounterMotor.COUNT_TRANSLATIONS_REPORT);
        reportChoice.addItem(MinoidsCounterMotor.STORE_TRANSLATIONS_REPORT);
        reportChoice.select(MinoidsCounterMotor.STORE_TRANSLATIONS_REPORT);
        reportChoice.addItemListener(this);

        // init information
        minoidsTypeChoice.select(MinoidsCounterMotor.POLYOMINOES_TYPE_STRING);
        minoidsOrderChoice.select(5);
        
        updateBoxesForCounting();
        
        Panel p1 = new Panel(new BorderLayout());
        p1.add(minoidsTypeLabel, BorderLayout.NORTH);
        p1.add(minoidsTypeChoice, BorderLayout.SOUTH);
        add(p1);
        Panel p2 = new Panel(new BorderLayout());
        p2.add(minoidsOrderLabel, BorderLayout.NORTH);
        p2.add(minoidsOrderChoice, BorderLayout.SOUTH);
        add(p2);
        Panel p3 = new Panel(new BorderLayout());
        p3.add(boxesForCountLabel, BorderLayout.NORTH);
        p3.add(boxesForCountChoice, BorderLayout.SOUTH);
        add(p3);
        Panel p4 = new Panel(new BorderLayout());
        p4.add(reportLabel, BorderLayout.NORTH);
        p4.add(reportChoice, BorderLayout.SOUTH);
        add(p4);
    }
    
    public void setEnabled(boolean enabled) {
        minoidsTypeLabel.setEnabled(enabled);
        minoidsTypeChoice.setEnabled(enabled);
        minoidsOrderLabel.setEnabled(enabled);
        minoidsOrderChoice.setEnabled(enabled);
        boxesForCountLabel.setEnabled(enabled);
        boxesForCountChoice.setEnabled(enabled);
        reportLabel.setEnabled(enabled);
        reportChoice.setEnabled(enabled);
    }

    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() instanceof Choice) {
            Choice c = (Choice)e.getSource();
            if (c == minoidsTypeChoice) {
                updateBoxesForCounting();
            } else if (c == minoidsOrderChoice) {
                updateBoxesForCounting();
            } else if (c == boxesForCountChoice) {
                int i = boxesForCountChoice.getSelectedIndex();
                onlyThisBox = (i == 0) ? null : allBoxes[i - 1];
            } else if (c == reportChoice) {
                String s = c.getSelectedItem();
                if (s.equals(MinoidsCounterMotor.COUNT_TRANSLATIONS_REPORT))
                    dialog.setSelectorsPanelEnabled(false);
                else if (s.equals(MinoidsCounterMotor.STORE_TRANSLATIONS_REPORT))
                    dialog.setSelectorsPanelEnabled(true);
            }
        }
    }

    private void updateBoxesForCounting() {
        allBoxes = null;
        String type = minoidsTypeChoice.getSelectedItem();
        int order = minoidsOrderChoice.getSelectedIndex();
        if (type.equals(MinoidsCounterMotor.POLYOMINOES_TYPE_STRING)) {
            allBoxes = Box.getAllRotationsRectangles(order);
        } else if (type.equals(MinoidsCounterMotor.POLYOMINOIDS_TYPE_STRING)) {
            allBoxes = Box.getAllRotationsBoxes(order);
        }
        if (allBoxes != null) {
            boxesForCountChoice.removeAll();
            boxesForCountChoice.addItem("All");
            for (int i=0; i < allBoxes.length; i++)
                boxesForCountChoice.addItem(allBoxes[i].toShortString());
        }
    }
    
    // Updates the type choice with: Mixed, Hard or Soft
    // acording internal data.
    public void updateConnectionsTypeChoice(Choice connectionTypeChoice) {
        Choice c = connectionTypeChoice;
        if (c == null)
            return;
        String t = minoidsTypeChoice.getSelectedItem();
        if (t.equals(MinoidsCounterMotor.POLYOMINOES_TYPE_STRING)) {
            c.removeAll();
            c.addItem(MinoidsCounterMotor.SOFT_CONNECTIONS_TYPE_STRING);
        } else if (t.equals(MinoidsCounterMotor.POLYOMINOIDS_TYPE_STRING)) {
            c.removeAll();
            c.addItem(MinoidsCounterMotor.ANY_CONNECTIONS_TYPE_STRING);
            c.addItem(MinoidsCounterMotor.HARD_CONNECTIONS_TYPE_STRING);
            c.addItem(MinoidsCounterMotor.SOFT_CONNECTIONS_TYPE_STRING);
        }
    }
    
    public MinoidsCounterMotor getMotor() {
        return new MinoidsCounterMotor(
            minoidsTypeChoice.getSelectedItem(),
            minoidsOrderChoice.getSelectedIndex(),
            reportChoice.getSelectedItem(),
            allBoxes,
            onlyThisBox);
    }
    
    public String getMinoidsType() {
        return minoidsTypeChoice.getSelectedItem();
    }
    
    public int getMinoidsOrder() {
        return minoidsOrderChoice.getSelectedIndex();
    }
    
    public Box[] getCountedBoxes() {
        return allBoxes;
    }
    
    public String getReportChoiceSelection() {
        return reportChoice.getSelectedItem();
    }

    
}




