package electrosphere.server.datacell;

import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.entity.Scene;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.types.foliage.FoliageUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.entity.types.object.ObjectUtils;
import electrosphere.entity.types.structure.StructureUtils;
import electrosphere.game.server.character.Character;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.net.parser.net.message.NetworkMessage;
import electrosphere.net.server.player.Player;
import electrosphere.server.pathfinding.navmesh.NavMesh;

import java.util.HashSet;
import java.util.Set;

/**
 * Container for entities loaded into memory. This isn't intended to be in charge
 * of simulation. It just acts as an object to relate players and entities by location.
 * This SHOULD be used for networking purposes. This is the mechanism to scope
 * network messages by location. If you are looking for something closer to a scene from
 * a traditional game engine, EntityManager is effectively a scene for all intents and
 * purposes.
 * 
 */
public class ServerDataCell {
    
    //all players attached to this server data cell
    Set<Player> activePlayers = new HashSet<Player>();

    //the navmesh for the data cell
    NavMesh navMesh;

    //the scene backing the server data cell
    Scene scene;

    //controls whether the server data cell simulates its entities or not
    boolean ready = false;    
    
    /**
     * Constructs a datacell based on a virtual cell. Should be used when a player
     * first comes into range of the cell.
     * @param virtualCell 
     */
    ServerDataCell(Scene scene){
        this.scene = scene;
    }
    

    protected static ServerDataCell createServerDataCell(Scene scene){
        return new ServerDataCell(scene);
    }
    
    /**
     * Add a player to the current datacell. This should be done if a player moves
     * into close enough proximity with the cell that they are concerned with
     * the microlevel simulation of the cell. This should _not_ be called if
     * this is the first player coming into range.
     * @param p 
     */
    public void addPlayer(Player p){
        if(!activePlayers.contains(p)){
            activePlayers.add(p);
            serializeStateToPlayer(p);
        }
    }
    
    /**
     * Removes a player from the current datacell. Basically the player has moved
     * far enough away that this cell doesn't concern them anymore.
     * If the number of players for this cell is zero, we should prepare it to be
     * unloaded from memory and converted back into a virtual cell.
     * @param p 
     */
    public void removePlayer(Player p){
        activePlayers.remove(p);
    }

    /**
     * Gets the set of all players in the server data cell
     * @return The set of players in the data cell
     */
    public Set<Player> getPlayers(){
        return activePlayers;
    }
    
    /**
     * This should be used to translate a character from macrolevel simulation to
     * microlevel, datacell based simulation.
     * @param character 
     */
    public void addCharacter(Character character){
        // Entity newEntity = new Entity();
        // loadedEntities.add(newEntity);
        throw new UnsupportedOperationException("Not implemented yet");
    }
    
    /**
     * Broadcast a message to all players within range of this cell.
     * @param message 
     */
    public void broadcastNetworkMessage(NetworkMessage message){
        for(Player player : activePlayers){
            if(player != Globals.clientPlayer){
                player.addMessage(message);
            }
        }
    }

    /**
     * Serializes the given creature to all players in this cell that aren't in the provided list of players
     * whom have already received spawn messages for the entity
     * @param creature The creature entity to spawn
     * @param previousCell The datacell the creature was previously in, which we will use to selectively not send the creature. Pass null if there is no previous cell (eg on first initialization).
     */
    public void initializeEntityForNewPlayers(Entity creature, ServerDataCell previousCell){
        for(Player player : this.activePlayers){
            if(previousCell != null){
                if(!previousCell.containsPlayer(player)){
                    serializeEntityToPlayer(creature,player);
                }
            } else {
                serializeEntityToPlayer(creature,player);
            }
        }
    }
    
    /**
     * Sends the current state of the datacell to the player
     * Commonly, this should be called when a player is added to the cell
     */
    void serializeStateToPlayer(Player player){
        for(Entity entity : scene.getEntityList()){
            serializeEntityToPlayer(entity,player);
        }
    }

    /**
     * Serializes an entity to a given player
     * @param entity The entity to serialize
     * @param player The player to send the entity to
     */
    void serializeEntityToPlayer(Entity entity, Player player){
        if(CreatureUtils.isCreature(entity)){
            CreatureUtils.sendEntityToPlayer(player, entity);
        }
        if(ItemUtils.isItem(entity) && ItemUtils.itemShouldBeSentToClient(entity)){
            ItemUtils.sendEntityToPlayer(player, entity);
        }
        if(ObjectUtils.isObject(entity)){
            ObjectUtils.sendEntityToPlayer(player,entity);
        }
        if(StructureUtils.isStructure(entity)){
            StructureUtils.sendStructureToPlayer(player, entity);
        }
        if(FoliageUtils.isFoliage(entity)){
            FoliageUtils.sendFoliageToPlayer(player, entity);
        }
    }
    
    
    /**
     * Check if player is relevant to cell
     */
    public boolean containsPlayer(Player player){
        return activePlayers.contains(player);
    }


    /**
     * Moves an entity from one datacell to another. This implicitly destroys the entity on player connections that would no longer
     * observe said entity (IE it sends destroy packets). It also initializes the entity for players that would not have already been
     * tracking the entity
     * @param entity The entity to move
     * @param oldCell The old datacell it used to be in
     * @param newCell The new datacell it's moving to
     */
    public static void moveEntityFromCellToCell(Entity entity, ServerDataCell oldCell, ServerDataCell newCell){
        //swap which holds the entity
        if(oldCell != null){
            oldCell.getScene().deregisterEntity(entity);
        }
        newCell.getScene().registerEntity(entity);
        //update entity data cell mapper
        Globals.entityDataCellMapper.updateEntityCell(entity, newCell);
        //send the entity to new players that should care about it
        for(Player player : newCell.activePlayers){
            if(oldCell != null){
                //if the player hasn't already seen the entity, serialize it
                if(!oldCell.containsPlayer(player)){
                    newCell.serializeEntityToPlayer(entity, player);
                }
            } else {
                //if the entity wasn't in a previous cell, send it to all players
                newCell.serializeEntityToPlayer(entity, player);
            }
        }
        //delete the entity for players that dont care about it
        if(oldCell != null){
            for(Player player : oldCell.activePlayers){
                if(!newCell.containsPlayer(player)){
                    //if the player isn't also in the new cell, delete the entity
                    player.addMessage(EntityMessage.constructDestroyMessage(entity.getId()));
                }
            }
        }
    }

    /**
     * Gets the scene backing this data cell
     * @return The scene backing this data cell
     */
    public Scene getScene(){
        return scene;
    }

    /**
     * Sets the nav mesh of this server data cell
     * @param navMesh The nav mesh to store
     */
    public void setNavMesh(NavMesh navMesh){
        this.navMesh = navMesh;
    }

    /**
     * Gets the simulation ready status of the server data cell
     * @return True if ready, false otherwise
     */
    public boolean isReady(){
        return ready;
    }

    /**
     * Sets the simulation ready status of the server data cell
     * @param ready True if ready, false otherwise
     */
    public void setReady(boolean ready){
        this.ready = ready;
    }
    
}
