package sim.lib.others;

import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Scanner;

import sim.*;
import sim.engine.*;

import sim.lib.wires.Junction;
import sim.lib.wires.JunctionList;

public class Decoder extends RotatableFlippableWrapperPainted implements EngineModule {
	/* ==================================================================
	 Creation Part
	 ================================================================= */

	public Image getIcon() {
		return GuiFileLink.getImage("sim/lib/others/Decoder.gif");
	}

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

	public Wrapper createWrapper(Point gridPosition) {
		Decoder result = this.getCopy();
		result.setGridLocation(gridPosition);
		return result;
	}

	public String getBubbleHelp() {
		return "Decoder";
	}

	/* ==================================================================
	 GUI part
	 ================================================================= */
	public Decoder() {
		super();

		this.setBusSize(3);
	}

	private Decoder getCopy() {
		Decoder result = new Decoder();

		result.setBusSize(this.inputBusSize);
		result.changeDelay(this.delay);

		return result;
	}

	public void initializeGridSize() {
		this.setGridSize(4, 6);
	}
	/* ==================================================================
	 Maintanance Part
	 ================================================================= */
	private Junction input = new Junction(1);
	private JunctionList output = new JunctionList();
	private int numOfOutputs;
	private int inputBusSize;
	private static int OUTPUT_BUS_SIZE = 1;

	public void setBusSize(int size) {
		this.inputBusSize = size;
		this.numOfOutputs = (int) Math.pow(2, this.inputBusSize);
		this.adjustToChanges();
	}

	public void selected() {
		this.input.removePin();
		for (int loop = 0; loop < this.numOfOutputs; loop++) {
			this.output.getItemAt(loop).removePin();
		}

		this.changeColor(Color.green);
	}

	public void checkAfterSelected() {
		Wrapper.checkPin(this.input);
		for (int loop = 0; loop < this.numOfOutputs; loop++) {
			Wrapper.checkPin(this.output.getItemAt(loop));
		}
	}
	/* ==================================================================
	 Simulation part
	 ================================================================= */
	protected double delay = 1;

	public void evaluateOutput(double currentTime, Data[] currentInputs, EnginePeer peer) {
		boolean foundUndefined = false;
		double time = currentTime + this.delay;
		int position = 0;

		int loop;

		for (loop = inputBusSize - 1; (loop >= 0) && (!foundUndefined); loop--) {
			if (currentInputs[loop].isUndefined()) {
				foundUndefined = true;
			} else {
				position = 2 * position;
				if (currentInputs[loop].getValue()) {
					position++;
				}
			}
		}

		//System.out.println(position);

		if (foundUndefined) {
			for (loop = 0; loop < this.numOfOutputs; loop++) {
				peer.setOutputPinUndefined(loop, time);
			}
		} else {
			for (loop = 0; loop < this.numOfOutputs; loop++) {
				peer.setOutputPinValue(loop, false, time);
			}
			peer.setOutputPinValue(position, true, time);
		}
	}

	public void createEnginePeer(EnginePeerList epl) {
		EnginePeer ep = new EnginePeer(this.inputBusSize, this.numOfOutputs * this.OUTPUT_BUS_SIZE, this);

		int loop, index, position;

		position = 0;

		for (index = 0; index < this.inputBusSize; index++) {
			ep.setInputPin(position, this.input.getNodes().getItemAt(index));
			position++;
		}

		position = 0;

		for (index = 0; index < this.numOfOutputs; index++) {
			for (loop = 0; loop < this.OUTPUT_BUS_SIZE; loop++) {
				ep.setOutputPin(position, this.output.getItemAt(index).getNodes().getItemAt(loop));
				position++;
			}
		}

		epl.insertItem(ep);
	}

	public void reset() {
	}

	public Wrapper getParentWrapper() {
		return this;
	}

	public double getDelay() {
		return this.delay;
	}

	public void changeDelay(double newValue) {
		this.delay = newValue;
	}

	/* ==================================================================
	 Storage Part
	 ================================================================= */
	public String getSpecificParameters() {
		return (Integer.toString(this.inputBusSize) + Wrapper.SEPARATOR + Double.toString(this.delay) + Wrapper.SEPARATOR);
	}

	public void loadWrapper(String[] specificParameters) throws SimException {
		if (specificParameters.length == this.getNumberOfSpecificParameters()) {
			try {
				this.setBusSize(Integer.valueOf(specificParameters[0]).intValue());
				this.delay = Double.valueOf(specificParameters[1]).doubleValue();
			} catch (NumberFormatException e) {
				throw (new SimException("incorrect parameter type"));
			}
		} else {
			throw (new SimException("incorrect number of parameters"));
		}
	}

	public int getNumberOfSpecificParameters() {
		return 2;
	}
	/* ==================================================================
	 Rotation abd Flipping Part
	 ================================================================= */

	protected void paintNormal_0(Graphics g) {
		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;
			int x, y, width, height;
			int cableLen = gridGap;
			int middle = (this.numOfOutputs / 2) * gridGap;

			g.setColor(this.brush);
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			FontMetrics fm = this.getFontMetrics(this.getFont());

			//body
			x = gridGap;
			y = 2 * increment;
			height = this.numOfOutputs * gridGap;
			width = 2 * gridGap;

			g.drawRect(x, y, width, height);
			g.drawLine(x, y + gridGap, x + width , y);
			g.drawLine(x, y + height - gridGap, x + width, y + height );

			// input bus
			int offset = 0;
			int inThickness = 1;
			if (this.inputBusSize != 1) {
				offset = -1;
				inThickness = 3;
			}
			x = 0;
			y = middle + offset;
			g.fillRect(x, y, cableLen, inThickness);

			//label
			x = gridGap + increment;
			y = middle + increment;
			g.drawString("D", x, y);

			//output bus
			x = 3 * gridGap;
			y = gridGap;
			for (int index = 0; index < this.numOfOutputs; index++) {
				g.drawString(Integer.toString(index), x - fm.stringWidth(Integer.toString(index)), y + increment);
				g.drawLine(x, y, x + cableLen, y);
				y += gridGap;
			}
		}
	}

	protected void paintNormal_90(Graphics g) {
		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;
			int x, y, width, height;
			int cableLen = gridGap;
			int middle = (this.numOfOutputs / 2) * gridGap;

			g.setColor(this.brush);
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			FontMetrics fm = this.getFontMetrics(this.getFont());

			//body
			x = 2 * increment;
			y = gridGap;
			width = this.numOfOutputs * gridGap;
			height = 2 * gridGap;
			g.drawLine(x, y, x + gridGap, y + height);
			g.drawLine(x + width, y, x + width - gridGap, y + height);

			g.drawRect(x, y, width, height);

			// input bus
			int offset = 0;
			int inThickness = 1;
			if (this.inputBusSize != 1) {
				offset = -1;
				inThickness = 3;
			}
			x = middle + offset;
			y = 3 * gridGap;
			g.fillRect(x, y, inThickness, cableLen);

			//label
			x = middle - increment;
			y = 3 * gridGap - increment;
			g.drawString("D", x, y);

			//output bus
			x = gridGap;
			y = 0;
			for (int index = 0; index < this.numOfOutputs; index++) {
				g.drawString(Integer.toString(index), x - increment, y + 2 * gridGap);
				g.drawLine(x, y, x, y + cableLen);
				x += gridGap;
			}
		}
	}

	protected void paintNormal_180(Graphics g) {


		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;

			g.setColor(this.brush);


			int height = this.numOfOutputs * gridGap;
			int width = 2 * gridGap;
			//top-left corner
			int xOffset = gridGap;
			int yOffset = 2 * increment;
			int middle = height / 2;
			int rightEdge = xOffset + width;
			//body
			g.drawRect(xOffset, yOffset, width, height);
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			g.drawLine(xOffset, yOffset, xOffset + width , yOffset + gridGap);
			g.drawLine(xOffset, yOffset + height, xOffset + width, yOffset + height - gridGap);

			// input bus
			int offset = 0;
			int inThickness = 1;
			int cableLen = gridGap;
			FontMetrics fm = this.getFontMetrics(this.getFont());

			if (this.inputBusSize != 1) {
				offset = -1;
				inThickness = 3;
			}

			g.fillRect(rightEdge, middle + offset, cableLen, inThickness);

			//label
			g.drawString("D", rightEdge - increment - fm.stringWidth("D"), middle + yOffset);


			//output bus
			int bottom = this.numOfOutputs * gridGap;
			int outThickness = 1;
			int textOffset = xOffset + increment;
			for (int index = 0; index < this.numOfOutputs; index++) {
				g.drawString(Integer.toString(index), textOffset, bottom + yOffset);
				g.fillRect(0, bottom, cableLen, outThickness);
				bottom -= gridGap;
			}
		}
	}

	protected void paintNormal_270(Graphics g) {
		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;

			g.setColor(this.brush);


			int width = this.numOfOutputs * gridGap;
			int height = 2 * gridGap;
			//top-left corner
			int yOffset = gridGap;
			int xOffset = 2 * increment;
			int middle = width / 2;

			//body
			g.drawRect(xOffset, yOffset, width, height);
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			g.drawLine(xOffset + gridGap , yOffset, xOffset, yOffset + height);
			g.drawLine(xOffset + width - gridGap, yOffset, xOffset + width, yOffset + height);

			// input bus
			int offset = 0;
			int inThickness = 1;
			int cableLen = gridGap;

			if (this.inputBusSize != 1) {
				offset = -1;
				inThickness = 3;
			}

			g.fillRect(middle + offset, 0, inThickness, cableLen);

			//label
			g.drawString("D", xOffset + middle, yOffset + height / 2 - increment);


			//output bus
			int right = this.numOfOutputs * gridGap;
			int outThickness = 1;
			int bottomEdge = yOffset + height;
			int textOffset = bottomEdge - increment;
			for (int index = 0; index < this.numOfOutputs; index++) {
				g.drawString(Integer.toString(index), right, textOffset);
				g.fillRect(right, bottomEdge, outThickness, cableLen);
				right -= gridGap;
			}
		}
	}

	protected void paintFlipped_0(Graphics g) {


		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;

			g.setColor(this.brush);


			int height = this.numOfOutputs * gridGap;
			int width = 2 * gridGap;
			//top-left corner
			int xOffset = gridGap;
			int yOffset = 2 * increment;
			int middle = height / 2;

			//body
			g.drawRect(xOffset, yOffset, width, height);
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			g.drawLine(xOffset, yOffset + gridGap, xOffset + width , yOffset);
			g.drawLine(xOffset, yOffset + height - gridGap, xOffset + width, yOffset + height);

			// input bus
			int offset = 0;
			int inThickness = 1;
			int cableLen = gridGap;
			FontMetrics fm = this.getFontMetrics(this.getFont());

			if (this.inputBusSize != 1) {
				offset = -1;
				inThickness = 3;
			}

			g.fillRect(0, middle + offset, cableLen, inThickness);

			//label
			g.drawString("D", xOffset + increment, middle + yOffset);


			//output bus
			int bottom = this.numOfOutputs * gridGap;
			int outThickness = 1;
			int rightEdge = xOffset + width;
			int textOffset = rightEdge - increment;
			for (int index = 0; index < this.numOfOutputs; index++) {
				g.drawString(Integer.toString(index), textOffset - fm.stringWidth(Integer.toString(index)), bottom + yOffset);
				g.fillRect(rightEdge, bottom, cableLen, outThickness);
				bottom -= gridGap;
			}
		}
	}

	protected void paintFlipped_90(Graphics g) {
		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;

			g.setColor(this.brush);


			int width = this.numOfOutputs * gridGap;
			int height = 2 * gridGap;
			//top-left corner
			int yOffset = gridGap;
			int xOffset = 2 * increment;
			int middle = width / 2;

			//body
			g.drawRect(xOffset, yOffset, width, height);
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			g.drawLine(xOffset + gridGap, yOffset, xOffset, yOffset + height);
			g.drawLine(xOffset + width - gridGap, yOffset, xOffset + width, yOffset + height);

			// input bus
			int offset = 0;
			int inThickness = 1;
			int cableLen = gridGap;

			if (this.inputBusSize != 1) {
				offset = -1;
				inThickness = 3;
			}

			g.fillRect(middle + offset, 0, inThickness, cableLen);

			//label
			g.drawString("D", xOffset + middle, yOffset + height / 2 - increment);


			//output bus
			int left = gridGap;
			int outThickness = 1;
			int bottomEdge = yOffset + height;
			int textOffset = bottomEdge - increment;
			for (int index = 0; index < this.numOfOutputs; index++) {
				g.drawString(Integer.toString(index), left, textOffset);
				g.fillRect(left, bottomEdge, outThickness, cableLen);
				left += gridGap;
			}
		}
	}

	protected void paintFlipped_180(Graphics g) {


		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;

			g.setColor(this.brush);


			int height = this.numOfOutputs * gridGap;
			int width = 2 * gridGap;
			//top-left corner
			int xOffset = gridGap;
			int yOffset = 2 * increment;
			int middle = height / 2;
			int rightEdge = xOffset + width;
			//body
			g.drawRect(xOffset, yOffset, width, height);
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			g.drawLine(xOffset, yOffset, xOffset + width , yOffset + gridGap);
			g.drawLine(xOffset, yOffset + height, xOffset + width, yOffset + height - gridGap);

			// input bus
			int offset = 0;
			int inThickness = 1;
			int cableLen = gridGap;
			FontMetrics fm = this.getFontMetrics(this.getFont());

			if (this.inputBusSize != 1) {
				offset = -1;
				inThickness = 3;
			}

			g.fillRect(rightEdge, middle + offset, cableLen, inThickness);

			//label
			g.drawString("D", rightEdge - increment - fm.stringWidth("D"), middle + yOffset);


			//output bus
			int up = gridGap;
			int outThickness = 1;
			int textOffset = xOffset + increment;
			for (int index = 0; index < this.numOfOutputs; index++) {
				g.drawString(Integer.toString(index), textOffset, up + yOffset);
				g.fillRect(0, up, cableLen, outThickness);
				up += gridGap;
			}
		}
	}

	protected void paintFlipped_270(Graphics g) {
		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;

			g.setColor(this.brush);


			int width = this.numOfOutputs * gridGap;
			int height = 2 * gridGap;
			//top-left corner
			int yOffset = gridGap;
			int xOffset = 2 * increment;
			int middle = width / 2;

			//body
			g.drawRect(xOffset, yOffset, width, height);
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			g.drawLine(xOffset, yOffset, xOffset + gridGap, yOffset + height);
			g.drawLine(xOffset + width, yOffset, xOffset + width - gridGap, yOffset + height);


			// input bus
			int offset = 0;
			int inThickness = 1;
			int cableLen = gridGap;

			if (this.inputBusSize != 1) {
				offset = -1;
				inThickness = 3;
			}

			g.fillRect(middle + offset, yOffset + height, inThickness, cableLen);

			//label
			g.drawString("D", xOffset + middle, height + yOffset - increment);


			//output bus
			int right = this.numOfOutputs * gridGap;
			int outThickness = 1;
			int topEdge = yOffset;
			int textOffset = 2 * topEdge - increment;
			for (int index = 0; index < this.numOfOutputs; index++) {
				g.drawString(Integer.toString(index), right, textOffset);
				g.fillRect(right, 0, outThickness, cableLen);
				right -= gridGap;
			}
		}
	}

	protected boolean canDropNormal_0() {

		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.x + this.gridSize.width;
		int loop;

		// check outputs
		for (loop = 0; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(end, this.gridLocation.y + 1 + loop, this.OUTPUT_BUS_SIZE);
		}

		// check inputs

		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + middle, this.inputBusSize);
		}

		return result;
	}

	protected boolean canDropNormal_90() {

		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.y + this.gridSize.height;
		int loop;

		// check outputs
		for (loop = 0; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 1 + loop, this.gridLocation.y, this.OUTPUT_BUS_SIZE);
		}

		// check inputs

		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + middle, end, this.inputBusSize);
		}

		return result;
	}

	protected boolean canDropNormal_180() {

		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.x + this.gridSize.width;
		int loop;

		// check outputs
		for (loop = 0; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + this.numOfOutputs - loop, this.OUTPUT_BUS_SIZE);
		}

		// check inputs

		if (result) {
			result = Wrapper.canDropJuncion(end, this.gridLocation.y + middle, this.inputBusSize);
		}

		return result;
	}

	protected boolean canDropNormal_270() {

		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.y + this.gridSize.height;
		int loop;

		// check outputs
		for (loop = 0; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.numOfOutputs - loop, end, this.OUTPUT_BUS_SIZE);
		}

		// check inputs

		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + middle, this.gridLocation.y, this.inputBusSize);
		}

		return result;
	}

	protected boolean canDropFlipped_0() {

		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.x + this.gridSize.width;
		int loop;

		// check outputs
		for (loop = 0; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(end, this.gridLocation.y + this.numOfOutputs - loop, this.OUTPUT_BUS_SIZE);
		}

		// check inputs

		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + middle, this.inputBusSize);
		}

		return result;
	}

	protected boolean canDropFlipped_90() {

		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.y + this.gridSize.height;
		int loop;

		// check outputs
		for (loop = 0; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 1 + loop, end, this.OUTPUT_BUS_SIZE);
		}

		// check inputs

		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + middle, this.gridLocation.y, this.inputBusSize);
		}

		return result;
	}

	protected boolean canDropFlipped_180() {

		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.x + this.gridSize.width;
		int loop;

		// check outputs
		for (loop = 0; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 1 + loop, this.OUTPUT_BUS_SIZE);
		}

		// check inputs

		if (result) {
			result = Wrapper.canDropJuncion(end, this.gridLocation.y + middle, this.inputBusSize);
		}

		return result;
	}

	protected boolean canDropFlipped_270() {

		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.y + this.gridSize.height;
		int loop;

		// check outputs
		for (loop = 0; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.numOfOutputs - loop, this.gridLocation.y, this.OUTPUT_BUS_SIZE);
		}

		// check inputs

		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + middle, end, this.inputBusSize);
		}

		return result;
	}

	protected void dropedNormal_0() {

		int loop;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.x + this.gridSize.width;

		//output
		this.output.setSize(this.numOfOutputs);

		for (loop = 0; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(end, this.gridLocation.y + 1 + loop, this.OUTPUT_BUS_SIZE));
		}

		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + middle, this.inputBusSize);

		this.changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void dropedNormal_90() {

		int loop;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.y + this.gridSize.height;

		//output
		this.output.setSize(this.numOfOutputs);

		for (loop = 0; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + 1 + loop, this.gridLocation.y, this.OUTPUT_BUS_SIZE));
		}

		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x + middle, end, this.inputBusSize);

		this.changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void dropedNormal_180() {

		int loop;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.x + this.gridSize.width;

		//output
		this.output.setSize(this.numOfOutputs);

		for (loop = 0; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + this.numOfOutputs - loop, this.OUTPUT_BUS_SIZE));
		}

		//input
		this.input = Wrapper.setPinAt(end, this.gridLocation.y + middle, this.inputBusSize);

		this.changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void dropedNormal_270() {

		int loop;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.y + this.gridSize.height;

		//output
		this.output.setSize(this.numOfOutputs);

		for (loop = 0; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + this.numOfOutputs - loop, end, this.OUTPUT_BUS_SIZE));
		}

		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x + middle, this.gridLocation.y, this.inputBusSize);

		this.changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void dropedFlipped_0() {

		int loop;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.x + this.gridSize.width;

		//output
		this.output.setSize(this.numOfOutputs);

		for (loop = 0; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(end, this.gridLocation.y + this.numOfOutputs - loop, this.OUTPUT_BUS_SIZE));
		}

		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + middle, this.inputBusSize);

		this.changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void dropedFlipped_90() {

		int loop;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.y + this.gridSize.height;

		//output
		this.output.setSize(this.numOfOutputs);

		for (loop = 0; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + 1 + loop, end, this.OUTPUT_BUS_SIZE));
		}

		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x + middle, this.gridLocation.y, this.inputBusSize);

		this.changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void dropedFlipped_180() {

		int loop;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.x + this.gridSize.width;

		//output
		this.output.setSize(this.numOfOutputs);

		for (loop = 0; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 1 + loop, this.OUTPUT_BUS_SIZE));
		}

		//input
		this.input = Wrapper.setPinAt(end, this.gridLocation.y + middle, this.inputBusSize);

		this.changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void dropedFlipped_270() {

		int loop;
		int middle = this.numOfOutputs / 2;
		int end = this.gridLocation.y + this.gridSize.height;

		//output
		this.output.setSize(this.numOfOutputs);

		for (loop = 0; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + this.numOfOutputs - loop, this.gridLocation.y, this.OUTPUT_BUS_SIZE));
		}

		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x + middle, end, this.inputBusSize);

		this.changeColor(Color.black);
		this.oldBusSize = 0;
	}

	protected void adjustToChanges() {
		if (this.angle % 180 == 0) {
			this.setGridSize(4, this.numOfOutputs + 1);
		} else {
			this.setGridSize(this.numOfOutputs + 1, 4);
		}
	}
	/* ==================================================================
	 Popup Part
	 ================================================================= */
	private int oldBusSize = 0;

	public boolean hasProperties() {
		return true;
	}

	public Component getPropertyWindow() {
		return (new EncoderProperties(this.inputBusSize, this.delay));
	}

	public void respondToChanges(Component property) {
		EncoderProperties dp = (EncoderProperties) property;

		this.delay = dp.getDelay();

		if (this.oldBusSize == 0) {
			this.oldBusSize = this.inputBusSize;
		}

		CentralPanel.ACTIVE_GRID.eraseComponent(this);
		this.setBusSize(dp.getBusSize());
		CentralPanel.ACTIVE_GRID.paintComponent(this);

	}

	public void restoreOriginalProperties() {
		if (this.oldBusSize != 0) {
			this.setBusSize(this.oldBusSize);
			this.oldBusSize = 0;
		}
	}
	
	@Override
	public void writeVHDLEntity(String path) {
		String fileName = "gates.vhd";
		File file = new File(path+"\\"+fileName);
		boolean written = false;
		try {
			Scanner scanner = new Scanner(file);
			while(scanner.hasNextLine()){
				String line = scanner.nextLine();
				if(line.contains(this.getVHDLName())){
					written = true;
				}
			}
			scanner.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		
		if(!written){
			Writer writer = null;
			try {
				writer = new FileWriter(path+"\\"+fileName,true);
				writer.write("\nlibrary IEEE;\n");
				writer.write("use IEEE.STD_LOGIC_1164.ALL;\n");
				writer.write("use ieee.numeric_std.all;\n");
				
				writer.write("\nentity " + this.getVHDLName() + " is\n");
				writer.write("port(\n");
				for(int i = 0; i < this.inputBusSize; i++){
					if(i != 0)
						writer.write(",");
					writer.write("x" + i);
				}
				writer.write(" : in std_logic;\n");
				for(int i = 0; i < this.numOfOutputs; i++){
					if(i != 0)
						writer.write(",");
					writer.write("f" + i);
				}
				writer.write(" : out std_logic);\n");
				writer.write("end "+ this.getVHDLName() + ";\n\n");
				
				writer.write("architecture behav of "+ this.getVHDLName() + " is\n");
				writer.write("signal inbus : std_logic_vector("+(this.inputBusSize-1)+" downto 0);\n");
				writer.write("begin\n");
				for(int j = 0; j < this.inputBusSize; j++){
					writer.write("inbus("+j+") <= x"+j+";\n");
				}
				for(int j = 0; j < Math.pow(2,this.inputBusSize); j++){
					writer.write("f"+j+" <= '1' when to_integer(unsigned(inbus))="+j+" else '0';\n");
				}
				writer.write("end behav;\n");
				writer.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

	@Override
	public String getVHDLName() {
		// TODO Auto-generated method stub
		return "DECODER_"+this.inputBusSize;
	}
	
}
