package nl.hanze.gameserver.server.command;

import nl.hanze.gameserver.app.Application;
import nl.hanze.gameserver.server.Client;
import nl.hanze.gameserver.server.message.Command;
import nl.hanze.gameserver.server.message.ErrorResponse;
import nl.hanze.gameserver.server.message.Response;
import nl.hanze.gameserver.util.StringUtils;

import java.util.ArrayList;

public class GetCommandHandler extends AbstractCommandHandler {

	public GetCommandHandler() {
		super("get");
	}

	@Override
	public void handleCommand(Client client, Command command) {
		if(command.getArgument().equalsIgnoreCase("gamelist")) {
			client.writeResponse(Response.OK);
			client.writeResponse(new Response(Response.Status.SVR, String.format("GAMELIST %s", getGameListString())));
		} else if(command.getArgument().equals("playerlist")) {
			client.writeResponse(Response.OK);
			client.writeResponse(new Response(Response.Status.SVR, String.format("PLAYERLIST %s", getPlayerListString())));
		} else {
			client.writeResponse(new ErrorResponse(String.format("Unknown GET argument: '%s'", command.getArgument())));
		}
	}

	public String getPlayerListString() {
		ArrayList<Client> playerList = Application.getInstance().getGameServer().getClientManager().getPlayerList();
		ArrayList<String> playerNameList = new ArrayList<String>(playerList.size());
		for(Client client : playerList) {
			playerNameList.add(client.getPlayerName());
		}

		return StringUtils.toString(playerNameList);
	}

	private String getGameListString() {
		return StringUtils.toString(Application.getInstance().getGameLoader().getGameTypeList());
	}

	@Override
	public String getDescription() {
		return "Get server data";
	}

	@Override
	public ArrayList<String> getUsage() {
		ArrayList<String> responseList = new ArrayList<String>();

		responseList.add("usage: get <gamelist | playerlist>");
		responseList.add("");
		responseList.add("Valid options and arguments:");
		responseList.add("  <gamelist>      : Available games");
		responseList.add("  <playerlist>    : Currently logged in players");

		return responseList;
	}

}

@@ -550,16 +550,17 @@
 ) %7B%0A%09%09if
+ 
 (command
@@ -766,16 +766,17 @@
  else if
+ 
 (command
@@ -796,16 +796,26 @@
 ).equals
+IgnoreCase
 (%22player
@@ -1342,16 +1342,17 @@
 );%0A%09%09for
+ 
 (Client 

make "get playerlist" case insensitive, fixes #19