package electrosphere.game.data.creature.type;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;

/**
 * The creature type loader
 */
public class CreatureTypeLoader {

    /**
     * The map of creature name -> creature data
     */
    Map<String,CreatureData> creatureMap = new HashMap<String,CreatureData>();

    /**
     * The list of playable races
     */
    List<String> playableRaceNames = new LinkedList<String>();

    /**
     * The lock on playable races
     */
    Semaphore playableRaceLock = new Semaphore(1);

    /**
     * Adds creature data to the loader
     * @param name The name of the creature
     * @param type The creature data
     */
    public void putCreature(String name, CreatureData type){
        creatureMap.put(name,type);
    }

    /**
     * Adds a playable race to the loader
     * @param name The race name
     */
    public void putPlayableRace(String name){
        playableRaceNames.add(name);
    }

    /**
     * Gets creature data from the name of a creature
     * @param name The name of the creature
     * @return The creature data if it exists, null otherwise
     */
    public CreatureData getCreature(String name){
        return creatureMap.get(name);
    }

    /**
     * Gets the collection of all creature data
     * @return the collection of all creature data
     */
    public Collection<CreatureData> getCreatures(){
        return creatureMap.values();
    }

    /**
     * Gets the list of playable races
     * @return The list of playable race names
     */
    public List<String> getPlayableRaces(){
        List<String> races = null;
        playableRaceLock.acquireUninterruptibly();
        races = playableRaceNames;
        playableRaceLock.release();
        return races;
    }

    /**
     * Clears the playable race list
     */
    public void clearPlayableRaces(){
        playableRaceNames.clear();
    }

    /**
     * Loads the list of playable races
     * @param races The list of playable races
     */
    public void loadPlayableRaces(List<String> races){
        playableRaceLock.acquireUninterruptibly();
        playableRaceNames = races;
        playableRaceLock.release();
    }


}
