package services;

import java.util.Scanner;

public class VirtualConnectionService implements ConnectionService
{
	private boolean isRunning;
	private Scanner sc;
	private boolean inputs[] = new boolean[16];
	private boolean outputs[] = new boolean[16];

	public VirtualConnectionService()
	{
		isRunning = true;
		sc = new Scanner(System.in);

		new Thread(new Runnable() {
			@Override
			public void run()
			{
				System.out.println("Interactive Command Interpreter");
				System.out.println("   inputs=[hex]      - set inputs");
				System.out.println("   outputs=[hex]     - set outputs");
				System.out.println("   outputs           - print outputs");
				while (isRunning) {
					System.out.print(" > ");
					String args[] = sc.nextLine().split("=");
					if (args[0].trim().equals("inputs")) {
						if (args.length == 1)
							continue;
						setInputs(args[1].trim());
						System.out.println("set input=" + args[1]);

					}
					else if (args[0].trim().equals("outputs")) {
						if (args.length == 1)
						{
							String outputString = "|";
							for (int i = outputs.length - 1; i >= 0; i--)
								outputString += String.format("%02d|", i);

							outputString += "\n|";
							for (int i = outputs.length - 1; i >= 0; i--)
								outputString += (outputs[i] ? " 1|" : " 0|");

							System.out.println(outputString);
							continue;
						}
						setOutputs(args[1].trim());
						System.out.println("set output=" + args[1]);
					}
				}
			}
		}).start();
	}

	void setInputs(String values)
	{
		String binary = Integer.toBinaryString(Integer.parseInt(values, 16));

		for (int i = binary.length() - 1; i >= 0; i--)
			inputs[binary.length() - i - 1] = binary.charAt(i) == '1';
	}

	void setOutputs(String values)
	{
		String binary = Integer.toBinaryString(Integer.parseInt(values, 16));

		for (int i = binary.length() - 1; i >= 0; i--)
			outputs[binary.length() - i - 1] = binary.charAt(i) == '1';
	}

	@Override
	public boolean[] getInputs() throws Exception
	{
		return inputs;
	}

	@Override
	public boolean[] getOutputs() throws Exception
	{
		return outputs;
	}

	@Override
	public void setOutput(int port, boolean value) throws Exception
	{
		outputs[port] = value;
	}

	@Override
	public void close()
	{
		isRunning = false;
	}
}

@@ -582,32 +582,95 @@
  set outputs%22);%0A
+%09%09%09%09System.out.println(%22   inputs            - print inputs%22);%0A
 %09%09%09%09System.out.p
@@ -886,24 +886,342 @@
 ength == 1)%0A
+%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09String inputString = %22%7C%22;%0A%09%09%09%09%09%09%09for (int i = inputs.length - 1; i %3E= 0; i--)%0A%09%09%09%09%09%09%09%09inputString += String.format(%22%2502d%7C%22, i);%0A%0A%09%09%09%09%09%09%09inputString += %22%5Cn%7C%22;%0A%09%09%09%09%09%09%09for (int i = inputs.length - 1; i %3E= 0; i--)%0A%09%09%09%09%09%09%09%09inputString += (inputs%5Bi%5D ? %22 1%7C%22 : %22 0%7C%22);%0A%0A%09%09%09%09%09%09%09System.out.println(inputString);%0A
 %09%09%09%09%09%09%09conti
@@ -1221,24 +1221,32 @@
 %09%09continue;%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09setInp
@@ -1313,24 +1313,25 @@
  args%5B1%5D);%0A%0A
+%0A
 %09%09%09%09%09%7D%0A%09%09%09%09%09
@@ -1903,38 +1903,35 @@
 ng values)%0A%09%7B%0A%09%09
-Str
 in
-g
+t
  binary = Intege
@@ -1924,39 +1924,16 @@
 inary = 
-Integer.toBinaryString(
 Integer.
@@ -1944,33 +1944,32 @@
 eInt(values, 16)
-)
 ;%0A%0A%09%09for (int i 
@@ -1974,103 +1974,76 @@
 i = 
-binary.length() - 1; i %3E= 0; i--)%0A%09%09%09inputs%5Bbinary.length() - i - 1%5D = binary.charAt(i) == '1';
+0; i %3C inputs.length; i++)%0A%09%09%09inputs%5Bi%5D = ((1 %3C%3C i) & binary) != 0;%0A
 %0A%09%7D%0A
@@ -2080,22 +2080,19 @@
 s)%0A%09%7B%0A%09%09
-Str
 in
-g
+t
  binary 
@@ -2097,31 +2097,8 @@
 y = 
-Integer.toBinaryString(
 Inte
@@ -2121,17 +2121,16 @@
 ues, 16)
-)
 ;%0A%0A%09%09for
@@ -2143,103 +2143,76 @@
 i = 
-binary.length() - 1; i %3E= 0; i--)%0A%09%09%09outputs%5Bbinary.length() - i - 1%5D = binary.charAt(i) == '1'
+0; i %3C outputs.length; i++)%0A%09%09%09outputs%5Bi%5D = ((1 %3C%3C i) & binary) != 0
 ;%0A%09%7D

fix input/output setting