package game.goals;

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

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

public class PopulationGoal extends Goal {

	private int populationNeeded = 10;
	private int castlesNeeded = 5;
	
	public PopulationGoal() {
		super("Bevölkerung", "Derjenige Spieler gewinnt, der als erstes jeweils 10 Truppen auf 5 Burgen bringt");
	}
	
    @Override
    public boolean isCompleted() {
        return this.getWinner() != null;
    }
    
    @Override
    public Player getWinner() {
        Game game = this.getGame();
        
        if(game.getRound() < 2)
            return null;

       
        // Ansonsten: Bevölkerung zählen
        
        int numberOfHighPopulationCastles = 0;
        
        List<Player> highPopulationPlayers = new ArrayList<>();
        
        for (Player p : game.getPlayers()) {
        	for(Castle c : p.getCastles(game)) {
        		if(c.getTroopCount() >= populationNeeded) {
        			numberOfHighPopulationCastles++;
        		}
        	}
        	
        	if(numberOfHighPopulationCastles >= castlesNeeded) {
        		highPopulationPlayers.add(p);
        	}
        	
        	numberOfHighPopulationCastles = 0;
        	
        }
        
        if(highPopulationPlayers.size() == 1) {
        	return highPopulationPlayers.get(0);
        } else if(highPopulationPlayers.size() > 1) {
        	// Spieler mit insgesamt größter Bevölkerung gewinnt
        	
        	Comparator<Player> comp = new Comparator<Player>() {

				@Override
				public int compare(Player o1, Player o2) {
					return Integer.compare(o1.getNumRegions(game), o2.getNumRegions(game));
				}
        		
        	};
        	return Collections.max(highPopulationPlayers, comp);      	
        }
        
        // Ansonsten... gewinnt noch kein spieler.
        return null;
        
    }
    
    
    @Override
    public boolean hasLost(Player player) {
        if(getGame().getRound() < 2)
            return false;

        return player.getNumRegions(getGame()) == 0 || (this.isCompleted() && !player.equals(this.getWinner()));
    }
    
}
