package game.goals;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Random;

import game.Game;
import game.Goal;
import game.Player;
import game.map.Castle;

public class CaptureTheFlagGoal extends Goal {

	final static boolean DEBUG = true;
	
	final static int ROUND_FOR_DISTRIBUTION = 2;
	final static int MIN_ROUND_FOR_WIN = 2;
	final static int NUM_FLAG_CASTLES = 4;
	
	List<Castle> flagCastles;
	
	private static Random random = new Random();
	private static int newRandomInt(int min, int max) {
		return min + random.nextInt(max - min + 1);
	}
	
	public CaptureTheFlagGoal() {
		super("Capture the flag", "Derjenige Spieler gewinnt, der zuerst"
				+ " alle " + NUM_FLAG_CASTLES + " Flaggen-Schlösser erobert.");
		
		flagCastles = new ArrayList<Castle>();
	}
	
	@Override
	public boolean isCompleted() {
		
		if (DEBUG)
			System.out.println("Checking is completed");
		
		Game game = this.getGame();
		
		// As this function is called every round,
		// let's check if we can set the flag castles
		if (flagCastles.isEmpty() && game.getRound() >= ROUND_FOR_DISTRIBUTION) {
			if (DEBUG)
				System.out.println("Distributing");
			distributeFlagCastles();
			return false;
		}
		
		return this.getWinner() != null;
	}

	@Override
	public Player getWinner() {
		
		Game game = this.getGame();
		
		if (game.getRound() < MIN_ROUND_FOR_WIN)
			return null;
		
		if (flagCastles.size() < 1)
			return null;
		
		return
		// Check if all flag castles belong
		// to exactly one player
		flagCastles.stream()
		    .map(x -> x.getOwner())
			.reduce(flagCastles.get(0).getOwner(), (x, y) -> {
				if (x == null || y == null)
					return null;
				else if (!x.equals(y))
					return null;
				else
					return x;
			});
				
	}

	@Override
	public boolean hasLost(Player player) {
		
		if (isCompleted())
			return player.equals(getWinner());
		
		return false;
		
	}
	
	/**
	 * Distribute the flag castles evenly to
	 * all players
	 */
	private void distributeFlagCastles() {
		
		Game game = this.getGame();
		List<Player> players = game.getPlayers();
		
		int numCastlesToDistribute = 
		/*if*/	NUM_FLAG_CASTLES < players.size() ?
		/*then*/		players.size() :
		/*else*/		NUM_FLAG_CASTLES;
					
		for (int i = 0; i < players.size(); i++) {
			
			if (DEBUG)
				System.out.println("Distributing for " + players.get(i).getName());
			
			int playersLeft = players.size() - i;
			int numCastlesForPlayer = numCastlesToDistribute / playersLeft;
			
			if (numCastlesForPlayer > players.get(i).getCastles(game).size())
				numCastlesForPlayer = players.get(i).getCastles(game).size();
			
			pickRandomCastles(players.get(i).getCastles(game), numCastlesForPlayer)
				.stream()
				.forEach(x -> flagCastles.add(x));
			
			numCastlesToDistribute -= numCastlesForPlayer;
			
		}
		
	}
	
	/**
	 * Pick a number of random castles
	 * @param castles All castles
	 * @param num The number of castles to choose
	 * @return The picked castles
	 */
	private List<Castle> pickRandomCastles(List<Castle> castles, int num) {
		
		int min = 0;
		int max = castles.size();
		
		List<Integer> nums = new ArrayList<Integer>();
		
		if (num >= max)
			return new ArrayList<Castle>(castles);
		
		for (int i = 0; i < num; i++) {
			
			int newNum = newRandomInt(min, max);
			while (nums.contains(newNum))
				newNum = newRandomInt(min, max);
			nums.add(newNum);
			
		}
		
		if (DEBUG)
			System.out.println(nums);
		
		return nums.stream().map(x -> castles.get(x)).collect(Collectors.toList());
		
		
	}
	
	

}
