package viewController;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import model.*;

public class SwingCalculator extends JFrame {

  private CalculatorModel model = new SimpleCalculatorModel();
  private JTextField lcd = new JTextField();
  private String value = "";

  public SwingCalculator() {

    super("Swing Calculator");

    // Layout the calculator
    Container c = getContentPane();
    c.setLayout(new BorderLayout());

    // Add the LCD panel
    lcd.setEnabled(false);
    lcd.setHorizontalAlignment(JTextField.RIGHT);
    lcd.setFont(lcd.getFont().deriveFont(Font.BOLD, 18.0f));

    c.add(lcd, BorderLayout.NORTH);
    
    // Layout the buttons
    JPanel panel = new JPanel(new GridLayout(4,4));

    // Add the Actions & Button
    panel.add(new JButton(new NumberAction("1")));
    panel.add(new JButton(new NumberAction("2")));
    panel.add(new JButton(new NumberAction("3")));

    panel.add(new JButton(new OperatorAction("+")));

    panel.add(new JButton(new NumberAction("4")));
    panel.add(new JButton(new NumberAction("5")));
    panel.add(new JButton(new NumberAction("6")));

    panel.add(new JButton(new OperatorAction("-")));

    panel.add(new JButton(new NumberAction("7")));
    panel.add(new JButton(new NumberAction("8")));
    panel.add(new JButton(new NumberAction("9")));

    panel.add(new JButton(new OperatorAction("/")));

    panel.add(new JButton(new NumberAction("0")));
    panel.add(new JButton(new NumberAction(".")));
    panel.add(new JButton(new OperatorAction("=")));   

    panel.add(new JButton(new OperatorAction("*")));

    c.add(panel);
    
  }

  // Update the text
  private void numberPressed(String number) {   

    if(value.length() > 0 || !number.equals("0")) {

      String s = value + number;

      try {

        model.setOperand(Double.parseDouble(s));
        lcd.setText(s);
        value = s;

      } catch(NumberFormatException e) { /* NaN */ }

    }

  }

  // Do some math
  private void operatorPressed(String number) {  
    
    if(number.equals("+")) 
      model.operatorAddition();
    else if(number.equals("-")) 
      model.operatorSubtraction();
    else if(number.equals("/")) 
      model.operatorDivision();
    else if(number.equals("*"))
      model.operatorMultiplication();    
    else
      model.operatorEquals();    

    lcd.setText(String.valueOf(model.getResult()));
    value = "";

  }

  class NumberAction extends AbstractAction {

    public NumberAction(String number) {
      super(number);
    }

    public void actionPerformed(ActionEvent e) {
      numberPressed(String.valueOf(getValue(NAME)));
    }

  }

  class OperatorAction extends AbstractAction {

    public OperatorAction(String s) {
      super(s);
    }

    public void actionPerformed(ActionEvent e) {
      operatorPressed(String.valueOf(getValue(NAME)));
    }

  }

  public static void main(String[] args) {
    
    SwingCalculator calc = new SwingCalculator();

    calc.setSize(new Dimension(200,250));
    calc.setDefaultCloseOperation(EXIT_ON_CLOSE);
    calc.setVisible(true);

  }
 
}
