/*
 * Copyright 2014 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.rendering.nui.layers.mainMenu;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.config.Config;
import org.terasology.engine.SimpleUri;
import org.terasology.engine.module.ModuleManager;
import org.terasology.entitySystem.Component;
import org.terasology.module.Module;
import org.terasology.module.ModuleEnvironment;
import org.terasology.naming.Name;
import org.terasology.reflection.copy.CopyStrategyLibrary;
import org.terasology.reflection.reflect.ReflectFactory;
import org.terasology.registry.CoreRegistry;
import org.terasology.registry.In;
import org.terasology.rendering.nui.CoreScreenLayer;
import org.terasology.rendering.nui.UIWidget;
import org.terasology.rendering.nui.WidgetUtil;
import org.terasology.rendering.nui.layouts.PropertyLayout;
import org.terasology.rendering.nui.properties.PropertyOrdering;
import org.terasology.rendering.nui.properties.PropertyProvider;
import org.terasology.rendering.nui.widgets.ActivateEventListener;
import org.terasology.world.generator.UnresolvedWorldGeneratorException;
import org.terasology.world.generator.WorldConfigurator;
import org.terasology.world.generator.WorldGenerator;
import org.terasology.world.generator.internal.WorldGeneratorInfo;
import org.terasology.world.generator.internal.WorldGeneratorManager;
import org.terasology.world.generator.plugin.WorldGeneratorPluginLibrary;

import java.util.Map;
import java.util.Set;

/**
 * A config screen for world generation
 *
 * @author Martin Steiger
 */
public class ConfigWorldGenScreen extends CoreScreenLayer {

    private static final Logger logger = LoggerFactory.getLogger(ConfigWorldGenScreen.class);

    @In
    private ModuleManager moduleManager;

    @In
    private WorldGeneratorManager worldGeneratorManager;

    @In
    private Config config;

    private Map<String, Component> params;

    @Override
    public void onOpened() {
        super.onOpened();

        // create a fake environment so that plugin facet parts can be loaded
        Set<Module> selectedModules = Sets.newHashSet();
        for (Name moduleName : config.getDefaultModSelection().listModules()) {
            selectedModules.add(moduleManager.getRegistry().getLatestModuleVersion(moduleName));
        }
        ModuleEnvironment environment = moduleManager.loadEnvironment(selectedModules, false);
        CoreRegistry.put(WorldGeneratorPluginLibrary.class, new WorldGeneratorPluginLibrary(environment,
                CoreRegistry.get(ReflectFactory.class), CoreRegistry.get(CopyStrategyLibrary.class)));

        PropertyLayout properties = find("properties", PropertyLayout.class);
        properties.setOrdering(PropertyOrdering.byLabel());
        properties.clear();

        SimpleUri generatorUri = config.getWorldGeneration().getDefaultGenerator();
        WorldGeneratorInfo info = worldGeneratorManager.getWorldGeneratorInfo(generatorUri);

        if (info == null) {
            return;
        }

        try {
            WorldGenerator wg = worldGeneratorManager.createGenerator(info.getUri());
            // set the world seed so that partial initialization happens and the facet system has all facet providers
            wg.setWorldSeed("");
            if (wg.getConfigurator().isPresent()) {
                WorldConfigurator worldConfig = wg.getConfigurator().get();

                params = Maps.newHashMap(worldConfig.getProperties());

                for (String key : params.keySet()) {
                    Class<? extends Component> clazz = params.get(key).getClass();
                    Component comp = config.getModuleConfig(generatorUri, key, clazz);
                    if (comp != null) {
                        params.put(key, comp);       // use the data from the config instead of defaults
                    }
                }

                for (String label : params.keySet()) {
                    PropertyProvider<?> provider = new PropertyProvider<>(params.get(label));
                    properties.addPropertyProvider(label, provider);
                }
            } else {
                logger.info(info.getUri().toString() + " does not support configuration");
            }
        } catch (UnresolvedWorldGeneratorException e) {
            logger.error("Requested unavailable world generator", e);
        }
    }

    @Override
    public void onClosed() {
        SimpleUri generatorUri = config.getWorldGeneration().getDefaultGenerator();
        if (params != null) {
            config.setModuleConfigs(generatorUri, params);
            params = null;
        }
        super.onClosed();
    }

    @Override
    public void initialise() {
        WidgetUtil.trySubscribe(this, "close", new ActivateEventListener() {
            @Override
            public void onActivated(UIWidget button) {
                getManager().popScreen();
            }
        });
    }

}



@@ -946,24 +946,69 @@
 .Component;%0A
+import org.terasology.module.DependencyInfo;%0A
 import org.t
@@ -2866,24 +2866,252 @@
 odules()) %7B%0A
+            Module module = moduleManager.getRegistry().getLatestModuleVersion(moduleName);%0A            selectedModules.add(module);%0A            for (DependencyInfo dependencyInfo : module.getMetadata().getDependencies()) %7B%0A    
             
@@ -3185,21 +3185,47 @@
 ion(
-moduleName));
+dependencyInfo.getId()));%0A            %7D
 %0A   

Fix bug where modules with dependencies caused errors on the world config screen.