import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 

import java.io.*;

public class Order extends JFrame {
	
	private static CatalogItem[] catalog;
	private static int catalogSize = 0;
	private static int currentItem = 0;
	
	private static JLabel center;
	private static JButton checkoutButton;
	private static Container mainContainer;
	
  /**
 * @param imageFile
 */
public Order(String catalogFileName) {
	 
	mainContainer = getContentPane();
	NonOrderHandler noh = new NonOrderHandler();
	
	//System.err.println(catalogFileName);
	readCatalogFile(catalogFileName);
	
	setTitle("The 22C022 Store");
	getContentPane().setLayout(new BorderLayout());
     	
    center = new JLabel();
    center.setHorizontalTextPosition(SwingConstants.CENTER); 
    center.setVerticalTextPosition(SwingConstants.BOTTOM);
    
    JPanel bottom = new JPanel(); 
    bottom.setLayout(new FlowLayout());
    JPanel select = new JPanel();
    select.setLayout(new GridLayout(2,1));
    JButton prevButton = new JButton("Prev item");
    JButton nextButton = new JButton("Next item");
    select.add(prevButton);
    select.add(nextButton);
    JButton orderButton = new JButton("Add item to cart");
    orderButton.setToolTipText("cool");
    checkoutButton = new JButton("Checkout");
    checkoutButton.setBackground(Color.gray);
    checkoutButton.setEnabled(false);
    JButton showcartButton = new JButton("Show Cart");
    bottom.add(select);
    bottom.add(orderButton); 
    bottom.add(showcartButton);
    bottom.add(checkoutButton);

    // added this "wrapper panel" because otherwise I 
    // couldn't get center/image label to lay out correctly
    JPanel top = new JPanel(new FlowLayout());
    top.add(center);
    
    getContentPane().add(top, BorderLayout.CENTER);
    getContentPane().add(bottom, BorderLayout.SOUTH);
    
    checkoutButton.addActionListener(new OrderHandler());
    showcartButton.addActionListener(new ShowCartHandler());
    prevButton.addActionListener(noh);
    nextButton.addActionListener(noh);
    orderButton.addActionListener(noh);
    addWindowListener(new AppCloser()); 
    
    updateView();
    pack();
  }

	void readCatalogFile (String catalogFileName) {
		String itemS;
		int itemP;
		String itemI;
		try {
		 FileReader infile = new FileReader(catalogFileName);
	     StreamTokenizer in = new StreamTokenizer(infile);
	     
	     //in.eolIsSignificant(true);
	     in.nextToken();
	     catalog = new CatalogItem[(int)in.nval];
	     in.nextToken();
	     while (in.ttype != in.TT_EOF) {
	    	 itemS = in.sval;
	    	 in.nextToken();
	    	 itemP = (int)in.nval;
	    	 in.nextToken();
	    	 itemI = in.sval;
	    	 //System.err.println(itemS + " " + Integer.toString(itemP)+ " " + itemI);
	    	 catalog[catalogSize] = 
	    		 new CatalogItem(itemS, itemP, itemI);
	    	 catalogSize++;
	    	 in.nextToken();
	     }
		}
		catch (FileNotFoundException e)
			{ System.err.println(e); }
		catch (IOException e)
		    { System.err.println(e); }
	}

  static class AppCloser extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      System.exit(0);
    }
  }

  class OrderHandler implements ActionListener {
    OrderDialog dialog; 
    public void actionPerformed(ActionEvent evt) {
      if (dialog == null) {
        dialog = new OrderDialog(Order.this); 
      }
      dialog.updateOrder(catalogSize, catalog);
      dialog.setVisible(true); 
      updateView();
    }
  }  
  
  
  private static void updateView() {
	  boolean enableCheckout = false;
	  for (int i = 0; i < catalogSize; i++) {
		  if (catalog[i].numOrdered > 0)
			  enableCheckout = true;
	  }
	  if (enableCheckout == true) {
		  checkoutButton.setBackground(Color.red);
		  checkoutButton.setEnabled(true);
	  } else {
		  checkoutButton.setBackground(Color.gray);
		  checkoutButton.setEnabled(false);
	  }
	  center.setIcon(catalog[currentItem].image);
	  center.setText(
			  "Item: " + catalog[currentItem].name 
			  + "      Price: " + Float.toString(catalog[currentItem].price));
	  //getContentPane().add(center, BorderLayout.CENTER);
	 // mainContainer.validate();
  }
  
  class ShowCartHandler implements ActionListener {
	    ShowCart showcart; 
	    public void actionPerformed(ActionEvent evt) {
	      if (showcart == null) {
	        showcart = new ShowCart(Order.this); 
	      }
	      showcart.updateShowCart(catalogSize, catalog);
	      showcart.setVisible(true); 
	      updateView();
	    }
	  }  
  class NonOrderHandler implements ActionListener {
	  public void actionPerformed(ActionEvent evt) {
		  JButton source = (JButton) evt.getSource(); 
		  if (source != null) {
			  //System.err.println("Button pushed: " + source.getText());
			  if (source.getText().equals("Prev item")) {
				  if (currentItem == 0) 
					  currentItem = catalogSize-1;
				  else 
					  currentItem--;
			
			  } else if (source.getText().equals("Next item")) {
				  if (currentItem == catalogSize-1)
					  currentItem = 0;
				  else 
					  currentItem++;

			  } else if (source.getText().equals("Add item to cart")) {
				  catalog[currentItem].numOrdered++;
			  }
			 updateView();
		  }
	  }
  }

  public static void main(String[] args) {
    if (args.length > 0) {
      Order frame = new Order(args[0]);
      frame.setSize(500,500);
      frame.setVisible(true);
    } 
  }
}
