package server.awk;

import server.adt.NetworkToken;
import utils.buffer.OutputBuffer;

public class AWK implements Runnable{
	
	private final OutputBuffer<NetworkToken> buffer;
	private boolean isStopped = false;
	

	
	public AWK(OutputBuffer<NetworkToken> buffer){
		this.buffer = buffer;
	}
	
	
	@Override
	public void run() {
		while(!isStopped){
			NetworkToken token = buffer.getMessageFromInput();
			String inputMessage = token.getMessage();
			
			/* execute command */
			String outputMessage = convertImput(inputMessage);
			
			buffer.addMessageIntoOutput(new NetworkToken(outputMessage, token.getClientID(), token.getClientIP()));
		}
	}
	
	public void stop(){
		
	}
	
	
	private String convertImput(String input){
		String returnMessage;
		
		/* split inputMessage into two parts,
		 * firstpart: command 
		 * second: message  */
		String[] splitString = input.split(" ", 2);
		
		String command = splitString[0];
		
		if(command.equals("BYE")){
			/* do what you have to do here */
		}
		else if (splitString.length < 2){
			return returnMessage = "ERROR SYNTAX ERROR: MESSAGE OR PASSWORD IS MISSING";
		}
		
		String message = splitString[1];
		
		switch (command){				
			case "LOWERCASE":	returnMessage = convertToLowercase(message);
								break;
			case "UPPERCASE": 	returnMessage = convertToUppercase(message);
								break;
			case "REVERSE":		returnMessage = convertToReverseString(message);
								break;
			case "SHUTDOWN":						
			default: 			returnMessage = "ERROR UNKNOWN COMMAND";
								break;
		}
		
		return returnMessage;
		
	}
	
	
	
	private String convertToLowercase(String text){
		return text.toLowerCase();
	}
	
	
	private String convertToUppercase(String text){
		return text.toUpperCase();
	}
	
	private String convertToReverseString(String text){
		return new StringBuilder(text).reverse().toString();
		
	}

}

@@ -672,24 +672,172 @@
 )%7B%0A%09%09%0A%09%7D%0A%09%0A%09
+/**%0A%09 * converts the input string to the intended output string%0A%09 * @param input  string which has to be converted%0A%09 * @return  converted input%0A%09 */
 %0A%09private St
@@ -1079,78 +1079,370 @@
 %09%0A%09%09
-if(command.equals(%22BYE%22))%7B%0A%09%09%09/* do what you have to do here */%0A
+/* If empty string is given*/ %0A%09%09if(command.equals(%22%5Cn%22))%7B%0A%09%09%09return returnMessage = %22ERROR SYNTAX ERROR: EMPTY STRING%22;%0A%09%09%7D%0A%09%09%0A%09%09if(command.equals(%22BYE%22))%7B%0A%09%09%09if(splitString.length == 1)%7B%0A%09%09%09%09return returnMessage = %22OK BYE%22;%0A%09%09%09%7Delse if(splitString.length %3E 1)%7B%0A%09%09%09%09return returnMessage = %22 ERROR SNTAX ERROR: COMMAND 'BYE' DOES NOT EXPECT PARAMETERS%22;%0A%09
 %09%09%7D%0A%09%09
+%7D
 else
@@ -1887,21 +1887,16 @@
 TDOWN%22:%09
-%09%09%09%09%09
 %0A%09%09%09defa

add Error exceptions if unser input is worng to awk