package core.commands;

import core.Constants;
import core.entities.Game;
import core.entities.Game.GameStatus;
import core.entities.QueueManager;
import core.entities.Server;
import core.exceptions.BadArgumentsException;
import core.exceptions.InvalidUseException;
import core.util.Utils;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;

public class CmdSubCaptain extends Command {

	public CmdSubCaptain(Server server) {
		this.name = Constants.SUBCAPTAIN_NAME;
		this.description = Constants.SUBCAPTAIN_DESC;
		this.helpMsg = Constants.SUBCAPTAIN_HELP;
		this.pugChannelOnlyCommand = true;
		this.server = server;
	}

	@Override
	public Message execCommand(Member caller, String[] args) {
		if (args.length != 1) {
			throw new BadArgumentsException();
		}

		QueueManager qm = server.getQueueManager();
		Member target = server.getMember(args[0]);

		if (!qm.isPlayerIngame(caller)) {
			throw new InvalidUseException("You are not in-game");
		}

		if (qm.isPlayerIngame(target)) {
			throw new InvalidUseException(target.getEffectiveName() + " is not in-game");
		}

		Game game = qm.getPlayersGame(caller);

		if (game.getStatus() != GameStatus.PICKING) {
			throw new InvalidUseException("Picking has finished");
		}

		if (game.getCaptain1() == target || game.getCaptain2() == target) {
			throw new InvalidUseException(target.getEffectiveName() + " is not a captain in your game");
		}

		if (game.getCaptain1() != caller && game.getCaptain2() != caller) {
			throw new InvalidUseException("You are already a captain");
		}

		game.subCaptain(caller, target);

		this.response = Utils.createMessage(String.format("`%s has replaced %s as captain`", caller.getEffectiveName(),
				target.getEffectiveName()));

		return response;
	}
}

@@ -992,129 +992,8 @@
 %09%7D%0A%0A
-%09%09if (qm.isPlayerIngame(target)) %7B%0A%09%09%09throw new InvalidUseException(target.getEffectiveName() + %22 is not in-game%22);%0A%09%09%7D%0A%0A
 %09%09Ga
@@ -1124,16 +1124,24 @@
 ing has 
+already 
 finished
@@ -1146,32 +1146,34 @@
 ed%22);%0A%09%09%7D%0A%0A%09%09if 
+(!
 (game.getCaptain
@@ -1218,16 +1218,17 @@
  target)
+)
  %7B%0A%09%09%09th
@@ -1348,17 +1348,17 @@
 tain1() 
-!
+=
 = caller
@@ -1362,10 +1362,10 @@
 ler 
-&&
+%7C%7C
  gam
@@ -1380,17 +1380,17 @@
 tain2() 
-!
+=
 = caller

Correct logic