import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.BoxLayout;
import javax.swing.border.EmptyBorder;

public class ShowCart extends JDialog {

  JPanel bottom; 
  JButton okButton, updateButton;  
  JPanel cartPanel; 
  JLabel topLabel1;
  JLabel topLabel2;
  JLabel totalLabel;
  JPanel headerPanel;
  int cs;
  CatalogItem[] cl;
  JTextField[] textfields;
  
  public ShowCart(JFrame owner) {
    super(owner,true); 
    setTitle("Your cart");
    okButton = new JButton("Ok"); 
    updateButton = new JButton("Update total"); 
    ButtonHandler bhandler = new ButtonHandler(); 
    okButton.addActionListener(bhandler); 
    updateButton.addActionListener(bhandler);
     
    bottom = new JPanel();
    bottom.setLayout(new BoxLayout(bottom, BoxLayout.Y_AXIS));
    JPanel bbuttonPanel = new JPanel(); 
    bbuttonPanel.add(updateButton); 
    bbuttonPanel.add(okButton);
    JPanel btextPanel = new JPanel();
    totalLabel = new JLabel("Total price: 0");
    btextPanel.add(totalLabel);
    bottom.add(btextPanel);
    bottom.add(bbuttonPanel);
    bottom.setBorder(BorderFactory.createEtchedBorder());
    
    topLabel1 = new JLabel("Item Name");
    topLabel1.setFont(new Font("Arial", Font.ITALIC, 12));
    topLabel1.setBorder(new EmptyBorder(0,5,0,0));
    topLabel2 = new JLabel("Price");
    topLabel2.setFont(new Font("Arial", Font.ITALIC, 12));
    
    cartPanel = new JPanel();
    cartPanel.setLayout(new GridLayout(0,3,20,5));
    
    getContentPane().setLayout(new BorderLayout()); 
    getContentPane().add(bottom, BorderLayout.SOUTH); 
    getContentPane().add(cartPanel, BorderLayout.CENTER);     
    pack(); 
   
  } 
  
  public int totalPrice() {
	  int total = 0;
	  for (int i = 0; i < cs; i++) {
		  total += cl[i].numOrdered * cl[i].price;
	  }
	  return total;
  }
 
  
  public void updateShowCart (int catalogSize, CatalogItem[] catalog) {
	int y = 0;
	JLabel label1, label2;
	JPanel panel;
	JTextField textfield1;
	textfields = new JTextField[catalogSize];
	cs = catalogSize;
	cl = catalog;
	
	cartPanel.removeAll();
	cartPanel.add(topLabel1);
	cartPanel.add(topLabel2);
	cartPanel.add(new JLabel(""));
	for (int i = 0; i < catalogSize; i++) {
		if (catalog[i].numOrdered > 0) {
			panel = new JPanel();
			label1 = new JLabel(catalog[i].name);
			label1.setBorder(new EmptyBorder(0,5,0,0));
			cartPanel.add(label1);
			label2 = new JLabel(Integer.toString(catalog[i].price));
			cartPanel.add(label2);
			textfields[i] = new JTextField(3);
			textfields[i].setText(Integer.toString(catalog[i].numOrdered));
			cartPanel.add(textfields[i]);
		}
	  }
	totalLabel.setText("Total price: " + Integer.toString(totalPrice()));   
	pack();
  }
 
  class ButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
      JButton button = (JButton) evt.getSource(); 
      String label = button.getText(); 
      if ("Ok".equals(label)) {
          setVisible(false);
      } 
      // use "else" if want okay to use last "updated" values
      // (i.e. value entered without hitting "Update total"
      // are ignored.
      //
      //else if ("Update total".equals(label)) {\
      //}
      for (int i = 0; i < cs; i++) {
    		  if (textfields[i] != null) {
    			  if (Integer.parseInt(textfields[i].getText()) < 0) {
    				  textfields[i].setText("0");
    			  }
    			  cl[i].numOrdered = Integer.parseInt(textfields[i].getText());
    		  }
    	  }   	
      totalLabel.setText("Total price: " + Integer.toString(totalPrice()));
    }
  }  

}


