package gui;

import java.awt.*;
// import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.Border;

import calc.*;

/** A frame for the Calculator.
 * @author <Your name here>
 */
public class CalculatorFrame extends JFrame {
    /**
     * Initialize this frame.
     */
    public CalculatorFrame() {
        setTitle("Formula-Register Calculator");
        setSize(WIDTH, HEIGHT);
        
        Container contentPane = getContentPane();
        GridBagLayout layout = new GridBagLayout();
        contentPane.setLayout(layout);
        
        // construct the components
        
        // textBox for output
        outputText = new JTextField(TEXT_NUMBER_SIZE);
        outputText.setEditable(false);
        outputText.setHorizontalAlignment(JTextField.RIGHT);
        Border lined = BorderFactory.createLineBorder(Color.BLUE);
        Border outputTitle 
             = BorderFactory.createTitledBorder(lined, "Output");
        outputText.setBorder(outputTitle);
        
        // Formulas
        // label for them
        JLabel nfLabel = new JLabel("Named Formulas");
        
        // Registers
        // label for them
        JLabel regsLabel = new JLabel("Registers");
        
        for (int i = 0; i < Registers.NUM_REGS; i++) {
            regLabels[i] = new JLabel("r" + i);
            regTexts[i] = new JTextField(TEXT_NUMBER_SIZE);
            regTexts[i].setEditable(true);
            regTexts[i].setText(Double.toString(Registers.getInstance().get(i)));
            regTexts[i].setHorizontalAlignment(JTextField.RIGHT);
            Border rb = BorderFactory.createLineBorder(Color.BLACK);
            regTexts[i].setBorder(rb);
            
            // Add Listener for events on this text field
            RegisterInputListener ril
                 = new RegisterInputListener(regTexts[i], i);
            regTexts[i].addActionListener(ril);
            regTexts[i].addFocusListener(ril);
        }
 

        // Formula Composition
        // label for it
        JLabel fcLabel = new JLabel("Formula Composition");
        
        // add components to the grid
        
        GridBagConstraints constraints = new GridBagConstraints();
        
        constraints.weightx = 100;
        constraints.weighty = 100;
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.CENTER;
        
        add(outputText, constraints, 0, 0, 7, 1);
        add(nfLabel, constraints, 0, 1, 7, 1);  
        add(regsLabel, constraints, 0, 3, 7, 1); 
        add(fcLabel, constraints, 0, 9, 7, 1);
          
        for (int i = 0; i < Registers.NUM_REGS; i = i + 2) {
            constraints.anchor = GridBagConstraints.EAST;
            int row = 4+i/2;
            add(regLabels[i], constraints, 0, row, 1, 1);
            add(regLabels[i+1], constraints, 4, row, 1, 1);
            constraints.anchor = GridBagConstraints.WEST;
            constraints.fill = GridBagConstraints.NONE;
            add(regTexts[i], constraints, 1, row, 2, 1);
            add(regTexts[i+1], constraints, 5, row, 2, 1);
        }
 
        
    }
    
    /**
     * Add the given component to the given grid bag layout.
     * This simplifies otherwise tedious code.  See Core Java 2,
     * Volume 1, pp. 521-522.
     * @param c the component to add
     * @param constraints the grid bag constraints object to use
     * @param x the gird x position
     * @param y the grid y position
     * @param w the grid width (columns spanned)
     * @param h the grid height (rows spanned)
     */
    private void add(Component c, GridBagConstraints constraints,
                     int x, int y, int w, int h)
    {
        constraints.gridx = x;
        constraints.gridy = y;
        constraints.gridwidth = w;
        constraints.gridheight = h;
        getContentPane().add(c, constraints);  
    }
    
    /**
     * Set the output to the given number.
     * @param d the number to appear in the output box
     */
    public void setOutput(double d) {
        outputText.setText(Double.toString(d));
    }
    /** The output text of the calculation. */
    private JTextField outputText;

    /** The size of text fields holding numbers */
    private final int TEXT_NUMBER_SIZE = 25;
        
    /** The labels for the registers. */
    private JLabel regLabels[] = new JLabel[Registers.NUM_REGS];
    /** The text fields for the registers. */
    private JTextField regTexts[] = new JTextField[Registers.NUM_REGS];
    
    /** The width of this frame, in pixels. */
    public static final int WIDTH = 700;
    /** The height of this frame, in pixels. */
    public static final int HEIGHT = 600;
}
