/*
 * Copyright 2015 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.terasology.cities;

import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import org.terasology.commonworld.heightmap.HeightMap;
import org.terasology.commonworld.heightmap.HeightMaps;
import org.terasology.commonworld.heightmap.NoiseHeightMap;
import org.terasology.commonworld.symmetry.Symmetries;
import org.terasology.core.world.generator.AbstractBaseWorldGenerator;
import org.terasology.core.world.generator.facetProviders.SeaLevelProvider;
import org.terasology.core.world.generator.facetProviders.World2dPreviewProvider;
import org.terasology.engine.SimpleUri;
import org.terasology.entitySystem.Component;
import org.terasology.world.generation.World;
import org.terasology.world.generation.WorldBuilder;
import org.terasology.world.generator.RegisterWorldGenerator;
import org.terasology.world.generator.WorldConfigurator;

import java.util.Map;

@RegisterWorldGenerator(id = "city", displayName = "City World")
public class CityWorldGenerator extends AbstractBaseWorldGenerator {

    World world;
    private NoiseHeightMap noiseMap;
    private HeightMap heightMap;
    
    /**
     * @param uri the uri
     */
    public CityWorldGenerator(SimpleUri uri) {
        super(uri);
    }

    @Override
    public void initialize() {

        noiseMap = new NoiseHeightMap();
        heightMap = HeightMaps.symmetric(noiseMap, Symmetries.alongNegativeDiagonal());
        
        register(new HeightMapTerrainGenerator(heightMap));
//        register(new BoundaryGenerator(heightMap));
        register(new CityTerrainGenerator(heightMap));
        register(new FloraGeneratorFast(heightMap));

        world.initialize();
    }
    
    @Override
    public void setWorldSeed(String seed) {
        if (seed == null) {
            return;
        }
        
        if (heightMap == null) {
            noiseMap = new NoiseHeightMap();
            heightMap = HeightMaps.symmetric(noiseMap, Symmetries.alongNegativeDiagonal());
        }
        
        noiseMap.setSeed(seed);

        world = new WorldBuilder(0)
                .addProvider(new HeightMapCompatibilityFacetProvider(heightMap))
                .addProvider(new SeaLevelProvider(2))
                .addProvider(new World2dPreviewProvider())
                .build();
        
        super.setWorldSeed(seed);
    }

    @Override
    public Optional<WorldConfigurator> getConfigurator() {

        WorldConfigurator wc = new WorldConfigurator() {

            @Override
            public Map<String, Component> getProperties() {
                Map<String, Component> map = Maps.newHashMap();
                map.put("Terrain", new CityTerrainComponent());
                map.put("Spawning", new CitySpawnComponent());
                return map;
            }

        };

        return Optional.of(wc);
    }

    @Override
    public World getWorld() {
        return world;
    }
}

@@ -634,79 +634,23 @@
 ort 
-com.google.common.base.Optional;%0Aimport com.google.common.collect
+java.util
 .Map
-s
 ;
+%0A
 %0Aimp
@@ -1187,16 +1187,206 @@
 ponent;%0A
+import org.terasology.entitySystem.entity.EntityRef;%0Aimport org.terasology.logic.spawner.FixedSpawner;%0Aimport org.terasology.logic.spawner.Spawner;%0Aimport org.terasology.math.geom.Vector3f;%0A
 import o
@@ -1603,29 +1603,46 @@
 %0Aimport 
-java.util
+com.google.common.collect
 .Map
+s
 ;%0A%0A@Regi
@@ -1784,24 +1784,86 @@
 orld world;%0A
+%0A    private final Spawner spawner = new FixedSpawner(0, 0);%0A%0A
     private 
@@ -1920,20 +1920,16 @@
 ghtMap;%0A
-    
 %0A    /**
@@ -2212,24 +2212,16 @@
 nal());%0A
-        
 %0A       
@@ -2470,20 +2470,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Ov
@@ -2583,32 +2583,24 @@
 ;%0A        %7D%0A
-        
 %0A        if 
@@ -2768,24 +2768,16 @@
       %7D%0A
-        
 %0A       
@@ -2835,17 +2835,16 @@
 Builder(
-0
 )%0A      
@@ -3057,24 +3057,16 @@
 uild();%0A
-        
 %0A       
@@ -3128,17 +3128,8 @@
 lic 
-Optional%3C
 Worl
@@ -3141,17 +3141,16 @@
 igurator
-%3E
  getConf
@@ -3545,16 +3545,170 @@
         
+    @Override%0A            public void setProperty(String key, Component comp) %7B%0A                // TODO Auto-generated method stub%0A            %7D%0A%0A        
 %7D;%0A%0A    
@@ -3722,23 +3722,10 @@
 urn 
-Optional.of(
 wc
-)
 ;%0A  
@@ -3801,10 +3801,150 @@
 ;%0A    %7D%0A
+%0A    @Override%0A    public Vector3f getSpawnPosition(EntityRef entity) %7B%0A        return spawner.getSpawnPosition(getWorld(), entity);%0A    %7D%0A%0A
 %7D%0A

Update for changes in engine world gen interfaces