/*
 * Created on 2005/02/12
 *
 * 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 ContainerPanel extends JPanel  {
	
	final int CRANE_WIDTH = 40;

	final int CRANE_HEIGHT = 40;

	final int CONTAINER_WIDTH = 25;

	final int CONTAINER_HEIGHT = 40;

	final int SPACER = 20;

	final Color CRANE_COLOR = Color.GREEN;

	final Color CONTAINER_COLOR = Color.YELLOW;
	final Color CONTAINER_WEIGHTCOLOR = Color.BLACK;
	final Color CONTAINER_SIZECOLOR = Color.BLACK;

	final Color TEXT_COLOR = Color.WHITE;
	
	int verticalPosition, horizonPosition;
	int crane, size;
	int nextContainer= 0;
	ContainerTerminal term;
	
	public ContainerPanel(ContainerTerminal ct)
	{
		super();
		term = ct;
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		DrawDiagram(g);
	}
	
	/*
	 *  
	 */
	public void DrawDiagram(Graphics g) {
		verticalPosition = 20 + 2 * SPACER;
		horizonPosition = 10 + 2 * SPACER;
		// draw all the cranes
		if (term.colHead != null && term.rows > 0) {
			// add caption
			g.setColor(TEXT_COLOR);
			g.drawString("Cranes", horizonPosition, verticalPosition);
			verticalPosition += 20;
			int leftAlign = horizonPosition;
			for (int count = 0; count < term.rows; count++) {
				horizonPosition = leftAlign;
				// draw cranes
				DrawCrane(g, horizonPosition, verticalPosition, CRANE_COLOR,
						TEXT_COLOR, count);
				horizonPosition += SPACER + CRANE_WIDTH;
				// draw container in a crane
				// draw only when there is container in a row
				if (term.rowHead[count] != null) {
					Container c = (Container) term.rowHead[count];
					DrawContainer(g, horizonPosition, verticalPosition,
							CONTAINER_COLOR, TEXT_COLOR, c, CONTAINER_WEIGHTCOLOR);
					horizonPosition += SPACER + 2*CONTAINER_WIDTH;
					while (c.right != null) {
						c = c.right;
						DrawContainer(g, horizonPosition, verticalPosition,
								CONTAINER_COLOR, TEXT_COLOR, c, CONTAINER_WEIGHTCOLOR);
						horizonPosition += SPACER + 2*CONTAINER_WIDTH;
					}
				}
				verticalPosition = verticalPosition + SPACER + CONTAINER_HEIGHT; // shift
																				 // position
																				 // down
			} // end for
		} // end if
	}

	public void DrawContainer(Graphics g, int x, int y, Color background,
			Color text, Container c, Color cweight) {
		g.setColor(background);
		g.fillRect(x, y, CONTAINER_WIDTH, CONTAINER_HEIGHT);
		// draw container ID
		g.setFont(new Font("Serif", Font.BOLD, 14));
		g.setColor(text);
		g.drawString(c.id, x + CONTAINER_WIDTH / 2 - 1, y + CONTAINER_HEIGHT / 2
				- 1);
		g.setColor(cweight);
		g.fillRect(x+CONTAINER_WIDTH, y, CONTAINER_WIDTH, CONTAINER_HEIGHT);
		// draw container ID
		g.setFont(new Font("Serif", Font.BOLD, 14));
		g.setColor(text);
		g.drawString(String.valueOf(c.weight), x + CONTAINER_WIDTH+ CONTAINER_WIDTH / 2 - 1, y + CONTAINER_HEIGHT / 2
				- 1);
	}

	public void DrawCrane(Graphics g, int x, int y, Color background,
			Color text, int craneID) {
		g.setColor(background);
		g.fillRect(x, y, CRANE_WIDTH, CRANE_HEIGHT);
		// draw crane ID
		g.setFont(new Font("Serif", Font.BOLD, 14));
		g.setColor(text);
		g.drawString(Integer.toString(craneID), x + CRANE_WIDTH / 2 - 1, y
				+ CRANE_HEIGHT / 2 - 1);
	}
}
