/*
 * 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.modes.loadProcesses;

import com.google.common.collect.Maps;

import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.module.ModuleManager;
import org.terasology.module.Module;
import org.terasology.naming.NameVersion;
import org.terasology.registry.CoreRegistry;
import org.terasology.engine.GameEngine;
import org.terasology.engine.bootstrap.ApplyModulesUtil;
import org.terasology.engine.modes.LoadProcess;
import org.terasology.engine.modes.StateMainMenu;
import org.terasology.game.Game;
import org.terasology.game.GameManifest;
import org.terasology.network.JoinStatus;
import org.terasology.network.NetworkSystem;
import org.terasology.network.ServerInfoMessage;
import org.terasology.world.internal.WorldInfo;

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

/**
 * @author Immortius
 */
public class JoinServer implements LoadProcess {
    private static final Logger logger = LoggerFactory.getLogger(JoinServer.class);

    private NetworkSystem networkSystem = CoreRegistry.get(NetworkSystem.class);
    private GameManifest gameManifest;
    private JoinStatus joinStatus;

    public JoinServer(GameManifest gameManifest, JoinStatus joinStatus) {
        this.gameManifest = gameManifest;
        this.joinStatus = joinStatus;
    }

    @Override
    public String getMessage() {
        return joinStatus.getCurrentActivity();
    }

    @Override
    public boolean step() {
        if (joinStatus.getStatus() == JoinStatus.Status.COMPLETE) {
            ServerInfoMessage serverInfo = networkSystem.getServer().getInfo();
            gameManifest.setTitle(serverInfo.getGameName());
            for (WorldInfo worldInfo : serverInfo.getWorldInfoList()) {
                gameManifest.addWorld(worldInfo);
            }

            Map<String, Short> blockMap = Maps.newHashMap();
            for (Entry<Integer, String> entry : serverInfo.getBlockIds().entrySet()) {
                String name = entry.getValue();
                short id = entry.getKey().shortValue();
                Short oldId = blockMap.put(name, id);
                if (oldId != null && oldId != id) {
                    logger.warn("Overwriting Id {} for {} with Id {}", oldId, name, id);
                }
            }
            gameManifest.setRegisteredBlockFamilies(serverInfo.getRegisterBlockFamilyList());
            gameManifest.setBlockIdMap(blockMap);
            gameManifest.setTime(networkSystem.getServer().getInfo().getTime());

            ModuleManager moduleManager = CoreRegistry.get(ModuleManager.class);

            Set<Module> moduleSet = Sets.newLinkedHashSet();
            for (NameVersion moduleInfo : networkSystem.getServer().getInfo().getModuleList()) {
                Module module = moduleManager.getRegistry().getModule(moduleInfo.getName(), moduleInfo.getVersion());
                if (module == null) {
                    StateMainMenu mainMenu = new StateMainMenu("Missing required module: " + moduleInfo);
                    CoreRegistry.get(GameEngine.class).changeState(mainMenu);
                    return false;
                } else {

                    logger.debug("Activating module: {}:{}", moduleInfo.getName(), moduleInfo.getVersion());
                    gameManifest.addModule(module.getId(), module.getVersion());
                    moduleSet.add(module);
                }
            }

            CoreRegistry.get(Game.class).load(gameManifest);
            ApplyModulesUtil.applyModules();

            return true;
        } else if (joinStatus.getStatus() == JoinStatus.Status.FAILED) {
            StateMainMenu mainMenu = new StateMainMenu("Failed to connect to server: " + joinStatus.getErrorMessage());
            CoreRegistry.get(GameEngine.class).changeState(mainMenu);
        }
        return false;
    }

    @Override
    public void begin() {
    }

    @Override
    public float getProgress() {
        return joinStatus.getCurrentActivityProgress();
    }

    @Override
    public int getExpectedCost() {
        return 10;
    }
}

@@ -682,17 +682,16 @@
 t.Maps;%0A
-%0A
 import c
@@ -782,183 +782,8 @@
 ry;%0A
-import org.terasology.engine.module.ModuleManager;%0Aimport org.terasology.module.Module;%0Aimport org.terasology.naming.NameVersion;%0Aimport org.terasology.registry.CoreRegistry;%0A
 impo
@@ -974,16 +974,67 @@
 inMenu;%0A
+import org.terasology.engine.module.ModuleManager;%0A
 import o
@@ -1099,16 +1099,95 @@
 nifest;%0A
+import org.terasology.module.Module;%0Aimport org.terasology.naming.NameVersion;%0A
 import o
@@ -1265,16 +1265,16 @@
 System;%0A
-
 import o
@@ -1314,16 +1314,61 @@
 essage;%0A
+import org.terasology.registry.CoreRegistry;%0A
 import o
@@ -3795,17 +3795,16 @@
  else %7B%0A
-%0A
         
@@ -4051,32 +4051,92 @@
  %7D%0A            %7D
+%0A            moduleManager.loadEnvironment(moduleSet, true);
 %0A%0A            Co

Fix multiplayer not activating modules correctly from the gestalt integration.