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 Demultiplexer extends RotatableFlippableWrapperPainted implements EngineModule {
	/* ==================================================================
	 Creation Part
	 ================================================================= */

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

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

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

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

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

		this.setNumOfOutputs(2);
		this.setBusSize(1);
	}

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

		result.setNumOfOutputs(this.numOfOutputs);
		result.setBusSize(this.busSize);
		result.changeDelay(this.delay);

		return result;
	}

	public void initializeGridSize() {
		this.setGridSize(4, 6);
	}
	/* ==================================================================
	 Maintanance Part
	 ================================================================= */
	private JunctionList output = new JunctionList();
	private Junction select = null;
	private Junction input = null;
	private int numOfOutputs;
	private int selectBusSize;
	private int busSize;

	public void setNumOfOutputs(int numOfOutputs) {
		this.numOfOutputs = numOfOutputs;
		this.selectBusSize = (this.numOfOutputs == 0 ? 0 : 32 - Integer.numberOfLeadingZeros(this.numOfOutputs - 1));
		this.adjustToChanges();
	}

	public void setBusSize(int size) {
		this.busSize = size;
	}

	public void selected() {
		//select bus
		this.select.removePin();

		//input
		this.input.removePin();

		//output
		for (int loop = 0; loop < this.numOfOutputs; loop++) {
			this.output.getItemAt(loop).removePin();
		}


		this.changeColor(Color.green);
	}

	public void checkAfterSelected() {
		for (int loop = 0; loop < this.numOfOutputs; loop++) {
			Wrapper.checkPin(this.output.getItemAt(loop));
		}

		Wrapper.checkPin(this.input);
		Wrapper.checkPin(this.select);
	}

	/* ==================================================================
	 Simulation part
	 ================================================================= */
	protected double delay = 1;

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

		int loop;

		for (loop = 0; (loop < this.selectBusSize) && (!foundUndefined); loop++) {
			if (currentInputs[loop].isUndefined()) {
				foundUndefined = true;
			} else {
				if (currentInputs[loop].getValue()) {
					position = position + base;
				}

				base = 2 * base;
			}
		}

		if (foundUndefined || (position >= this.numOfOutputs)) {
			base = this.busSize * this.numOfOutputs;

			for (loop = 0; loop < base; loop++) {
				peer.setOutputPinUndefined(loop, time);
			}
		} else {
			position = position * this.busSize;

			for (loop = 0; loop < position; loop++) {
				peer.setOutputPinValue(loop, false, time);
			}

			base = this.selectBusSize;
			end = position + this.busSize;

			for (loop = position; loop < end; loop++) {
				if (currentInputs[base].isUndefined()) {
					peer.setOutputPinUndefined(loop, time);
				} else {
					peer.setOutputPinValue(loop, currentInputs[base].getValue(), time);
				}

				base++;
			}

			base = this.busSize * this.numOfOutputs;
			for (loop = end; loop < base; loop++) {
				peer.setOutputPinValue(loop, false, time);
			}
		}
	}

	public void createEnginePeer(EnginePeerList epl) {
		EnginePeer ep = new EnginePeer(this.busSize + this.selectBusSize, this.numOfOutputs * this.busSize, this);

		int loop, index, position;

		//select bus
		for (loop = 0; loop < this.selectBusSize; loop++) {
			ep.setInputPin(loop, this.select.getNodes().getItemAt(loop));
		}

		//input
		position = this.selectBusSize;

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

		//output bus
		position = 0;

		for (index = 0; index < this.numOfOutputs; index++) {
			for (loop = 0; loop < this.busSize; 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.numOfOutputs) + Wrapper.SEPARATOR + Integer.toString(this.busSize) + Wrapper.SEPARATOR + Double.toString(this.delay) + Wrapper.SEPARATOR);
	}

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

	public int getNumberOfSpecificParameters() {
		return 3;
	}

	/* ==================================================================
	 Rotation abd Flipping Part
	 ================================================================= */
	public void paintNormal_0(Graphics g) {
		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;
			int height, width;

			int x1, y1, x2, y2;
			int thickness;
			int xOffset, yOffset;
			int cableLen;
			int xCenter = gridGap * this.getGridSize().width / 2;
			int yCenter = gridGap * this.getGridSize().height / 2;

			//body
			g.setColor(this.brush);
//			x1 = gridGap;
//			y1 = 2 * gridGap;
//			x2 = x1;
//			y2 = height - 2 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//left vertical line
//
//			x1 = 3 * gridGap;
//			y1 = gridGap;
//			x2 = x1;
//			y2 = height - gridGap;
//			g.drawLine(x1, y1, x2, y2);	//right vertical line
//
//			x1 = gridGap;
//			y1 = 2 * gridGap;
//			x2 = 3 * gridGap;
//			y2 = gridGap;
//			g.drawLine(x1, y1, x2, y2);	//top slope line
//
//			x1 = gridGap;
//			y1 = height - 2 * gridGap;
//			x2 = 3 * gridGap;
//			y2 = height - gridGap;
//			g.drawLine(x1, y1, x2, y2);	//bottom slope line
			x1 = gridGap;
			y1 = gridGap;
			height = gridGap * (this.numOfOutputs + 2);
			width = 2 * gridGap;
			g.drawRect(x1, y1, width, height);

			//label
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			x1 = gridGap + increment;
			y1 = yCenter + increment;
			g.drawString("D", x1, y1);

			//select line
			if (this.selectBusSize == 1) {
				xOffset = 0;
				thickness = 1;
			} else {
				xOffset = -1;
				thickness = 3;
			}
			x1 = xCenter + xOffset;
			y1 = 0;
			cableLen = gridGap;
			g.fillRect(x1, y1, thickness, cableLen);

			//output bus
			if (this.busSize == 1) {
				yOffset = 0;
				thickness = 1;
			} else {
				yOffset = -1;
				thickness = 3;
			}
			x1 = 0;
			y1 = yCenter + yOffset;
			cableLen = gridGap;
			g.fillRect(x1, y1, cableLen, thickness);

			//input bus
			xOffset = increment;
			x1 = 3 * gridGap;
			y1 = gridGap + yOffset;
			cableLen = gridGap;
			x2 = xCenter + xOffset;
			y2 = gridGap + increment;
			for (int i = 0; i < numOfOutputs / 2; i++) {
				y1 += gridGap;
				y2 += gridGap;
				g.fillRect(x1, y1, cableLen, thickness);
				g.drawString(Integer.toString(i), x2, y2);
			}
			y1 += gridGap;
			y2 += gridGap;
			for (int i = numOfOutputs / 2; i < numOfOutputs; i++) {
				y1 += gridGap;
				y2 += gridGap;
				g.fillRect(x1, y1, cableLen, thickness);
				g.drawString(Integer.toString(i), x2, y2);
			}
		}
	}

	public void paintNormal_90(Graphics g) {
		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;
			int height, width;

			int x1, y1, x2, y2;
			int thickness;
			int xOffset, yOffset;
			int cableLen;
			int xCenter = gridGap * this.getGridSize().width / 2;
			int yCenter = gridGap * this.getGridSize().height / 2;

			//body
			g.setColor(this.brush);
//			x1 = gridGap;
//			y1 = gridGap;
//			x2 = width - gridGap;
//			y2 = y1;
//			g.drawLine(x1, y1, x2, y2);	//top horizontal line
//
//			x1 = 2 * gridGap;
//			y1 = 3 * gridGap;
//			x2 = width - 2 * gridGap;
//			y2 = y1;
//			g.drawLine(x1, y1, x2, y2);	//bottom horizontal line
//
//			x1 = gridGap;
//			y1 = gridGap;
//			x2 = 2 * gridGap;
//			y2 = 3 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//left slope line
//
//			x1 = width - gridGap;
//			y1 = gridGap;
//			x2 = width - 2 * gridGap;
//			y2 = 3 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//right slope line
			x1 = gridGap;
			y1 = gridGap;
			width = gridGap * (this.numOfOutputs + 2);
			height = 2 * gridGap;
			g.drawRect(x1, y1, width, height);

			//label
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			x1 = xCenter - increment;
			y1 = 3 * gridGap - increment;
			g.drawString("D", x1, y1);

			//select line
			if (this.selectBusSize == 1) {
				yOffset = 0;
				thickness = 1;
			} else {
				yOffset = -1;
				thickness = 3;
			}
			x1 = 0;
			y1 = yCenter + yOffset;
			cableLen = gridGap;
			g.fillRect(x1, y1, cableLen, thickness);

			//output bus
			if (this.busSize == 1) {
				xOffset = 0;
				thickness = 1;
			} else {
				xOffset = -1;
				thickness = 3;
			}
			x1 = xCenter + xOffset;
			y1 = 3 * gridGap;
			cableLen = gridGap;
			g.fillRect(x1, y1, thickness, cableLen);

			//input bus
			yOffset = increment;
			x1 = gridGap + xOffset;
			y1 = 0;
			cableLen = gridGap;
			x2 = x1;
			y2 = 2 * gridGap;
			for (int i = 0; i < numOfOutputs / 2; i++) {
				x1 += gridGap;
				x2 += gridGap;
				g.fillRect(x1, y1, thickness, cableLen);
				g.drawString(Integer.toString(i), x2, y2);
			}
			x1 += gridGap;
			x2 += gridGap;
			for (int i = numOfOutputs / 2; i < numOfOutputs; i++) {
				x1 += gridGap;
				x2 += gridGap;
				g.fillRect(x1, y1, thickness, cableLen);
				g.drawString(Integer.toString(i), x2, y2);
			}
		}
	}

	public void paintNormal_180(Graphics g) {
		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;
			int height, width;

			int x1, y1, x2, y2;
			int thickness;
			int xOffset, yOffset;
			int cableLen;
			int xCenter = gridGap * this.getGridSize().width / 2;
			int yCenter = gridGap * this.getGridSize().height / 2;

			//body
			g.setColor(this.brush);
//			x1 = gridGap;
//			y1 = gridGap;
//			x2 = x1;
//			y2 = height - gridGap;
//			g.drawLine(x1, y1, x2, y2);	//left vertical line
//
//			x1 = 3 * gridGap;
//			y1 = 2 * gridGap;
//			x2 = x1;
//			y2 = height - 2 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//right vertical line
//
//			x1 = gridGap;
//			y1 = gridGap;
//			x2 = 3 * gridGap;
//			y2 = 2 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//top slope line
//
//			x1 = gridGap;
//			y1 = height - gridGap;
//			x2 = 3 * gridGap;
//			y2 = height - 2 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//bottom slope line
			x1 = gridGap;
			y1 = gridGap;
			width = 2 * gridGap;
			height = gridGap * (this.numOfOutputs + 2);
			g.drawRect(x1, y1, width, height);

			//label
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			x1 = xCenter + increment;
			y1 = yCenter + increment;
			g.drawString("D", x1, y1);

			//select line
			if (this.selectBusSize == 1) {
				xOffset = 0;
				thickness = 1;
			} else {
				xOffset = -1;
				thickness = 3;
			}
			x1 = xCenter + xOffset;
			cableLen = gridGap;
			y1 = height + gridGap;
			g.fillRect(x1, y1, thickness, cableLen);

			//output bus
			if (this.busSize == 1) {
				yOffset = 0;
				thickness = 1;
			} else {
				yOffset = -1;
				thickness = 3;
			}
			x1 = 3 * gridGap;
			y1 = yCenter + yOffset;
			cableLen = gridGap;
			g.fillRect(x1, y1, cableLen, thickness);

			//input bus
			xOffset = increment;
			x1 = 0;
			y1 = gridGap + yOffset;
			cableLen = gridGap;
			x2 = gridGap + xOffset;
			y2 = gridGap + increment;
			for (int i = 0; i < numOfOutputs / 2; i++) {
				y1 += gridGap;
				y2 += gridGap;
				g.fillRect(x1, y1, cableLen, thickness);
				g.drawString(Integer.toString(numOfOutputs - 1 - i), x2, y2);
			}
			y1 += gridGap;
			y2 += gridGap;
			for (int i = numOfOutputs / 2; i < numOfOutputs; i++) {
				y1 += gridGap;
				y2 += gridGap;
				g.fillRect(x1, y1, cableLen, thickness);
				g.drawString(Integer.toString(numOfOutputs - 1 - i), x2, y2);
			}
		}
	}

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

			int x1, y1, x2, y2;
			int thickness;
			int xOffset, yOffset;
			int cableLen;
			int xCenter = gridGap * this.getGridSize().width / 2;
			int yCenter = gridGap * this.getGridSize().height / 2;

			//body
			g.setColor(this.brush);
//			x1 = 2 * gridGap;
//			y1 = gridGap;
//			x2 = width - 2 * gridGap;
//			y2 = y1;
//			g.drawLine(x1, y1, x2, y2);	//top horizontal line
//
//			x1 = gridGap;
//			y1 = 3 * gridGap;
//			x2 = width - gridGap;
//			y2 = y1;
//			g.drawLine(x1, y1, x2, y2);	//bottom horizontal line
//
//			x1 = 2 * gridGap;
//			y1 = gridGap;
//			x2 = gridGap;
//			y2 = 3 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//left slope line
//
//			x1 = width - 2 * gridGap;
//			y1 = gridGap;
//			x2 = width - gridGap;
//			y2 = 3 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//right slope line
			x1 = gridGap;
			y1 = gridGap;
			width = gridGap * (this.numOfOutputs + 2);
			height = 2 * gridGap;
			g.drawRect(x1, y1, width, height);

			//label
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			x1 = xCenter - increment;
			y1 = 2 * gridGap;
			g.drawString("D", x1, y1);

			//select line
			if (this.selectBusSize == 1) {
				yOffset = 0;
				thickness = 1;
			} else {
				yOffset = -1;
				thickness = 3;
			}
			y1 = yCenter + yOffset;
			cableLen = gridGap;
			x1 = width + gridGap;
			g.fillRect(x1, y1, cableLen, thickness);

			//output bus
			if (this.busSize == 1) {
				xOffset = 0;
				thickness = 1;
			} else {
				xOffset = -1;
				thickness = 3;
			}
			x1 = xCenter + xOffset;
			y1 = 0;
			cableLen = gridGap;
			g.fillRect(x1, y1, thickness, cableLen);

			//input bus
			yOffset = increment;
			x1 = gridGap + xOffset;
			y1 = 3 * gridGap;
			cableLen = gridGap;
			x2 = x1;
			y2 = 3 * gridGap - increment;
			for (int i = 0; i < numOfOutputs / 2; i++) {
				x1 += gridGap;
				x2 += gridGap;
				g.fillRect(x1, y1, thickness, cableLen);
				g.drawString(Integer.toString(numOfOutputs - 1 - i), x2, y2);
			}
			x1 += gridGap;
			x2 += gridGap;
			for (int i = numOfOutputs / 2; i < numOfOutputs; i++) {
				x1 += gridGap;
				x2 += gridGap;
				g.fillRect(x1, y1, thickness, cableLen);
				g.drawString(Integer.toString(numOfOutputs - 1 - i), x2, y2);
			}
		}
	}

	public void paintFlipped_0(Graphics g) {
		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;
			int width, height;

			int x1, y1, x2, y2;
			int thickness;
			int xOffset, yOffset;
			int cableLen;
			int xCenter = gridGap * this.getGridSize().width / 2;
			int yCenter = gridGap * this.getGridSize().height / 2;

			//body
			g.setColor(this.brush);
//			x1 = gridGap;
//			y1 = 2 * gridGap;
//			x2 = x1;
//			y2 = height - 2 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//left vertical line
//
//			x1 = 3 * gridGap;
//			y1 = gridGap;
//			x2 = x1;
//			y2 = height - gridGap;
//			g.drawLine(x1, y1, x2, y2);	//right vertical line
//
//			x1 = gridGap;
//			y1 = 2 * gridGap;
//			x2 = 3 * gridGap;
//			y2 = gridGap;
//			g.drawLine(x1, y1, x2, y2);	//top slope line
//
//			x1 = 3 * gridGap;
//			y1 = height - gridGap;
//			x2 = gridGap;
//			y2 = height - 2 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//bottom slope line
			x1 = gridGap;
			y1 = gridGap;
			width = 2 * gridGap;
			height = gridGap * (this.numOfOutputs + 2);
			g.drawRect(x1, y1, width, height);

			//label
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			x1 = gridGap + increment;
			y1 = yCenter + increment;
			g.drawString("D", x1, y1);

			//select line
			if (this.selectBusSize == 1) {
				xOffset = 0;
				thickness = 1;
			} else {
				xOffset = -1;
				thickness = 3;
			}
			x1 = xCenter + xOffset;
			cableLen = gridGap;
			y1 = gridGap + height;
			g.fillRect(x1, y1, thickness, cableLen);

			//output bus
			if (this.busSize == 1) {
				yOffset = 0;
				thickness = 1;
			} else {
				yOffset = -1;
				thickness = 3;
			}
			x1 = 0;
			y1 = yCenter + yOffset;
			cableLen = gridGap;
			g.fillRect(x1, y1, cableLen, thickness);

			//input bus
			xOffset = increment;
			x1 = 3 * gridGap;
			y1 = gridGap + yOffset;
			cableLen = gridGap;
			x2 = xCenter + increment;
			y2 = gridGap + increment;
			for (int i = 0; i < numOfOutputs / 2; i++) {
				y1 += gridGap;
				y2 += gridGap;
				g.fillRect(x1, y1, cableLen, thickness);
				g.drawString(Integer.toString(numOfOutputs - 1 - i), x2, y2);
			}
			y1 += gridGap;
			y2 += gridGap;
			for (int i = numOfOutputs / 2; i < numOfOutputs; i++) {
				y1 += gridGap;
				y2 += gridGap;
				g.fillRect(x1, y1, cableLen, thickness);
				g.drawString(Integer.toString(numOfOutputs - 1 - i), x2, y2);
			}
		}
	}

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

			int x1, y1, x2, y2;
			int thickness;
			int xOffset, yOffset;
			int cableLen;
			int xCenter = gridGap * this.getGridSize().width / 2;
			int yCenter = gridGap * this.getGridSize().height / 2;

			//body
			g.setColor(this.brush);
//			x1 = 2 * gridGap;
//			y1 = gridGap;
//			x2 = width - 2 * gridGap;
//			y2 = y1;
//			g.drawLine(x1, y1, x2, y2);	//top horizontal line
//
//			x1 = gridGap;
//			y1 = 3 * gridGap;
//			x2 = width - gridGap;
//			y2 = y1;
//			g.drawLine(x1, y1, x2, y2);	//bottom horizontal line
//
//			x1 = 2 * gridGap;
//			y1 = gridGap;
//			x2 = gridGap;
//			y2 = 3 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//left slope line
//
//			x1 = width - 2 * gridGap;
//			y1 = gridGap;
//			x2 = width - gridGap;
//			y2 = 3 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//right slope line
			x1 = gridGap;
			y1 = gridGap;
			width = gridGap * (this.numOfOutputs + 2);
			height = 2 * gridGap;
			g.drawRect(x1, y1, width, height);

			//label
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			x1 = xCenter - increment;
			y1 = 2 * gridGap - increment;
			g.drawString("D", x1, y1);

			//select line
			if (this.selectBusSize == 1) {
				yOffset = 0;
				thickness = 1;
			} else {
				yOffset = -1;
				thickness = 3;
			}
			x1 = 0;
			y1 = yCenter + yOffset;
			cableLen = gridGap;
			g.fillRect(x1, y1, cableLen, thickness);

			//output bus
			if (this.busSize == 1) {
				xOffset = 0;
				thickness = 1;
			} else {
				xOffset = -1;
				thickness = 3;
			}
			x1 = xCenter + xOffset;
			y1 = 0;
			cableLen = gridGap;
			g.fillRect(x1, y1, thickness, cableLen);

			//input bus
			yOffset = increment;
			x1 = gridGap + xOffset;
			y1 = 3 * gridGap;
			cableLen = gridGap;
			x2 = x1;
			y2 = 3 * gridGap - increment;
			for (int i = 0; i < numOfOutputs / 2; i++) {
				x1 += gridGap;
				x2 += gridGap;
				g.fillRect(x1, y1, thickness, cableLen);
				g.drawString(Integer.toString(i), x2, y2);
			}
			x1 += gridGap;
			x2 += gridGap;
			for (int i = numOfOutputs / 2; i < numOfOutputs; i++) {
				x1 += gridGap;
				x2 += gridGap;
				g.fillRect(x1, y1, thickness, cableLen);
				g.drawString(Integer.toString(i), x2, y2);
			}
		}
	}

	public void paintFlipped_180(Graphics g) {
		// draw if visible
		if (this.isVisible()) {
			int gridGap = CentralPanel.ACTIVE_GRID.getCurrentGridGap();
			int increment = gridGap / 4;
			int height, width;

			int x1, y1, x2, y2;
			int thickness;
			int xOffset, yOffset;
			int cableLen;
			int xCenter = gridGap * this.getGridSize().width / 2;
			int yCenter = gridGap * this.getGridSize().height / 2;

			//body
			g.setColor(this.brush);
//			x1 = gridGap;
//			y1 = gridGap;
//			x2 = x1;
//			y2 = height - gridGap;
//			g.drawLine(x1, y1, x2, y2);	//left vertical line
//
//			x1 = 3 * gridGap;
//			y1 = 2 * gridGap;
//			x2 = x1;
//			y2 = height - 2 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//right vertical line
//
//			x1 = gridGap;
//			y1 = gridGap;
//			x2 = 3 * gridGap;
//			y2 = 2 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//top slope line
//
//			x1 = gridGap;
//			y1 = height - gridGap;
//			x2 = 3 * gridGap;
//			y2 = height - 2 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//bottom slope line
			x1 = gridGap;
			y1 = gridGap;
			width = 2 * gridGap;
			height = gridGap * (this.numOfOutputs + 2);
			g.drawRect(x1, y1, width, height);

			//label
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			x1 = xCenter + increment;
			y1 = yCenter + increment;
			g.drawString("D", x1, y1);

			//select line
			if (this.selectBusSize == 1) {
				xOffset = 0;
				thickness = 1;
			} else {
				xOffset = -1;
				thickness = 3;
			}
			x1 = xCenter + xOffset;
			cableLen = gridGap;
			y1 = 0;
			g.fillRect(x1, y1, thickness, cableLen);

			//output bus
			if (this.busSize == 1) {
				yOffset = 0;
				thickness = 1;
			} else {
				yOffset = -1;
				thickness = 3;
			}
			x1 = 3 * gridGap;
			y1 = yCenter + yOffset;
			cableLen = gridGap;
			g.fillRect(x1, y1, cableLen, thickness);

			//input bus
			xOffset = increment;
			x1 = 0;
			y1 = gridGap + yOffset;
			cableLen = gridGap;
			x2 = gridGap + xOffset;
			y2 = gridGap + increment;
			for (int i = 0; i < numOfOutputs / 2; i++) {
				y1 += gridGap;
				y2 += gridGap;
				g.fillRect(x1, y1, cableLen, thickness);
				g.drawString(Integer.toString(i), x2, y2);
			}
			y1 += gridGap;
			y2 += gridGap;
			for (int i = numOfOutputs / 2; i < numOfOutputs; i++) {
				y1 += gridGap;
				y2 += gridGap;
				g.fillRect(x1, y1, cableLen, thickness);
				g.drawString(Integer.toString(i), x2, y2);
			}
		}
	}

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

			int x1, y1, x2, y2;
			int thickness;
			int xOffset, yOffset;
			int cableLen;
			int xCenter = gridGap * this.getGridSize().width / 2;
			int yCenter = gridGap * this.getGridSize().height / 2;

			//body
			g.setColor(this.brush);
//			x1 = gridGap;
//			y1 = gridGap;
//			x2 = width - gridGap;
//			y2 = y1;
//			g.drawLine(x1, y1, x2, y2);	//top horizontal line
//
//			x1 = 2 * gridGap;
//			y1 = 3 * gridGap;
//			x2 = width - 2 * gridGap;
//			y2 = y1;
//			g.drawLine(x1, y1, x2, y2);	//bottom horizontal line
//
//			x1 = gridGap;
//			y1 = gridGap;
//			x2 = 2 * gridGap;
//			y2 = 3 * gridGap;
//			g.drawLine(x1, y1, x2, y2);	//left slope line
//
//			x1 = width - 2 * gridGap;
//			y1 = 3 * gridGap;
//			x2 = width - gridGap;
//			y2 = gridGap;
//			g.drawLine(x1, y1, x2, y2);	//right slope line
			x1 = gridGap;
			y1 = gridGap;
			width = gridGap * (this.numOfOutputs + 2);
			height = 2 * gridGap;
			g.drawRect(x1, y1, width, height);

			//label
			g.setFont(new Font(Wrapper.FONT_NAME, Font.PLAIN, 3 * increment));
			x1 = xCenter - increment;
			y1 = 3 * gridGap - increment;
			g.drawString("D", x1, y1);

			//select line
			if (this.selectBusSize == 1) {
				yOffset = 0;
				thickness = 1;
			} else {
				yOffset = -1;
				thickness = 3;
			}
			y1 = yCenter + yOffset;
			cableLen = gridGap + 2 * increment;
			x1 = width + gridGap;
			g.fillRect(x1, y1, cableLen, thickness);

			//output bus
			if (this.busSize == 1) {
				xOffset = 0;
				thickness = 1;
			} else {
				xOffset = -1;
				thickness = 3;
			}
			x1 = xCenter + xOffset;
			y1 = 3 * gridGap;
			cableLen = gridGap;
			g.fillRect(x1, y1, thickness, cableLen);

			//input bus
			yOffset = increment;
			x1 = gridGap + xOffset;
			y1 = 0;
			cableLen = gridGap;
			x2 = x1;
			y2 = 2 * gridGap;
			for (int i = 0; i < numOfOutputs / 2; i++) {
				x1 += gridGap;
				x2 += gridGap;
				g.fillRect(x1, y1, thickness, cableLen);
				g.drawString(Integer.toString(numOfOutputs - 1 - i), x2, y2);
			}
			x1 += gridGap;
			x2 += gridGap;
			for (int i = numOfOutputs / 2; i < numOfOutputs; i++) {
				x1 += gridGap;
				x2 += gridGap;
				g.fillRect(x1, y1, thickness, cableLen);
				g.drawString(Integer.toString(numOfOutputs - 1 - i), x2, y2);
			}
		}
	}

	protected boolean canDropNormal_0() {
		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int loop;

		// check outputs
		for (loop = 0; (loop < middle) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 2 + loop, this.busSize);
		}
		for (loop = middle; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 3 + loop, this.busSize);
		}
		//select line
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 2, this.gridLocation.y, this.selectBusSize);
		}
		// check input
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 2 + middle, this.busSize);
		}
		return result;
	}

	protected boolean canDropNormal_90() {
		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int loop;

		// check outputs
		for (loop = 0; (loop < middle) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 2 + loop, this.gridLocation.y, this.busSize);
		}
		for (loop = middle; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 3 + loop, this.gridLocation.y, this.busSize);
		}
		//select line
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 2, this.selectBusSize);
		}
		// check input
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 2 + middle, this.gridLocation.y + this.gridSize.height, this.busSize);
		}
		return result;
	}

	protected boolean canDropNormal_180() {
		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int loop;

		// check outputs
		for (loop = 0; (loop < middle) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + this.gridSize.height - 2 - loop, this.busSize);
		}
		for (loop = middle; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + this.gridSize.height - 3 - loop, this.busSize);
		}
		//select line
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 2, this.gridLocation.y + this.gridSize.height, this.selectBusSize);
		}
		// check input
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 2 + middle, this.busSize);
		}
		return result;
	}

	protected boolean canDropNormal_270() {
		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int loop;

		// check outputs
		for (loop = 0; (loop < middle) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width - 2 - loop, this.gridLocation.y + this.gridSize.height, this.busSize);
		}
		for (loop = middle; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width - 3 - loop, this.gridLocation.y + this.gridSize.height, this.busSize);
		}
		//select line
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 2, this.selectBusSize);
		}
		// check input
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 2 + middle, this.gridLocation.y, this.busSize);
		}
		return result;
	}

	protected boolean canDropFlipped_0() {
		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int loop;

		// check outputs
		for (loop = 0; (loop < middle) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + this.gridSize.height - 2 - loop, this.busSize);
		}
		for (loop = middle; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + this.gridSize.height - 3 - loop, this.busSize);
		}
		//select line
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 2, this.gridLocation.y + this.gridSize.height, this.selectBusSize);
		}
		// check input
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 2 + middle, this.busSize);
		}
		return result;
	}

	protected boolean canDropFlipped_90() {
		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int loop;

		// check outputs
		for (loop = 0; (loop < middle) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 2 + loop, this.gridLocation.y + this.gridSize.height, this.busSize);
		}
		for (loop = middle; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 3 + loop, this.gridLocation.y + this.gridSize.height, this.busSize);
		}
		//select line
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 2, this.selectBusSize);
		}
		// check input
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 2 + middle, this.gridLocation.y, this.busSize);
		}
		return result;
	}

	protected boolean canDropFlipped_180() {
		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int loop;

		// check outputs
		for (loop = 0; (loop < middle) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 2 + loop, this.busSize);
		}
		for (loop = middle; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x, this.gridLocation.y + 3 + loop, this.busSize);
		}
		//select line
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 2, this.gridLocation.y, this.selectBusSize);
		}
		// check input
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 2 + middle, this.busSize);
		}
		return result;
	}

	protected boolean canDropFlipped_270() {
		boolean result = true;
		int middle = this.numOfOutputs / 2;
		int loop;

		// check outputs
		for (loop = 0; (loop < middle) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width - 2 - loop, this.gridLocation.y, this.busSize);
		}
		for (loop = middle; (loop < this.numOfOutputs) && result; loop++) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width - 3 - loop, this.gridLocation.y, this.busSize);
		}
		//select line
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 2, this.selectBusSize);
		}
		// check input
		if (result) {
			result = Wrapper.canDropJuncion(this.gridLocation.x + 2 + middle, this.gridLocation.y + this.gridSize.height, this.busSize);
		}
		return result;
	}

	protected void dropedNormal_0() {
		int loop;
		int middle = this.numOfOutputs / 2;
		//outputs
		this.output.setSize(this.numOfOutputs);
		for (loop = 0; loop < middle; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 2 + loop, this.busSize));
		}
		for (loop = middle; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 3 + loop, this.busSize));
		}

		//select line
		this.select = Wrapper.setPinAt(this.gridLocation.x + 2, this.gridLocation.y, this.selectBusSize);
		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 2 + middle, this.busSize);

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

	protected void dropedNormal_90() {
		int loop;
		int middle = this.numOfOutputs / 2;
		//outputs
		this.output.setSize(this.numOfOutputs);
		for (loop = 0; loop < middle; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + 2 + loop, this.gridLocation.y, this.busSize));
		}
		for (loop = middle; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + 3 + loop, this.gridLocation.y, this.busSize));
		}

		//select line
		this.select = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 2, this.selectBusSize);
		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x + 2 + middle, this.gridLocation.y + this.gridSize.height, this.busSize);

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

	protected void dropedNormal_180() {
		int loop;
		int middle = this.numOfOutputs / 2;
		//outputs
		this.output.setSize(this.numOfOutputs);
		for (loop = 0; loop < middle; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + this.gridSize.height - 2 - loop, this.busSize));
		}
		for (loop = middle; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + this.gridSize.height - 3 - loop, this.busSize));
		}

		//select line
		this.select = Wrapper.setPinAt(this.gridLocation.x + 2, this.gridLocation.y + this.gridSize.height, this.selectBusSize);
		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 2 + middle, this.busSize);

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

	protected void dropedNormal_270() {
		int loop;
		int middle = this.numOfOutputs / 2;
		//outputs
		this.output.setSize(this.numOfOutputs);
		for (loop = 0; loop < middle; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width - 2 - loop, this.gridLocation.y + this.gridSize.height, this.busSize));
		}
		for (loop = middle; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width - 3 - loop, this.gridLocation.y + this.gridSize.height, this.busSize));
		}

		//select line
		this.select = Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 2, this.selectBusSize);
		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x + 2 + middle, this.gridLocation.y, this.busSize);

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

	protected void dropedFlipped_0() {
		int loop;
		int middle = this.numOfOutputs / 2;
		//outputs
		this.output.setSize(this.numOfOutputs);
		for (loop = 0; loop < middle; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + this.gridSize.height - 2 - loop, this.busSize));
		}
		for (loop = middle; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + this.gridSize.height - 3 - loop, this.busSize));
		}

		//select line
		this.select = Wrapper.setPinAt(this.gridLocation.x + 2, this.gridLocation.y + this.gridSize.height, this.selectBusSize);
		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 2 + middle, this.busSize);

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

	protected void dropedFlipped_90() {
		int loop;
		int middle = this.numOfOutputs / 2;
		//outputs
		this.output.setSize(this.numOfOutputs);
		for (loop = 0; loop < middle; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + 2 + loop, this.gridLocation.y + this.gridSize.height, this.busSize));
		}
		for (loop = middle; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + 3 + loop, this.gridLocation.y + this.gridSize.height, this.busSize));
		}

		//select line
		this.select = Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 2, this.selectBusSize);
		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x + 2 + middle, this.gridLocation.y, this.busSize);

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

	protected void dropedFlipped_180() {
		int loop;
		int middle = this.numOfOutputs / 2;
		//outputs
		this.output.setSize(this.numOfOutputs);
		for (loop = 0; loop < middle; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 2 + loop, this.busSize));
		}
		for (loop = middle; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x, this.gridLocation.y + 3 + loop, this.busSize));
		}

		//select line
		this.select = Wrapper.setPinAt(this.gridLocation.x + 2, this.gridLocation.y, this.selectBusSize);
		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 2 + middle, this.busSize);

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

	protected void dropedFlipped_270() {
		int loop;
		int middle = this.numOfOutputs / 2;
		//outputs
		this.output.setSize(this.numOfOutputs);
		for (loop = 0; loop < middle; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width - 2 - loop, this.gridLocation.y, this.busSize));
		}
		for (loop = middle; loop < this.numOfOutputs; loop++) {
			this.output.changeItem(loop, Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width - 3 - loop, this.gridLocation.y, this.busSize));
		}

		//select line
		this.select = Wrapper.setPinAt(this.gridLocation.x + this.gridSize.width, this.gridLocation.y + 2, this.selectBusSize);
		//input
		this.input = Wrapper.setPinAt(this.gridLocation.x + 2 + middle, this.gridLocation.y + this.gridSize.height, this.busSize);

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

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

	public boolean hasProperties() {
		return true;
	}

	public Component getPropertyWindow() {
		return (new MuxProperties(this.busSize, this.selectBusSize, this.delay));
	}

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

		this.delay = dp.getDelay();

		if (this.oldBusSize == 0) {
			this.oldBusSize = this.busSize;
			this.oldSelectSize = this.selectBusSize;
		}

		CentralPanel.ACTIVE_GRID.eraseComponent(this);
		this.setBusSize(dp.getBusSize());
		this.setNumOfOutputs((int) Math.pow(2, dp.getInputSize()));
		CentralPanel.ACTIVE_GRID.paintComponent(this);

	}

	public void restoreOriginalProperties() {
		if (this.oldBusSize != 0) {
			this.setBusSize(this.oldBusSize);
			this.oldBusSize = 0;
			this.setNumOfOutputs((int) Math.pow(2, this.oldSelectSize));
		}
	}
	
	@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 j = 0; j < this.selectBusSize; j++){
					writer.write("s"+j+",");
				}
				for(int i = 0; i < this.busSize; i++){
					if(i != 0)
						writer.write(",");
					writer.write("x" + i);
				}
				writer.write(" : in std_logic;\n");
				for(int j = 0; j < (Math.pow(2,this.selectBusSize)); j++){
					if(j != 0)
						writer.write (",");
					for(int i = 0; i < this.busSize; i++){
						if(i != 0)
							writer.write(",");
						writer.write("f" + j +""+ 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 sel : std_logic_vector("+(this.selectBusSize-1)+" downto 0);\n");
				writer.write("begin\n");
				for(int j = 0; j < this.selectBusSize; j++){
					writer.write("sel("+j+") <= s"+j+";\n");
				}
				for(int i = 0; i < this.busSize; i++){
					for(int j = 0; j < (Math.pow(2,this.selectBusSize)); j++){
						writer.write("f"+j+i+" <= x"+i+" when to_integer(unsigned(sel))=" + 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 "DEMUX_SEL"+this.selectBusSize+"_DATA"+this.busSize;
	}
	
}
