/*
 * Copyright 2013 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.engine.subsystem.headless.mode;

import org.terasology.config.Config;
import org.terasology.config.WorldGenerationConfig;
import org.terasology.engine.GameEngine;
import org.terasology.engine.SimpleUri;
import org.terasology.engine.TerasologyConstants;
import org.terasology.engine.modes.StateLoading;
import org.terasology.engine.modes.StateSetup;
import org.terasology.engine.module.ModuleManager;
import org.terasology.game.GameManifest;
import org.terasology.module.Module;
import org.terasology.naming.Name;
import org.terasology.network.NetworkMode;
import org.terasology.registry.CoreRegistry;
import org.terasology.world.internal.WorldInfo;
import org.terasology.world.time.WorldTime;

/**
 * The class implements the main game menu.
 * <p/>
 *
 * @author Benjamin Glatzel <benjamin.glatzel@me.com>
 * @author Anton Kireev <adeon.k87@gmail.com>
 * @author Marcel Lehwald <marcel.lehwald@googlemail.com>
 * @version 0.3
 */
public class StateHeadlessSetup extends StateSetup {

    public StateHeadlessSetup() {
    }

    @Override
    public void init(GameEngine gameEngine) {
        super.init(gameEngine);

        GameManifest gameManifest = new GameManifest();

        Config config = CoreRegistry.get(Config.class);
        ModuleManager moduleManager = CoreRegistry.get(ModuleManager.class);
        for (Name moduleName : config.getDefaultModSelection().listModules()) {
            Module module = moduleManager.getRegistry().getLatestModuleVersion(moduleName);
            if (module != null) {
                gameManifest.addModule(module.getId(), module.getVersion());
            }
        }

        WorldGenerationConfig worldGenConfig = config.getWorldGeneration();

        // If no valid default world generator set then try to find one - no option to pick one manually in headless
        if (!worldGenConfig.getDefaultGenerator().isValid()) {

            // find the first gameplay module that is available, it should have a preferred world gen
            for (Name moduleName : config.getDefaultModSelection().listModules()) {
                Module module = moduleManager.getRegistry().getLatestModuleVersion(moduleName);
                if (moduleManager.isGameplayModule(module)) {
                    String defaultWorldGenerator = module.getMetadata().getExtension(ModuleManager.DEFAULT_WORLD_GENERATOR_EXT, String.class);
                    worldGenConfig.setDefaultGenerator(new SimpleUri(defaultWorldGenerator));
                    break;
                }
            }
        }
        SimpleUri worldGeneratorUri = worldGenConfig.getDefaultGenerator();

        gameManifest.setTitle(worldGenConfig.getWorldTitle());
        gameManifest.setSeed(worldGenConfig.getDefaultSeed());

        WorldInfo worldInfo = new WorldInfo(TerasologyConstants.MAIN_WORLD, gameManifest.getSeed(),
                (long) (WorldTime.DAY_LENGTH * 0.025f), worldGeneratorUri);
        gameManifest.addWorld(worldInfo);
        gameEngine.changeState(new StateLoading(gameManifest, NetworkMode.LISTEN_SERVER));
    }

    @Override
    public void dispose() {
        super.dispose();
    }

    @Override
    public void handleInput(float delta) {
    }

    @Override
    public void update(float delta) {
        super.update(delta);
    }

    @Override
    public void render() {
    }

    @Override
    public boolean isHibernationAllowed() {
        return true;
    }
}

@@ -1214,16 +1214,166 @@
 gistry;%0A
+import org.terasology.rendering.nui.layers.mainMenu.savedGames.GameInfo;%0Aimport org.terasology.rendering.nui.layers.mainMenu.savedGames.GameProvider;%0A
 import o
@@ -1461,63 +1461,108 @@
 e;%0A%0A
-/**%0A * The class impl
+import java.util.List;%0A%0A/**%0A * The class is game selection menu replac
 ement
-s
+ for
  the 
-main game menu.%0A * %3Cp/%3E
+headless server.
 %0A *%0A
@@ -1727,19 +1727,45 @@
  * @
-version 0.3
+author Florian %3Cflorian@fkoeberle.de%3E
 %0A */
@@ -1949,24 +1949,451 @@
 meEngine);%0A%0A
+        GameManifest gameManifest = null;%0A        List%3CGameInfo%3E savedGames = GameProvider.getSavedGames();%0A        if (savedGames.size() %3E 0) %7B%0A            gameManifest = savedGames.get(0).getManifest();%0A        %7D else %7B%0A            gameManifest = createGameManifest();%0A        %7D%0A        gameEngine.changeState(new StateLoading(gameManifest, NetworkMode.LISTEN_SERVER));%0A    %7D%0A%0A    private GameManifest createGameManifest() %7B%0A
         Game
@@ -4220,89 +4220,27 @@
     
-gameEngine.changeState(new StateLoading(gameManifest, NetworkMode.LISTEN_SERVER))
+return gameManifest
 ;%0A  

Make the headless server load the game manifest of the previous game.