/*
 * Created on Mar 3, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Jenny
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class POS {

	final static JFrame frame = new JFrame("The Store");
	static POS app = new POS();
	int[] pinID = new int[10];
	int i = 0;
	
	public static void main(String[] args){
		
		Component contents = app.createInsertWindow();
		
		frame.getContentPane().add(contents, BorderLayout.CENTER);
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		frame.pack();
		frame.setVisible(true);
	}
	
	public Component createInsertWindow(){
		
		//Need to check if card is inserted
		final JButton insertCard = new JButton("Insert Card to Continue");
		final JPanel pane = new JPanel();
		pane.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Insert Card"),
                BorderFactory.createEmptyBorder(50,50,50,50)));

		
		pane.setLayout(new GridLayout(4, 2));
		
		pane.add(insertCard);
		insertCard.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					
					frame.remove(pane);
					frame.repaint();
					
					Component content = app.createPinWindow();
					frame.getContentPane().add(content, BorderLayout.CENTER);
					frame.setVisible(true);
				}
		});
		
		
		return pane;
	}
	
	public Component createPinWindow(){
		
		//Get pin from card
	
		final JLabel deny = new JLabel("Incorrect PIN ID");
		final JPanel pane = new JPanel();
		pane.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Enter PIN ID"),
                BorderFactory.createEmptyBorder(50,50,50,50)));

		pane.setLayout(new GridBagLayout());

		final GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		
		final JButton button1 = new JButton("1");
		c.gridx = 0;
		c.gridy = 1;
		pane.add(button1, c);
		button1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pane.remove(deny);
				pane.updateUI();
				pinID[i] = 1;
				i++;
			}
		});				
		final JButton button2 = new JButton("2");
		c.gridx = 1;
		c.gridy = 1;
		pane.add(button2, c);
		button2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pane.remove(deny);
				pane.updateUI();
				pinID[i] = 2;
				i++;
			}
		});	
		
		final JButton button3 = new JButton("3");
		c.gridx = 2;
		c.gridy = 1;
		pane.add(button3, c);
		button3.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pane.remove(deny);
				pane.updateUI();
				pinID[i] = 3;
				i++;
			}	
		});	
	
		final JButton button4 = new JButton("4");
		c.gridx = 0;
		c.gridy = 2;
		pane.add(button4, c);
		button4.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pane.remove(deny);
				pane.updateUI();
				pinID[i] = 4;
				i++;
			}
		});	
			
		final JButton button5 = new JButton("5");
		c.gridx = 1;
		c.gridy = 2;
		pane.add(button5, c);
		button5.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pane.remove(deny);
				pane.updateUI();
				pinID[i] = 5;
				i++;
			}
		});	
		
		final JButton button6 = new JButton("6");
		c.gridx = 2;
		c.gridy = 2;
		pane.add(button6, c);
		button6.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pane.remove(deny);
				pane.updateUI();
				pinID[i] = 6;
				i++;
			}
		});	
		
		final JButton button7 = new JButton("7");
		c.gridx = 0;
		c.gridy = 3;
		pane.add(button7, c);
		button7.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pane.remove(deny);
				pane.updateUI();
				pinID[i] = 7;
				i++;
			}
		});	
		
		final JButton button8 = new JButton("8");
		c.gridx = 1;
		c.gridy = 3;
		pane.add(button8, c);
		button8.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pane.remove(deny);
				pane.updateUI();
				pinID[i] = 8;
				i++;
			}
		});	
		
		final JButton button9 = new JButton("9");
		c.gridx = 2;
		c.gridy = 3;
		pane.add(button9, c);
		button9.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pane.remove(deny);
				pane.updateUI();
				pinID[i] = 9;
				i++;
			}
		});	
		
		final JButton button0 = new JButton("0");
		c.gridx = 1;
		c.gridy = 4;
		pane.add(button0, c);
		button0.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				pane.remove(deny);
				pane.updateUI();
				pinID[i] = 0;
				i++;
			}
		});	
		
		final JButton enter = new JButton("Enter");
		c.anchor = GridBagConstraints.PAGE_END;
		c.insets = new Insets(10,0,0,0);
		c.gridx = 0;
		c.gridwidth = 3;
		c.gridy = 5;
		pane.add(enter, c);
		enter.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				
				if(pinID[0]==1 && pinID[1]==2 && pinID[2]==3&&pinID[3]==4){
					frame.remove(pane);
					frame.repaint();
				
					Component content = app.createPaymentWindow();
					frame.getContentPane().add(content, BorderLayout.CENTER);
					frame.setVisible(true);
				}
				else{
					i=0;
					
					c.anchor = GridBagConstraints.PAGE_START;
					c.gridx = 0;
					c.gridy = 0;
					c.insets = new Insets(10,10,10,10);
					pane.add(deny, c);
					pane.updateUI();
				}
			}
		});
		
		return pane;
		
	}
	
	public Component createPaymentWindow(){
		
		//Need to check what options of payment are available
		final JPanel pane = new JPanel();
		pane.setLayout(new BorderLayout());
		pane.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Select Payment Type"),
                BorderFactory.createEmptyBorder(50,50,50,50)));
	
		final JLabel total = new JLabel("Total: $78.00");
		total.setPreferredSize(new Dimension(10, 20));
		pane.add(total, BorderLayout.PAGE_START);
		
		final JButton cash = new JButton("Cash");
		cash.setPreferredSize(new Dimension(10, 50));
		pane.add(cash, BorderLayout.CENTER);
		cash.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				frame.remove(pane);
				frame.repaint();
				
				Component content = app.cashPaymentWindow();
				frame.getContentPane().add(content, BorderLayout.CENTER);
				frame.setVisible(true);
			}
		});	
		
		final JButton credit = new JButton("Credit");
		credit.setPreferredSize(new Dimension(10, 40));
		pane.add(credit, BorderLayout.PAGE_END);
	
		return pane;
		

	}
	public Component cashPaymentWindow(){
		
		final JPanel pane = new JPanel();
		pane.setLayout(new BorderLayout());
		pane.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Verify Payment"),
				BorderFactory.createEmptyBorder(50,50,50,50)));
		
		final JLabel total = new JLabel("Total Purchase: $78.00");
		total.setPreferredSize(new Dimension(10, 20));
		pane.add(total, BorderLayout.PAGE_START);
		
		final JLabel cardAmount = new JLabel("Amount on Card: $150.00");
		total.setPreferredSize(new Dimension(10, 20));
		pane.add(cardAmount, BorderLayout.CENTER);
		
		final JButton ok = new JButton("OK to make payment");
		total.setPreferredSize(new Dimension(10, 20));
		pane.add(ok, BorderLayout.PAGE_END);
		ok.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				frame.remove(pane);
				frame.repaint();
				
				Component content = app.thankWindow();
				frame.getContentPane().add(content, BorderLayout.CENTER);
				frame.setVisible(true);
			}
		});	
		
		return pane;
	}

	public Component thankWindow(){
		
		final JPanel pane = new JPanel();
		pane.setLayout(new BorderLayout());
		pane.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Thank You"),
				BorderFactory.createEmptyBorder(50,50,50,50)));
		
		final JLabel cardAmount = new JLabel("Amount on Card: $72.00");
		cardAmount.setPreferredSize(new Dimension(10, 20));
		pane.add(cardAmount, BorderLayout.PAGE_START);
		
		final JLabel payment = new JLabel("Transaction Complete");
		payment.setPreferredSize(new Dimension(10, 20));
		pane.add(payment, BorderLayout.CENTER);
		
		return pane;
	}
	
}
