package sim.lib.wires;

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

public class Fat2AnyThinSplitter extends RotatableFlippableWrapperPainted
		implements SplitterModule {

	private static Image ICON = GuiFileLink.getImage("sim/lib/wires/HalfSplitterIcon.gif");
	private Junction[] nodes;
	private Junction bus = null;
	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.bus.getNodes().getItemAt(k);
			Node localNode2 = this.nodes[(k - this.start)].getNodes().getItemAt(0);
			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;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(j, i - 1, i, 3);
		int k = j;
		int m = i / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, 3 * m));
		paramGraphics.drawString(Integer.toString(this.end), m, j - m);
		if (this.start != this.end) {
			paramGraphics.drawString(Integer.toString(this.start), m, getSize().height - m);
		}
		for (m = 0; m < this.nodes.length; m++) {
			paramGraphics.drawLine(0, k, i, k);
			paramGraphics.drawLine(i, k, j, i);
			k += i;
		}
	}

	public void selected() {
		int i = this.end - this.start + 1;
		for (int j = 0; j < i; j++) {
			this.nodes[j].removePin();
		}
		this.bus.removePin();
		changeColor(Color.green);
	}

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

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

	public boolean hasProperties() {
		return true;
	}

	public String getBubbleHelp() {
		return "Partial Wire Splitter";
	}

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

	protected boolean canDropFlipped_0() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 1, this.busSize);
		for (int i = 0; (i < this.nodes.length) && (bool); i++) {
			bool = Wrapper.canDropJuncion(this.gridLocation.x + 3, this.gridLocation.y + 2 + i, 1);
		}
		return bool;
	}

	protected void dropedFlipped_0() {
		this.bus = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 1, this.busSize);
		for (int i = 0; i < this.nodes.length; i++) {
			this.nodes[(this.nodes.length - 1 - i)] = Wrapper.setPinAt(this.gridLocation.x + 3, this.gridLocation.y + 2 + i, 1);
		}
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void paintFlipped_270(Graphics paramGraphics) {
		int i = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
		int j = 2 * i;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(i - 1, j, 3, i);
		int k = j;
		int m = 3 * i / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, m));
		paramGraphics.drawString(Integer.toString(this.end), j - m, m);
		if (this.start != this.end) {
			paramGraphics.drawString(Integer.toString(this.start), getSize().width - m, m);
		}
		for (m = 0; m < this.nodes.length; m++) {
			paramGraphics.drawLine(k, 0, k, i);
			paramGraphics.drawLine(k, i, i, j);
			k += i;
		}
	}

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

	protected void dropedNormal_270() {
		this.bus = Wrapper.setPinAt(this.gridLocation.x + 1, this.gridLocation.y, this.busSize);
		for (int i = 0; i < this.nodes.length; i++) {
			this.nodes[(this.nodes.length - 1 - i)] = Wrapper.setPinAt(this.gridLocation.x + 2 + i, this.gridLocation.y + 3, 1);
		}
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

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

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

	protected boolean canDropNormal_90() {
		int i = this.end - this.start + 1;
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x + i + 1, this.gridLocation.y + 3, this.busSize);
		for (int j = 0; (j < i) && (bool); j++) {
			bool = Wrapper.canDropJuncion(this.gridLocation.x + 1 + j, this.gridLocation.y, 1);
		}
		return bool;
	}

	protected void dropedNormal_90() {
		this.bus = Wrapper.setPinAt(this.gridLocation.x + this.nodes.length + 1, this.gridLocation.y + 3, this.busSize);
		for (int i = 0; i < this.nodes.length; i++) {
			this.nodes[i] = Wrapper.setPinAt(this.gridLocation.x + 1 + i, this.gridLocation.y, 1);
		}
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected boolean canDropFlipped_90() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x + this.nodes.length + 1, this.gridLocation.y, this.busSize);
		for (int i = 0; (i < this.nodes.length) && (bool); i++) {
			bool = Wrapper.canDropJuncion(this.gridLocation.x + 1 + i, this.gridLocation.y + 3, 1);
		}
		return bool;
	}

	protected void dropedFlipped_90() {
		this.bus = Wrapper.setPinAt(this.gridLocation.x + this.nodes.length + 1, this.gridLocation.y, this.busSize);
		for (int i = 0; i < this.nodes.length; i++) {
			this.nodes[i] = Wrapper.setPinAt(this.gridLocation.x + 1 + i, this.gridLocation.y + 3, 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);
		for (int i = 0; (i < this.nodes.length) && (bool); i++) {
			bool = Wrapper.canDropJuncion(this.gridLocation.x + 2 + i, this.gridLocation.y, 1);
		}
		return bool;
	}

	public Image getIcon() {
		return ICON;
	}

	protected void dropedFlipped_270() {
		this.bus = Wrapper.setPinAt(this.gridLocation.x + 1, this.gridLocation.y + 3, this.busSize);
		for (int i = 0; i < this.nodes.length; i++) {
			this.nodes[(this.nodes.length - 1 - i)] = Wrapper.setPinAt(this.gridLocation.x + 2 + i, this.gridLocation.y, 1);
		}
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	public int getNumberOfSpecificParameters() {
		return 3;
	}

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

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

	public Wrapper createWrapper(Point paramPoint) {
		Fat2AnyThinSplitter localFat2AnyThinSplitter = getCopy();
		localFat2AnyThinSplitter.setGridLocation(paramPoint);
		return localFat2AnyThinSplitter;
	}

	protected void paintNormal_270(Graphics paramGraphics) {
		int i = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
		int j = 2 * i;
		int k = 3 * i;
		paramGraphics.setColor(this.brush);
		paramGraphics.fillRect(i - 1, 0, 3, i);
		int m = j;
		int n = k / 4;
		paramGraphics.setFont(new Font(Wrapper.FONT_NAME, 0, n));
		paramGraphics.drawString(Integer.toString(this.end), j - n, j + n);
		if (this.start != this.end) {
			paramGraphics.drawString(Integer.toString(this.start), getSize().width - n, j + n);
		}
		for (n = 0; n < this.nodes.length; n++) {
			paramGraphics.drawLine(m, j, m, k);
			paramGraphics.drawLine(m, j, i, i);
			m += i;
		}
	}

	protected void adjustToChanges() {
		if (this.angle % 180 == 0) {
			setGridSize(3, this.end - this.start + 3);
			return;
		}
		setGridSize(this.end - this.start + 3, 3);
	}

	private void setParameters(int paramInt1, int paramInt2, int paramInt3) {
		this.busSize = paramInt1;
		this.start = paramInt2;
		this.end = paramInt3;
		this.nodes = new Junction[paramInt3 - paramInt2 + 1];
		adjustToChanges();
	}

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

	public void checkAfterSelected() {
		int i = this.end - this.start + 1;
		for (int j = 0; j < i; j++) {
			Wrapper.checkPin(this.nodes[j]);
		}
		Wrapper.checkPin(this.bus);
	}

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

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

	protected boolean canDropFlipped_180() {
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x + 3, this.gridLocation.y + this.nodes.length + 1, this.busSize);
		for (int i = 0; (i < this.nodes.length) && (bool); i++) {
			bool = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 1 + i, 1);
		}
		return bool;
	}

	protected void dropedNormal_180() {
		this.bus = Wrapper.setPinAt(this.gridLocation.x + 3, this.gridLocation.y + 1, this.busSize);
		for (int i = 0; i < this.nodes.length; i++) {
			this.nodes[(this.nodes.length - 1 - i)] = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 2 + i, 1);
		}
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void dropedFlipped_180() {
		this.bus = Wrapper.setPinAt(this.gridLocation.x + 3, this.gridLocation.y + this.nodes.length + 1, this.busSize);
		for (int i = 0; i < this.nodes.length; i++) {
			this.nodes[i] = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 1 + i, 1);
		}
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

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

	protected void dropedNormal_0() {
		this.bus = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + this.nodes.length + 1, this.busSize);
		for (int i = 0; i < this.nodes.length; i++) {
			this.nodes[i] = Wrapper.setPinAt(this.gridLocation.x + 3, this.gridLocation.y + 1 + i, 1);
		}
		changeColor(Color.black);
		this.oldBusSize = 0;
	}

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

	protected boolean canDropNormal_0() {
		int i = this.end - this.start + 1;
		boolean bool = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + i + 1, this.busSize);
		for (int j = 0; (j < i) && (bool); j++) {
			bool = Wrapper.canDropJuncion(this.gridLocation.x + 3, this.gridLocation.y + 1 + j, 1);
		}
		return bool;
	}
}
