package core.commands;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import core.entities.Game;
import core.entities.Game.GameStatus;
import core.entities.menus.RepickConfirmationMenu;
import core.exceptions.InvalidUseException;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;

public class CmdRepick extends Command {

	private static List<Long> gameList = Collections.synchronizedList(new ArrayList<Long>());
	
	@Override
	public Message execCommand(Member caller, String[] args) {
		Game game = server.getQueueManager().getPlayersGame(caller);
		
		if(game == null){
			throw new InvalidUseException("You are not in-game");
		}
		
		if(!game.isCaptain(caller)){
			throw new InvalidUseException("You must be a captain to use this command");
		}
		
		if(game.getStatus() != GameStatus.PLAYING){
			throw new InvalidUseException("You must finish picking before calling for a repick");
		}
		
		if(!gameList.contains(game.getTimestamp())){
			gameList.add(game.getTimestamp());
			
			new Thread(new Runnable(){
				public void run(){
					Member captain2;
					
					if(game.getTeam1().getCaptain() == caller) {
						captain2 = game.getTeam2().getCaptain();
					} else {
						captain2 = game.getTeam1().getCaptain();
					}
					
					RepickConfirmationMenu menu = new RepickConfirmationMenu(captain2);
					
					if(menu.getResult()){
						game.repick();
					}
					
					gameList.remove(game.getTimestamp());
				}
			}).start();
		}
		
		return null;
	}

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

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

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

	@Override
	public String getDescription() {
		return "Restarts the picking process for your game";
	}

	@Override
	public String getHelp() {
		return getBaseCommand();
	}

}

@@ -49,38 +49,8 @@
 st;%0A
-import java.util.Collections;%0A
 impo
@@ -100,46 +100,8 @@
 me;%0A
-import core.entities.Game.GameStatus;%0A
 impo
@@ -338,15 +338,8 @@
 ate 
-static 
 List
@@ -360,37 +360,8 @@
 t = 
-Collections.synchronizedList(
 new 
@@ -377,17 +377,16 @@
 %3CLong%3E()
-)
 ;%0A%09%0A%09@Ov
@@ -723,150 +723,8 @@
 %0A%09%09%0A
-%09%09if(game.getStatus() != GameStatus.PLAYING)%7B%0A%09%09%09throw new InvalidUseException(%22You must finish picking before calling for a repick%22);%0A%09%09%7D%0A%09%09%0A
 %09%09if

Allow repick before picking has finished