package org.studiorailgun.sim.space.gen;

import java.util.List;

import org.studiorailgun.sim.space.Location;
import org.studiorailgun.sim.space.World;
import org.studiorailgun.sim.space.util.LocationResolver;
import org.studiorailgun.Globals;
import org.studiorailgun.sim.character.Character;
import org.studiorailgun.sim.character.gen.CharacterGenerator;
import org.studiorailgun.sim.config.creature.CreatureDef;
import org.studiorailgun.sim.org.Government;

/**
 * Generates a world
 */
public class WorldGenerator {
    
    /**
     * Generates a world
     * @return The world
     */
    public static World generateWorld(){
        World rVal = new World();
        Globals.world = rVal;
        
        //generate the regions
        rVal.setRegion(RegionGenerator.generate("continent"));

        //generate the govs
        WorldGenerator.generateGovernments(rVal);

        //generate the characters
        WorldGenerator.generateCharacters(rVal);

        return rVal;
    }

    /**
     * Populates the world with characters
     * @param world The world
     */
    private static void generateCharacters(World world){
        List<Character> characters = world.getCharacters();

        //create the character
        Character newChar = CharacterGenerator.generateCharacter();
        characters.add(newChar);

        //place the character in the world
        Location placementDest = LocationResolver.first(world);
        newChar.move(placementDest);
    }

    /**
     * Populates the world with governments
     * @param world The world
     */
    private static void generateGovernments(World world){
        for(CreatureDef creatureDef : Globals.config.getCreatureDefManager().getDefinitions()){
            if(creatureDef.getCivBuilder() != null && creatureDef.getCivBuilder()){
                Government creatureGov = new Government("kingdom", "Kingdom of the " + creatureDef.getName());
                world.addGovernment(creatureGov);
            }
        }
    }

}
