/**
 * Class CalculatorGui - write a description of the class here
 * 
 * @author: Kleanthis Thramboulidis
 * date: 
 */

import java.awt.*;
import java.awt.event.*;
//import Calc.java;

public class CalculatorGui extends Frame
{
      public MyDigitButton button0, button1, button2, button3, button4, button5;
      public MyDigitButton button6, button7, button8, button9;
      public Button buttonBs, buttonEnter;
      public Button buttonAnd, buttonSub, buttonEq;
      public TextField display;
      public Operand op;
      
      public CalculatorGui(Operand op) {
	   super("JAVA Polish Reverse Calculator");

		this.op = op;
      this.setFont(new Font("TimesRoman", Font.PLAIN, 12));
      this.setBackground(Color.blue);
      
		this.setSize(new Dimension(283,297));
      this.setLocation(50,100);      

      this.setVisible(true);
      this.toFront();            
      this.setResizable(false);  
      this.addWindowListener(new CloseWindowAndExit());
      
      MyDigitButton.cg = this;
      button0=new MyDigitButton("0",64, 265, 35, 28);
      button1=new MyDigitButton("1",64, 232, 35, 28);
      button2=new MyDigitButton("2",104, 232, 35, 28);
      button3=new MyDigitButton("3",144, 232, 35, 28);	
      button4=new MyDigitButton("4",64, 199, 35, 28);	
      button5=new MyDigitButton("5",104, 199, 35, 28);	
      button6=new MyDigitButton("6",144, 199, 35, 28);	
      button7=new MyDigitButton("7",64, 166, 35, 28);	
		button8=new MyDigitButton("8",104, 166, 35, 28);	
		button9=new MyDigitButton("9",144, 166, 35, 28);	
		
      buttonBs=new Button("BackSpace");
 		this.add(buttonBs);
		buttonBs.setBounds(new Rectangle(64, 100, 75, 28));
		buttonBs.setFont(new Font("TimesRoman",Font.PLAIN,14));
		buttonBs.setBackground(Color.cyan);
		buttonBs.setForeground(Color.blue);		
		buttonBs.addActionListener(new BsButtonHandler(this,"BackSpace"));
      		
		buttonEnter=new Button("Enter");
 		this.add(buttonEnter);
		buttonEnter.setBounds(new Rectangle(204, 100, 56, 28));
		buttonEnter.setFont(new Font("TimesRoman",Font.PLAIN,14));
		buttonEnter.setBackground(Color.cyan);
		buttonEnter.setForeground(Color.blue);		
		buttonEnter.addActionListener(new EnterButtonHandler(this,"Enter"));      		

      OperatorButton.myCg = this;
  		buttonAnd = new OperatorButton("+",195, 265,35,28);
 		buttonAnd.addActionListener(Calc.ad);  
  		buttonSub = new OperatorButton("-",195,232,35,28);
 		buttonSub.addActionListener(Calc.sub); 
  		buttonEq = new OperatorButton("=",235,265, 35, 28);
  		buttonEq.addActionListener(Calc.eq); 
		      			
		display = new TextField("0",14);
      this.add(display);
      display.setEditable(false);  
		display.setBounds(new Rectangle(13, 55, 257, 30));
	}  
	

}		



class CloseWindowAndExit extends WindowAdapter
  {
    public void windowClosing (WindowEvent closeWindowAndExit)
     {
      System.exit(0);
     }
  } 


class DigitButtonHandler implements ActionListener {
		public CalculatorGui calcGui;
		String label;

		
		public DigitButtonHandler(CalculatorGui calcGui, String label) {
			this.calcGui = calcGui;
			this.label = label;
		}
		
     	public void actionPerformed(ActionEvent pushingButton){ 
			calcGui.op.addDigit(label.charAt(0));
       	}
   }
   
class BsButtonHandler implements ActionListener {
		public CalculatorGui calcGui;
		String label;

		
		public BsButtonHandler(CalculatorGui calcGui, String label) {
			this.calcGui = calcGui;
			this.label = label;
			}
		
     	public void actionPerformed(ActionEvent pushingButton0){ 
			calcGui.op.deleteLastDigit();
       	}
   }
   
class EnterButtonHandler implements ActionListener {
		public CalculatorGui calcGui;
		String label;

		
		public EnterButtonHandler(CalculatorGui calcGui, String label) {
			this.calcGui = calcGui;
			this.label = label;
			}
		
     	public void actionPerformed(ActionEvent pushingButton){ 
			calcGui.op.complete();
       	}
   }   
  
/*  
class OperatorButtonHandler implements ActionListener {
		char label;
		Operator myOperator;

		
		public OperatorButtonHandler(char label,Operator op) {
			this.label = label;
			myOperator = op;
			}
		
     	public void actionPerformed(ActionEvent pushingButton){ 
     		myOperator.operate();
        	}
   }   
*/
   
class MyDigitButton extends Button {
		String label;
		static CalculatorGui cg;		
		
		public MyDigitButton(String label, int x, int y, int width, int height){   
			super(label);
			this.label = label;
			setBounds(new Rectangle(x, y, width, height));
			setFont(new Font("TimesRoman",Font.PLAIN,14));
			setBackground(Color.cyan);
			setForeground(Color.blue);		
         addActionListener(new DigitButtonHandler(cg,label));
         cg.add(this);
      	}
     	}
     	
     	
class OperatorButton extends Button {
		static CalculatorGui myCg;

		public OperatorButton(String label, int x, int y, int width, int height){       	
			super(label);

			setBounds(new Rectangle(x, y, width, height));
			setFont(new Font("TimesRoman",Font.PLAIN,14));
			setBackground(Color.cyan);
			setForeground(Color.blue);		
         myCg.add(this);
     		}
      }
		
	