import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextField;


public class CalculadoraApplet extends JApplet implements ActionListener {

	private static final long serialVersionUID = 3221707769370092517L;
	Container container;
	JTextField datos;
	JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
	JButton opSuma, opResta, opMult, opDiv;
	JButton punto, calcular, limpiar;
	Stack<String> pila;
	char operacion;
	boolean operacionTerminada=false;
	
	public void init(){
		container = this.getContentPane();
		container.setLayout( new GridLayout(6,1) );
		
		pila = new Stack<String>();
		
		Font fuente = new Font("Verdana", Font.BOLD, 18);
		
		/* Formato del campo de entrada */
		datos = new JTextField();
		datos.setFont(fuente);
		datos.setEditable(false);
		
		limpiar = new JButton("Limpiar");
		
		Container renglon2 = new Container();
		renglon2.setLayout( new GridLayout(1,4) );
		
		Container renglon3 = new Container();
		renglon3.setLayout( new GridLayout(1,4) );
		
		Container renglon4 = new Container();
		renglon4.setLayout( new GridLayout(1,4) );
		
		Container renglon5 = new Container();
		renglon5.setLayout( new GridLayout(1,4) );
		
		renglon2.add(b1 = new JButton("1"));
		renglon2.add(b2 = new JButton("2"));
		renglon2.add(b3 = new JButton("3"));
		renglon2.add(opSuma = new JButton("+"));
		renglon3.add(b4 = new JButton("4"));
		renglon3.add(b5 = new JButton("5"));
		renglon3.add(b6 = new JButton("6"));
		renglon3.add(opResta = new JButton("-"));
		renglon4.add(b7 = new JButton("7"));
		renglon4.add(b8 = new JButton("8"));
		renglon4.add(b9 = new JButton("9"));
		renglon4.add(opMult = new JButton("*"));
		renglon5.add(punto = new JButton("."));
		renglon5.add(b0 = new JButton("0"));
		renglon5.add(calcular = new JButton("="));
		renglon5.add(opDiv = new JButton("/"));
		
		/* Aplicando fuente a todos los botones */
		b1.setFont(fuente);
		b2.setFont(fuente);
		b3.setFont(fuente);
		b4.setFont(fuente);
		b5.setFont(fuente);
		b6.setFont(fuente);
		b7.setFont(fuente);
		b8.setFont(fuente);
		b9.setFont(fuente);
		b0.setFont(fuente);
		opSuma.setFont(fuente);
		opResta.setFont(fuente);
		opMult.setFont(fuente);
		opDiv.setFont(fuente);
		punto.setFont(fuente);
		calcular.setFont(fuente);
		
		/* Agregando acción a todos los botones */
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		b5.addActionListener(this);
		b6.addActionListener(this);
		b7.addActionListener(this);
		b8.addActionListener(this);
		b9.addActionListener(this);
		b0.addActionListener(this);
		opSuma.addActionListener(this);
		opResta.addActionListener(this);
		opMult.addActionListener(this);
		opDiv.addActionListener(this);
		punto.addActionListener(this);
		calcular.addActionListener(this);
		limpiar.addActionListener(this);
		
		
		container.add(datos);
		container.add(renglon2);
		container.add(renglon3);
		container.add(renglon4);
		container.add(renglon5);
		container.add(limpiar);
		
	}

	public void actionPerformed(ActionEvent e) {
		
		if(operacionTerminada){
			datos.setText("");
			operacionTerminada = false;
		}
		
		if( e.getSource()==b1 ){
			datos.setText(datos.getText()+1);
		}
		else if( e.getSource()==b2 ){
			datos.setText(datos.getText()+2);
		}
		else if( e.getSource()==b3 ){
			datos.setText(datos.getText()+3);
		}
		else if( e.getSource()==b4 ){
			datos.setText(datos.getText()+4);
		}
		else if( e.getSource()==b5 ){
			datos.setText(datos.getText()+5);
		}
		else if( e.getSource()==b6 ){
			datos.setText(datos.getText()+6);
		}
		else if( e.getSource()==b7 ){
			datos.setText(datos.getText()+7);
		}
		else if( e.getSource()==b8 ){
			datos.setText(datos.getText()+9);
		}
		else if( e.getSource()==b9 ){
			datos.setText(datos.getText()+9);
		}
		else if( e.getSource()==b0 ){
			datos.setText(datos.getText()+0);
		}
		else if( e.getSource()==punto ){
			//Sólo si no contiene punto lo agrega y sólo que no sea vacío
			if( !datos.getText().contains(".") && !datos.getText().equals("") ) 
				datos.setText(datos.getText()+".");
		}
		else if(e.getSource()==opSuma){
			preparaOperacion('+');
		}
		else if(e.getSource()==opResta){
			preparaOperacion('-');
		}
		else if(e.getSource()==opMult){
			preparaOperacion('*');
		}
		else if(e.getSource()==opDiv){
			preparaOperacion('/');
		}
		else if(e.getSource()==calcular){
			calculaOperacion();
		}
		else if( e.getSource()==limpiar ){
			datos.setText("");
			pila.removeAllElements();
		}
	}

	private void calculaOperacion() {
		if( !datos.getText().equals("") && !pila.isEmpty() ){
			if(operacion=='+')
				datos.setText( Double.toString( Double.parseDouble( pila.pop() ) + Double.parseDouble( datos.getText() ) ));
			else if(operacion=='-')
				datos.setText( Double.toString( Double.parseDouble( pila.pop() ) - Double.parseDouble( datos.getText() ) ));
			else if(operacion=='*')
				datos.setText( Double.toString( Double.parseDouble( pila.pop() ) * Double.parseDouble( datos.getText() ) ));
			else if(operacion=='/')
				datos.setText( Double.toString( Double.parseDouble( pila.pop() ) / Double.parseDouble( datos.getText() ) ));
		}
		
		operacionTerminada = true;
			
	}

	private void preparaOperacion(char c) {
		operacion = c;
		System.out.println("operacion: "+operacion);
		if(!datos.getText().equals(""))
			pila.push(datos.getText());
		
		datos.setText("");
	}

}

