package electrosphere.net.server.player;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.server.datacell.Realm;

/**
 * Server player manager
 */
public class PlayerManager {
    
    Map<Integer,Player> idMap = new HashMap<Integer,Player>();
    
    public PlayerManager(){
        
    }
    
    public void registerPlayer(Player player){
        idMap.put(player.getId(),player);
    }
    
    public Player getPlayerFromId(int id){
        return idMap.get(id);
    }

    /**
     * Gets the first registered player
     * @return The first registered player if it exists, null otherwise
     */
    public Player getFirstPlayer(){
        if(idMap.values().size() > 0){
            return idMap.values().iterator().next();
        }
        return null;
    }

    public List<Player> getPlayers(){
        return new LinkedList<Player>(idMap.values());
    }

    /**
     * Gets the realm a player is within
     * @param player The player
     * @return The realm if it exists, null otherwise
     */
    public Realm getPlayerRealm(Player player){
        Entity playerEntity = player.getPlayerEntity();
        if(playerEntity == null){
            throw new IllegalStateException("Trying to get realm of player who does not have an entity assigned!");
        }
        return Globals.realmManager.getEntityRealm(playerEntity);
    }
    
}
