/*
 * Created on 2005/02/06
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

/**
 * @author Sakda
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class ContainerTestGUI extends JFrame {

	// container list
	public Container[] box = new Container[25];
	public int nextContainer = 0; // Next container to be added
	String input, param;
	private ContainerPanel pnlDrawContainer;
	static ContainerTestGUI ctg;
	private ButtonClickHandler btnHandlers;
	private ContainerTerminal terminal;
	//	 operation controls
	private JButton btnAdd, btnRemove, btnListCrane, btnListSize,
			btnShowSumWeight, btnQuit;
	private JPanel buttonPanel;
	private JTextArea textArea;
	int crane, size;

	public ContainerTestGUI(ContainerTerminal ct, Container[] cs) {
		super("1.00/1.01 Homework 9 (Spring 2005)");
		terminal = ct;
		box = cs;
		btnHandlers = new ButtonClickHandler();
		btnHandlers.ctg = this;
		java.awt.Container pane = getContentPane();
		pane.setLayout(new BorderLayout(5,5));
		// add buttons
		btnAdd = new JButton("Add a container");
		btnAdd.addActionListener(btnHandlers);
		btnRemove = new JButton("Remove a container");
		btnRemove.addActionListener(btnHandlers);
		btnListCrane = new JButton("List containers of a crane");
		btnListCrane.addActionListener(btnHandlers);
		btnListSize = new JButton("List containers by size");
		btnListSize.addActionListener(btnHandlers);
		btnShowSumWeight = new JButton("Show sum weight of container size");
		btnShowSumWeight.addActionListener(btnHandlers);
		btnQuit = new JButton("Quit application");
		btnQuit.addActionListener(btnHandlers);
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.setSize(900,100);
		buttonPanel.add(btnAdd);
		buttonPanel.add(btnRemove);
		buttonPanel.add(btnListCrane);
		buttonPanel.add(btnListSize);
		buttonPanel.add(btnShowSumWeight);
		pane.add(buttonPanel, BorderLayout.SOUTH);
		pnlDrawContainer = new ContainerPanel(terminal);
		pnlDrawContainer.setSize(900,550);
		pane.add(pnlDrawContainer, BorderLayout.CENTER);
		setSize(900, 650);
		show();
	}

	public static void main(String[] args) {
		// hard-wired listing the arrival list of containers
		Container[] local_box = new Container[25];
		local_box[0] = new Container(10, 4, "a");
		local_box[1] = new Container(10, 9, "b");
		local_box[2] = new Container(20, 23, "c");
		local_box[3] = new Container(20, 7, "d");
		local_box[4] = new Container(40, 8, "e");
		local_box[5] = new Container(40, 6, "f");
		local_box[6] = new Container(40, 17, "g");
		local_box[7] = new Container(40, 25, "h");
		local_box[8] = new Container(20, 31, "i");
		local_box[9] = new Container(10, 9, "j");
		local_box[10] = new Container(40, 8, "k");
		local_box[11] = new Container(40, 27, "l");
		local_box[12] = new Container(20, 8, "m");
		local_box[13] = new Container(10, 16, "n");
		local_box[14] = new Container(20, 33, "o");
		local_box[15] = new Container(40, 7, "p");
		local_box[16] = new Container(10, 8, "q");
		local_box[17] = new Container(20, 9, "r");
		local_box[18] = new Container(10, 23, "s");
		local_box[19] = new Container(10, 39, "t");
		local_box[20] = new Container(40, 34, "u");
		local_box[21] = new Container(10, 25, "v");
		local_box[22] = new Container(10, 27, "w");
		local_box[23] = new Container(40, 15, "x");
		local_box[24] = new Container(10, 18, "y");

		final int CRANES = 6; // Numbered 0-5
		final int SIZES = 3; // Size 0 is 10 ft, size 1 is 20 ft, size 2 is 40
							 // ft
		ContainerTerminal term1 = new ContainerTerminal(CRANES, SIZES);
		boolean quit = false;
		//		 Uncomment this to add containers automatically
		// do add containers to cranes (for testing purpose)
		// so that users can skip manually input for 25 times
		/*for (int i = 0; i < box.length; i++) {
			// add containers to cranes
			int mod = i % CRANES;
			term.add(box[i], mod);
		}*/

		ctg = new ContainerTestGUI(term1, local_box);
		ctg.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	
	private class ButtonClickHandler implements ActionListener
	{
		public ContainerTestGUI ctg;
		
		public void actionPerformed(ActionEvent e)
		{
			// if "Add container" button is clicked
			if (e.getSource() == btnAdd)
			{
				param= JOptionPane.showInputDialog("Crane:");
				crane= Integer.parseInt(param);
				if (ctg.nextContainer < ctg.box.length) {
					ctg.terminal.add(ctg.box[ctg.nextContainer++], crane);
					JOptionPane.showMessageDialog(null, "Container "+ctg.box[ctg.nextContainer-1].id+" added to"+
							" crane line "+crane+"\n"); 
					// redraw
					repaint();
				}
				else
					JOptionPane.showMessageDialog(null, "No more containers available to add\n");
			}
			else if (e.getSource() == btnRemove)
			{
				param= JOptionPane.showInputDialog("ID to remove:");
				boolean success= ctg.terminal.remove(param);
				JOptionPane.showMessageDialog(null, "Container "+param+" removed? "+success+"\n");
				// redraw
				repaint();
			}
			else if (e.getSource() == btnListCrane)
			{
				param= JOptionPane.showInputDialog("Crane:");
				crane= Integer.parseInt(param);
				JOptionPane.showMessageDialog(null, ctg.terminal.printCraneLine(crane));
			}
			else if (e.getSource() == btnListSize)
			{
				param= JOptionPane.showInputDialog("Size:");
				size= Integer.parseInt(param);
				JOptionPane.showMessageDialog(null, ctg.terminal.printContainerSizeLine(size));
			}
			else if (e.getSource() == btnShowSumWeight)
			{
				param= JOptionPane.showInputDialog("Size:");
				size= Integer.parseInt(param);
				int nbr= ctg.terminal.getWeightBySize(size);
				JOptionPane.showMessageDialog(null, "Total weight of containers for size "+size+" : "+nbr+"\n");
			}
			else if (e.getSource() == btnQuit)
			{
				System.exit(0);
			}
		}
	}
}