package sim.lib.wires;

import java.awt.*;
import sim.*;
import sim.engine.*;

public class Fat2FatSplitter extends RotatableFlippableWrapperPainted
		implements SplitterModule {

	private static Image ICON = GuiFileLink.getImage("sim/lib/wires/Fat2FatSplitterIcon.gif");
	private Junction outBus;
	private Junction inBus;
	private int busSize;
	private int start;
	private int end;
	private int oldBusSize = 0;
	private int oldStart;
	private int oldEnd;

	public void loadWrapper(String[] paramArrayOfString)
			throws SimException {
		if (paramArrayOfString.length == getNumberOfSpecificParameters()) {
			try {
				setParameters(Integer.valueOf(paramArrayOfString[0]).intValue(), Integer.valueOf(paramArrayOfString[1]).intValue(), Integer.valueOf(paramArrayOfString[2]).intValue());
				return;
			} catch (NumberFormatException localNumberFormatException) {
				throw new SimException("incorrect parameter type");
			}
		}
		throw new SimException("incorrect number of parameters");
	}

	public void restoreOriginalProperties() {
		if (this.oldBusSize != 0) {
			setParameters(this.oldBusSize, this.oldStart, this.oldEnd);
			this.oldBusSize = 0;
		}
	}

	public void mergeNodes(NodeList paramNodeList) {
		for (int k = this.start; k < this.end + 1; k++) {
			Node localNode1 = this.inBus.getNodes().getItemAt(k);
			Node localNode2 = this.outBus.getNodes().getItemAt(k - this.start);
			if (localNode1 != localNode2) {
				paramNodeList.removeItem(localNode2);
				WireList localWireList = localNode2.getWires();
				for (int i = 0; i < localWireList.getSize(); i++) {
					localNode1.addWire(localWireList.getItemAt(i));
				}
				JunctionList localJunctionList = localNode2.getJunctiions();
				for (int i = 0; i < localJunctionList.getSize(); i++) {
					localNode1.addJunction(localJunctionList.getItemAt(i));
					NodeList localNodeList = localJunctionList.getItemAt(i).getNodes();
					int j = localNodeList.indexOf(localNode2);
					if (j != -1) {
						localNodeList.changeItem(j, localNode1);
					}
				}
			}
		}
	}

	public void respondToChanges(Component paramComponent) {
		if (this.oldBusSize == 0) {
			this.oldBusSize = this.busSize;
			this.oldStart = this.start;
			this.oldEnd = this.end;
		}
		SplitterProperties localSplitterProperties = (SplitterProperties) paramComponent;
		CentralPanel.ACTIVE_GRID.eraseComponent(this);
		setParameters(localSplitterProperties.getBusSize(), localSplitterProperties.getFrom(), localSplitterProperties.getTo());
		CentralPanel.ACTIVE_GRID.paintComponent(this);
	}

	protected void paintNormal_180(Graphics paramGraphics) {
		int i = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
		int j = 2 * i;
		int k = 3 * i / 2;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(0, j - 1, j, 3);
		paramGraphics.fillRect(k, i - 1, k, 3);
		paramGraphics.drawLine(k, i, i, j);
		paramGraphics.drawLine(k, i, k, j);
		paramGraphics.drawLine(k, i, j, j);
		k = i / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, 3 * k));
		paramGraphics.drawString(Integer.toString(this.start) + "-" + Integer.toString(this.end), k, 3 * i);
	}

	public void selected() {
		this.inBus.removePin();
		this.outBus.removePin();
		changeColor(Color.green);
	}

	protected void paintNormal_0(Graphics paramGraphics) {
		int i = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
		int j = 2 * i;
		int k = 3 * i / 2;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(0, j - 1, k, 3);
		paramGraphics.fillRect(i, i - 1, j, 3);
		paramGraphics.drawLine(k, j, i, i);
		paramGraphics.drawLine(k, j, k, i);
		paramGraphics.drawLine(k, j, j, i);
		k = i / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, 3 * k));
		paramGraphics.drawString(Integer.toString(this.start) + "-" + Integer.toString(this.end), i, i - k);
	}

	public void initializeGridSize() {
		setGridSize(3, 3);
	}

	public boolean hasProperties() {
		return true;
	}

	public String getBubbleHelp() {
		return "Bus-to-Bus Splitter";
	}

	protected void paintFlipped_0(Graphics paramGraphics) {
		int i = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
		int j = 2 * i;
		int k = 3 * i / 2;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(i, j - 1, j, 3);
		paramGraphics.fillRect(0, i - 1, k, 3);
		paramGraphics.drawLine(k, i, i, j);
		paramGraphics.drawLine(k, i, k, j);
		paramGraphics.drawLine(k, i, j, j);
		k = i / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, 3 * k));
		paramGraphics.drawString(Integer.toString(this.start) + "-" + Integer.toString(this.end), i, 3 * i - k);
	}

	protected boolean canDropFlipped_0() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 1, this.busSize);
		bool = (bool) && (Wrapper.canDropJuncion(this.gridLocation.x + 3, this.gridLocation.y + 2, this.end - this.start + 1));
		return bool;
	}

	protected void dropedFlipped_0() {
		this.inBus = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 1, this.busSize);
		this.outBus = Wrapper.setPinAt(this.gridLocation.x + 3, this.gridLocation.y + 2, this.end - this.start + 1);
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void paintFlipped_270(Graphics paramGraphics) {
		int i = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
		int j = 2 * i;
		int k = 3 * i / 2;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(i - 1, k, 3, k);
		paramGraphics.fillRect(j - 1, 0, 3, j);
		paramGraphics.drawLine(i, k, j, i);
		paramGraphics.drawLine(i, k, j, k);
		paramGraphics.drawLine(i, k, j, j);
		k = i / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, 3 * k));
		String str = Integer.toString(this.start) + "-" + Integer.toString(this.end);
		FontMetrics localFontMetrics = paramGraphics.getFontMetrics();
		paramGraphics.drawString(str, j - k - localFontMetrics.stringWidth(str), i - k);
	}

	protected boolean canDropNormal_270() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x + 1, this.gridLocation.y, this.busSize);
		bool = (bool) && (Wrapper.canDropJuncion(this.gridLocation.x + 2, this.gridLocation.y + 3, this.end - this.start + 1));
		return bool;
	}

	protected void dropedNormal_270() {
		this.inBus = Wrapper.setPinAt(this.gridLocation.x + 1, this.gridLocation.y, this.busSize);
		this.outBus = Wrapper.setPinAt(this.gridLocation.x + 2, this.gridLocation.y + 3, this.end - this.start + 1);
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	public Fat2FatSplitter() {
		setParameters(8, 0, 3);
	}

	protected void paintFlipped_90(Graphics paramGraphics) {
		int i = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
		int j = 2 * i;
		int k = 3 * i / 2;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(i - 1, i, 3, j);
		paramGraphics.fillRect(j - 1, 0, 3, k);
		paramGraphics.drawLine(j, k, i, i);
		paramGraphics.drawLine(j, k, i, k);
		paramGraphics.drawLine(j, k, i, j);
		k = i / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, 3 * k));
		paramGraphics.drawString(Integer.toString(this.start) + "-" + Integer.toString(this.end), i + k, 3 * i - k);
	}

	protected boolean canDropNormal_90() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x + 2, this.gridLocation.y + 3, this.busSize);
		bool = (bool) && (Wrapper.canDropJuncion(this.gridLocation.x + 1, this.gridLocation.y, this.end - this.start + 1));
		return bool;
	}

	protected void dropedNormal_90() {
		this.inBus = Wrapper.setPinAt(this.gridLocation.x + 2, this.gridLocation.y + 3, this.busSize);
		this.outBus = Wrapper.setPinAt(this.gridLocation.x + 1, this.gridLocation.y, this.end - this.start + 1);
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected boolean canDropFlipped_90() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x + 2, this.gridLocation.y, this.busSize);
		bool = (bool) && (Wrapper.canDropJuncion(this.gridLocation.x + 1, this.gridLocation.y + 3, this.end - this.start + 1));
		return bool;
	}

	protected void dropedFlipped_90() {
		this.inBus = Wrapper.setPinAt(this.gridLocation.x + 2, this.gridLocation.y, this.busSize);
		this.outBus = Wrapper.setPinAt(this.gridLocation.x + 1, this.gridLocation.y + 3, this.end - this.start + 1);
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected boolean canDropFlipped_270() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x + 1, this.gridLocation.y + 3, this.busSize);
		bool = (bool) && (Wrapper.canDropJuncion(this.gridLocation.x + 2, this.gridLocation.y, this.end - this.start + 1));
		return bool;
	}

	public Image getIcon() {
		return ICON;
	}

	protected void dropedFlipped_270() {
		this.inBus = Wrapper.setPinAt(this.gridLocation.x + 1, this.gridLocation.y + 3, this.busSize);
		this.outBus = Wrapper.setPinAt(this.gridLocation.x + 2, this.gridLocation.y, this.end - this.start + 1);
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	public int getNumberOfSpecificParameters() {
		return 3;
	}

	private Fat2FatSplitter getCopy() {
		Fat2FatSplitter localFat2FatSplitter = new Fat2FatSplitter();
		localFat2FatSplitter.setParameters(this.busSize, this.start, this.end);
		return localFat2FatSplitter;
	}

	public Wrapper createWrapper() {
		return getCopy();
	}

	public Wrapper createWrapper(Point paramPoint) {
		Fat2FatSplitter localFat2FatSplitter = getCopy();
		localFat2FatSplitter.setGridLocation(paramPoint);
		return localFat2FatSplitter;
	}

	protected void paintNormal_270(Graphics paramGraphics) {
		int i = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
		int j = 2 * i;
		int k = 3 * i / 2;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(i - 1, 0, 3, k);
		paramGraphics.fillRect(j - 1, i, 3, j);
		paramGraphics.drawLine(i, k, j, i);
		paramGraphics.drawLine(i, k, j, k);
		paramGraphics.drawLine(i, k, j, j);
		k = i / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, 3 * k));
		String str = Integer.toString(this.start) + "-" + Integer.toString(this.end);
		FontMetrics localFontMetrics = paramGraphics.getFontMetrics();
		paramGraphics.drawString(str, j - k - localFontMetrics.stringWidth(str), 3 * i - k);
	}

	protected void adjustToChanges() {
		CentralPanel.ACTIVE_GRID.eraseComponent(this, false);
		CentralPanel.ACTIVE_GRID.paintComponent(this);
	}

	private void setParameters(int paramInt1, int paramInt2, int paramInt3) {
		this.busSize = paramInt1;
		this.start = paramInt2;
		this.end = paramInt3;
	}

	public String getSpecificParameters() {
		return Integer.toString(this.busSize) + Wrapper.SEPARATOR + this.start + Wrapper.SEPARATOR + this.end + Wrapper.SEPARATOR;
	}

	public void checkAfterSelected() {
		Wrapper.checkPin(this.inBus);
		Wrapper.checkPin(this.outBus);
	}

	protected void paintFlipped_180(Graphics paramGraphics) {
		int i = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
		int j = 2 * i;
		int k = 3 * i / 2;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(k, j - 1, k, 3);
		paramGraphics.fillRect(0, i - 1, j, 3);
		paramGraphics.drawLine(k, j, i, i);
		paramGraphics.drawLine(k, j, k, i);
		paramGraphics.drawLine(k, j, j, i);
		k = i / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, 3 * k));
		paramGraphics.drawString(Integer.toString(this.start) + "-" + Integer.toString(this.end), k, i - k);
	}

	protected boolean canDropNormal_180() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x + 3, this.gridLocation.y + 1, this.busSize);
		bool = (bool) && (Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 2, this.end - this.start + 1));
		return bool;
	}

	protected boolean canDropFlipped_180() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x + 3, this.gridLocation.y + 2, this.busSize);
		bool = (bool) && (Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 1, this.end - this.start + 1));
		return bool;
	}

	protected void dropedNormal_180() {
		this.inBus = Wrapper.setPinAt(this.gridLocation.x + 3, this.gridLocation.y + 1, this.busSize);
		this.outBus = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 2, this.end - this.start + 1);
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void dropedFlipped_180() {
		this.inBus = Wrapper.setPinAt(this.gridLocation.x + 3, this.gridLocation.y + 2, this.busSize);
		this.outBus = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 1, this.end - this.start + 1);
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	public Component getPropertyWindow() {
		return new SplitterProperties(this.busSize, this.start, this.end, false);
	}

	protected void dropedNormal_0() {
		this.inBus = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 2, this.busSize);
		this.outBus = Wrapper.setPinAt(this.gridLocation.x + 3, this.gridLocation.y + 1, this.end - this.start + 1);
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void paintNormal_90(Graphics paramGraphics) {
		int i = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
		int j = 2 * i;
		int k = 3 * i / 2;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(i - 1, 0, 3, j);
		paramGraphics.fillRect(j - 1, k, 3, k);
		paramGraphics.drawLine(j, k, i, i);
		paramGraphics.drawLine(j, k, i, k);
		paramGraphics.drawLine(j, k, i, j);
		k = i / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, 3 * k));
		paramGraphics.drawString(Integer.toString(this.start) + "-" + Integer.toString(this.end), i + k, i - k);
	}

	protected boolean canDropNormal_0() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 2, this.busSize);
		bool = (bool) && (Wrapper.canDropJuncion(this.gridLocation.x + 3, this.gridLocation.y + 1, this.end - this.start + 1));
		return bool;
	}
}
