package net.woodyfolsom.msproj;

import org.apache.log4j.Logger;

public class CommandParser {
	private static final Logger LOGGER = Logger.getLogger(CommandParser.class
			.getName());

	//private static final String INT_PATTERN = "^(\\d+.*|-\\d+.*)";

	/**
	 * This very simple parser converts a small subset of GTP command strings
	 * into Commands.
	 * 
	 * @param commandString
	 * @return
	 */
	public static Command parse(String commandString) {
		LOGGER.info("Parsing command: " + commandString);
		//split on whitespace
		if (commandString == null) {
			return new Command(Command.TYPE.INVALID,commandString,new String[0]);
		}
		
		String cmdFields[] = commandString.split("\\s+");
		if (cmdFields.length == 0) {
			return new Command(Command.TYPE.INVALID,commandString,new String[0]);
		}
		
		if ("boardsize".equals(cmdFields[0])) {
			return new Command(Command.TYPE.boardsize,commandString,cmdFields);
		} else if ("clear_board".equals(cmdFields[0])) {
			return new Command(Command.TYPE.clear_board,commandString,cmdFields);
		} else if ("final_status_list".equals(cmdFields[0])) {
			return new Command(Command.TYPE.final_status_list,commandString,cmdFields);
		} else if ("genmove".equals(cmdFields[0])) {
			return new Command(Command.TYPE.genmove,commandString,cmdFields);
		} else if ("komi".equals(cmdFields[0])) {
			return new Command(Command.TYPE.komi,commandString,cmdFields);
		} else if ("list_commands".equals(cmdFields[0])) {
			return new Command(Command.TYPE.list_commands,commandString,cmdFields);
		} else if ("name".equals(cmdFields[0])) {
			return new Command(Command.TYPE.name,commandString,cmdFields);
		} else if ("play".equals(cmdFields[0])) {
			return new Command(Command.TYPE.play,commandString,cmdFields);
		} else if ("quit".equals(cmdFields[0])) {
			return new Command(Command.TYPE.quit,commandString,cmdFields);
		} else if ("version".equals(cmdFields[0])) {
			return new Command(Command.TYPE.version,commandString,cmdFields);
		} else {
			return new Command(Command.TYPE.INVALID,commandString,cmdFields);	
		}
	}
}
