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(){
		
	}
	
	/**
	 * converts the input string to the intended output string
	 * @param input  string which has to be converted
	 * @return  converted input
	 */
	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 empty string is given*/ 
		if(command.equals("\n")){
			return returnMessage = "ERROR SYNTAX ERROR: EMPTY STRING";
		}
		
		if(command.equals("BYE")){
			if(splitString.length == 1){
				return returnMessage = "OK BYE";
			}else if(splitString.length > 1){
				return returnMessage = " ERROR SNTAX ERROR: COMMAND 'BYE' DOES NOT EXPECT PARAMETERS";
			}
		}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();
		
	}

}

@@ -46,16 +46,53 @@
 kToken;%0A
+import server.controller.Controller;%0A
 import u
@@ -434,24 +434,54 @@
 Input();%0A%09%09%09
+%0A%09%09%09if(token != null)%7B%09%09%09%0A%09%09%09%09
 String input
@@ -513,17 +513,19 @@
 e();%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%09/* ex
@@ -541,16 +541,17 @@
 mand */%0A
+%09
 %09%09%09Strin
@@ -599,17 +599,19 @@
 ge);%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%09buffe
@@ -709,16 +709,21 @@
 IP()));%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%09
@@ -746,16 +746,33 @@
 op()%7B%0A%09%09
+isStopped = true;
 %0A%09%7D%0A%09%0A%09/
@@ -1981,16 +1981,71 @@
 TDOWN%22:%09
+returnMessage = shutdownServer(message);%0A%09%09%09%09%09%09%09%09break;
 %0A%09%09%09defa
@@ -2432,12 +2432,238 @@
 );%0A%09%09%0A%09%7D
+%0A%09%0A%09private String shutdownServer(String password)%7B%0A%09%09boolean bool = Controller.shutdown(password);%0A%09%09%0A%09%09if(bool == true)%7B%0A%09%09%09return %22OK SHUTDOWN%22;%0A%09%09%7Delse%7B%0A%09%09%09return %22ERROR SERVER COULD NOT SHUTDOWN PASSWORD WRONG%22;%0A%09%09%7D%0A%09%09%0A%09%7D
 %0A%0A%7D%0A

implement awk