import java.awt.*;
import java.applet.*;

/**  class IfConnector
 *   This class creates the connectors for the alternative branches
 *   The width, height and connector location for each diagram in
 *   a branch are required as parameters
 *   The constructor puts the information into arrays for painting
 *   Painting takes into consideration where each each connector
 *   needs to be in order to link correctly to the diagrams
 *   Constructors exist for 2, 3, 4, and an array of diagrams
 */

public class IfConnector extends Diagram{

	private int connectorWidth;
	private int connectorHeight;
	private int [] connectorPlace;
	private int [] height;
	private int num;
	private String type;

	// the constructors connectors for 2, 3 or 4 diagrams
	// each receive as parameters the connector location and
	// height of each diagram being connected
	// the first parameter indicates whether the connector
	// is at the "begin"ning or "end"
	// connector locations and heights are put in an array for
	// ease of handling in the paint method
	// the total connector height (the sum of the heights
	// plus vertical spacing) and width (connectorSize as
	// defined in the base class) are set for use in
	// the preferred size method

	//constructor for 2 diagrams
	public IfConnector (String direction,
					int c1, int h1,
					int c2, int h2){
		height = new int [2];
		connectorPlace = new int [2];
		connectorLocation = c1;
		type = direction;
		height[0] = h1;
		height[1] = h2;
		connectorPlace [0] = c1;
		connectorPlace [1] = c2;
		num = 2;
		connectorWidth = connectorSize;
		connectorHeight = height[0] + height[1] + verticalSpacing;

	}

	//constructor for 3 diagrams
	public IfConnector (String direction,
					int c1, int h1,
					int c2, int h2,
					int c3, int h3){
		height = new int [3];
		connectorPlace = new int [3];
		connectorLocation = c1;
		type = direction;
		height[0] = h1;
		height[1] = h2;
		height[2] = h3;
		connectorPlace [0] = c1;
		connectorPlace [1] = c2;
		connectorPlace [2] = c3;
		num = 3;
		connectorWidth = connectorSize;
		connectorHeight =  height[0] + height[1]
							+ height[2] + 2 * verticalSpacing;

	}

	// constructor for 4 diagrams
  	public IfConnector (String direction,
					int c1, int h1,
					int c2, int h2,
					int c3, int h3,
					int c4, int h4){
		height = new int [4];
		connectorPlace = new int [4];
		connectorLocation = c1;
		type = direction;
		height[0] = h1;
		height[1] = h2;
		height[2] = h3;
		height[3] = h4;
		connectorPlace [0] = c1;
		connectorPlace [1] = c2;
		connectorPlace [2] = c3;
		connectorPlace [3] = c4;
		num = 4;
		connectorWidth = connectorSize;
		connectorHeight =  height[0] + height[1]
							+ height[2] + height[3] +
							3 * verticalSpacing;

	}

	//constructor for an array of diagrams
	// similar to other constructors
	// arrays sent as parameters are copied to the
	// private array
	// connector height is calculated with a loop
	public IfConnector (String direction,
					int [] c, int [] h,
					int size){
		height = new int [size];
		connectorPlace = new int [size];
		connectorLocation = c[0];
		System.arraycopy(c, 0, connectorPlace,0,size);
		System.arraycopy(h, 0, height,0,size);
		type = direction;
		num = size;
		connectorWidth = connectorSize;
		connectorHeight =  verticalSpacing * (size - 1);
		for (int i = 0; i < size; i++){
			connectorHeight += h[i];
		}
	}

	// paint method uses information about the type of connector
	// (begin or end) to created arcs and lines to make the diagram
	// pieces that connect the inner diagrams to surrounding diagrams

	public void paint (Graphics g){
		int currentLine = connectorPlace [0];
		int i = 1;

		//connect to next diagram
		//round edge to go down (up)
		g.drawLine (0, currentLine, connectorSize, currentLine);
		if (type.equals("begin")){
			g.drawArc ( -connectorSize / 2, currentLine, connectorSize,
				connectorSize, 0, 90);
		}
		else{
			g.drawArc (connectorSize / 2, currentLine, connectorSize,
					connectorSize, 180,-90);
		}
		//if not the first, a line to connect to previous line
		// then a line to next choice
		// then a curve to connect to next diagram
		// and a horizontal line to connect
		while (i < num){
			int verticalLine = height[i-1] - connectorPlace[i-1]
							+ connectorPlace[i] + verticalSpacing;

			int beginLine = currentLine + verticalSpacing;

			if (i > 1){
				g.drawLine(connectorSize / 2, currentLine - connectorSize / 2,
					connectorSize / 2, currentLine + connectorSize / 2);
			}
			g.drawLine (connectorSize / 2, currentLine + connectorSize / 2,
					connectorSize / 2, currentLine + verticalLine - connectorSize / 2);
			if (type.equals("begin")){
				g.drawArc (connectorSize / 2, currentLine + verticalLine - connectorSize,
				   connectorSize, connectorSize, 180, 90);
			}
			else{
				g.drawArc (-connectorSize / 2, currentLine + verticalLine - connectorSize,
					connectorSize, connectorSize, 270, 90);
			}
			currentLine += verticalSpacing + height[i-1] - connectorPlace[i-1] +
							connectorPlace[i];
			i++;
		}

	}

	//preferredSize method overridden to be size calculated
	// in the constructor
	public Dimension preferredSize () {
		return new Dimension (connectorWidth, connectorHeight );
    }

}
