package core.commands;

import java.util.concurrent.TimeUnit;

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 CmdFinishGame extends Command {
	
	public CmdFinishGame(Server server) {
		super(server);
	}

	@Override
	public Message execCommand(Member caller, String[] args) {
		QueueManager qm = server.getQueueManager();
		
		if(qm.isPlayerWaiting(caller)){
			return null;
		}
		
		Game game = qm.getPlayersGame(caller);
		
		if(game == null){
			throw new InvalidUseException("You are not currently in-game");
		}
		
		if(!(caller.equals(game.getCaptain1()) || caller.equals(game.getCaptain2()))){
			throw new InvalidUseException("You must be a captain to finish your game");
		}
		
		String title = String.format("Game '%s' finished", game.getQueueName());
		
		if(args.length == 0){
			if(game.getStatus() == GameStatus.PICKING || server.isAdmin(caller)){
				qm.finishGame(game, null);
				
				return Utils.createMessage("`" + title + "`");
			}
			
			throw new BadArgumentsException();
		}
		
		String result = args[0].toLowerCase();
		int team = caller.equals(game.getCaptain1()) ? 1 : 2;
		int winningTeam;
		
		switch(result){
		case "tie": winningTeam = 0; break;
		case "win": winningTeam = team; break;
		case "loss": winningTeam = (team == 1) ? 2 : 1; break;
		default: throw new BadArgumentsException("Result must either be `win`, `loss`, or `tie`");
		}
		
		qm.finishGame(game, winningTeam);
		
		long msDiff = System.currentTimeMillis() - game.getTimestamp();
		long duration = TimeUnit.MINUTES.convert(msDiff, TimeUnit.MILLISECONDS);
		
		if(winningTeam != 0){
			Member winner = (winningTeam == 1) ? game.getCaptain1() : game.getCaptain2();
			String teamName = "Team " + winner.getEffectiveName();
			
			response = Utils.createMessage(title, String.format("**Winner:** %s%n**Duration:** %d Minutes",
					teamName, duration), true);
		}else{
			response = Utils.createMessage(title, String.format("**Tie game**%n**Duration:** %d Minutes", duration), true);
		}
		
		return response;
	}

	@Override
	public boolean isAdminRequired() {
		return false;
	}

	@Override
	public boolean isGlobalCommand() {
		return false;
	}

	@Override
	public String getName() {
		return "FinishGame";
	}

	@Override
	public String getDescription() {
		return "Finishes a game so that users can requeue (usable only by captains and admins)";
	}

	@Override
	public String getHelp() {
		return  getBaseCommand() + " - Finishes an unstarted game\n" +
				getBaseCommand() + " win/loss/tie - Finishes a game and records the result";
	}

}

@@ -823,79 +823,29 @@
 if(!
-(caller.equals(
 game.
-get
+is
 Captain
-1()) %7C%7C caller.equals(game.getCaptain2())
+(caller
 ))%7B%0A
@@ -934,111 +934,8 @@
 %0A%09%09%0A
-%09%09String title = String.format(%22Game '%25s' finished%22, game.getQueueName());%0A%09%09%0A%09%09if(args.length == 0)%7B%0A%09
 %09%09if
@@ -981,34 +981,27 @@
  %7C%7C 
-server.isAdmin(caller)
+args.length == 0
 )%7B%0A
-%09
 %09%09%09q
@@ -1029,19 +1029,17 @@
 ll);%0A%09%09%09
-%09%0A%09
+%0A
 %09%09%09retur
@@ -1066,79 +1066,108 @@
 e(%22%60
-%22 + title + %22
+Game cancelled
 %60%22);%0A%09%09
-%09
 %7D%0A%09%09
-%09
 %0A%09%09
-%09throw new BadArgumentsException();%0A%09%09%7D%0A%09%09
+String title = String.format(%22Game '%25s' finished%22, game.getQueueName());
 %0A%09%09S
@@ -1506,31 +1506,37 @@
  be 
-%60win%60, %60
+**win**, **
 loss
-%60
+**
 , or 
-%60tie%60
+**tie**
 %22);%0A
@@ -2477,45 +2477,8 @@
 ueue
- (usable only by captains and admins)
 %22;%0A%09

Fix logic again