import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Font;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Rectangle;
import java.text.NumberFormat;

public class ATM2 {
	
	// these must be declared here to be seen in the Action classes
	Graphics2D thePen;
	JTextField numberField;
	JLabel verifyLabel;
	BankAccount theAccount;
	double theAmount;
	int width;
	int height;
	NumberFormat currency;
	
	public ATM2() {
						
		// The window to hold the panel
		JFrame frame = new JFrame("ATM");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocation(0,0);
				
		// the main panel
		JPanel thePanel = new JPanel();
		width = 500;
		height = 300;
		thePanel.setPreferredSize(new Dimension(width,height));
		thePanel.setBackground(Color.LIGHT_GRAY);
		thePanel.setLayout(new FlowLayout(FlowLayout.CENTER,200,20));
		
		// all other componenets are added to the panel here
		
		// the input label and input field will be added to an input panel
		JPanel inputPanel = new JPanel();
		
		JLabel numberLabel = new JLabel("Type Amount and Press Enter");
		numberField = new JTextField(10);
		numberField.addActionListener(new NumberFieldAction());
		inputPanel.add(numberLabel);
		inputPanel.add(numberField);
		
		JPanel verifyPanel = new JPanel();
		verifyLabel = new JLabel("No Amount Entered");
		verifyPanel.add(verifyLabel);
		
		// the buttons will be added to a button panel
		JPanel buttonPanel = new JPanel();
		
		JButton initialButton = new JButton("Get New Account");
		initialButton.addActionListener(new InitialButtonAction());
		buttonPanel.add(initialButton);
		
		JButton depositButton = new JButton("Deposit the Amount");
		depositButton.addActionListener(new DepositButtonAction());
		buttonPanel.add(depositButton);
		
		JButton withdrawButton = new JButton("Withdraw the Amount");
		withdrawButton.addActionListener(new WithdrawButtonAction());
		buttonPanel.add(withdrawButton);
		
		JPanel drawPanel = new JPanel();
		drawPanel.setPreferredSize(new Dimension(width,height/2));
		drawPanel.setBackground(Color.LIGHT_GRAY);

		// Add the button panel and the draw panel to the main panel
		thePanel.add(inputPanel);
		thePanel.add(verifyPanel);
		thePanel.add(buttonPanel);		
		thePanel.add(drawPanel);
		
		// To see the panel it must be "added" to the frame
		// and set visible
		frame.getContentPane().add(thePanel);
		frame.pack();
		frame.setVisible(true);
		
		// This line gets the "pen" (Graphics2D object) to draw 
		// on the draw panel. It must be 'gotten" after the frame
		// is set visible
		thePen = (Graphics2D)drawPanel.getGraphics();		
		thePen.setFont(new Font("monospaced",Font.BOLD,20));
		
		// get the currency format for the current country
		currency = NumberFormat.getCurrencyInstance();
		
	} // end constructor
	
	class NumberFieldAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// these are the statments that are executed when enter is pressed in the numberField
			String input = numberField.getText();
			if( input.equals("") ) {
				JOptionPane.showMessageDialog(null,"Nothing was entered. Try again.");
				numberField.requestFocus();
				return;
			} // end if
			// the trim method removes beginning and trailing blanks
			input = input.trim();
			boolean isNumber = true;
			for( int i = 0; i < input.length(); i++ ) {
				if( !Character.isDigit(input.charAt(i)) ) {
					isNumber = false;
				} // end if
			} // end for
			if( !isNumber )	{
				JOptionPane.showMessageDialog(null,"Non-digits were entered. Try again.");
				numberField.requestFocus();
				return;
			} // end if
			theAmount = Double.parseDouble(input);
			verifyLabel.setText("Amount Entered was " + currency.format(theAmount));
			numberField.setText("");
			numberField.requestFocus();
		} // end actionPerformed
	} // end NumberButtonAction
	
	class InitialButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// these are the statments that are executed when the sum Button is clicked
			theAccount = new BankAccount(theAmount);
			theAmount = 0.0;
			verifyLabel.setText("No Amount Entered");			
			// clear the number field
			numberField.requestFocus();
			thePen.setColor(Color.LIGHT_GRAY);
			thePen.fill(new Rectangle(0,0,width,height/2));
			thePen.setColor(Color.RED);
			thePen.drawString("The balance is " + currency.format(theAccount.getBalance()) + ".",120,30);
		} // end actionPerformed
	} // end InitialButtonAction
	
	class DepositButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// these are the statments that are executed when the sum Button is clicked
			if( theAccount == null ) {
				JOptionPane.showMessageDialog(null,"Get an Account First.");
				numberField.requestFocus();
				return;
			} // end if
			theAccount.deposit(theAmount);
			theAmount = 0.0;
			verifyLabel.setText("No Amount Entered");			
			thePen.setColor(Color.LIGHT_GRAY);
			thePen.fill(new Rectangle(0,0,width,height/2));
			thePen.setColor(Color.RED);
			thePen.drawString("The balance is " + currency.format(theAccount.getBalance()) + ".",120,30);
		} // end actionPerformed
	} // end DepositButtonAction
	
	class WithdrawButtonAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// this statment is executed when the clear Button is clicked
			if( theAccount == null ) {
				JOptionPane.showMessageDialog(null,"Get an Account First.");
				numberField.requestFocus();
				return;
			} // end if
			theAccount.withdrawal(theAmount);
			theAmount = 0.0;
			verifyLabel.setText("No Amount Entered");			
			thePen.setColor(Color.LIGHT_GRAY);
			thePen.fill(new Rectangle(0,0,width,height/2));
			thePen.setColor(Color.RED);
			thePen.drawString("The balance is " + currency.format(theAccount.getBalance()) + ".",120,30);
		} // end actionPerformed
	} // end WithDrawalButtonAction
		
	public static void main(String[] args) {
		new ATM2();		
	} // end main
	
} // end ATM2
