/*
 * Copyright 2017 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.core.module;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.core.paths.PathManager;
import org.terasology.engine.utilities.download.MultiFileDownloader;
import org.terasology.engine.utilities.download.MultiFileTransferProgressListener;
import org.terasology.gestalt.module.Module;
import org.terasology.gestalt.module.ModuleMetadata;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class ModuleInstaller implements Callable<List<Module>> {

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

    private Iterable<Module> moduleList;
    private ModuleManager moduleManager;
    private MultiFileTransferProgressListener downloadProgressListener;

    ModuleInstaller(ModuleManager moduleManager, Iterable<Module> moduleList,
                    MultiFileTransferProgressListener downloadProgressListener) {
        this.moduleList = moduleList;
        this.downloadProgressListener = downloadProgressListener;
        this.moduleManager = moduleManager;
    }

    @Override
    public List<Module> call() throws Exception {
        Map<URI, Path> filesToDownload = getDownloadUrls(moduleList);
        logger.info("Started downloading {} modules", filesToDownload.size());
        MultiFileDownloader downloader = new MultiFileDownloader(filesToDownload, downloadProgressListener);
        List<Path> downloadedModulesPaths = downloader.call();
        logger.info("Module download completed, loading the new modules...");
        List<Module> newInstalledModules = new ArrayList<>(downloadedModulesPaths.size());
//        ModuleLoader loader = new ModuleLoader(moduleManager.getModuleMetadataReader());
//        loader.setModuleInfoPath(TerasologyConstants.MODULE_INFO_FILENAME);
        for (Path filePath : downloadedModulesPaths) {
            try {
                Module module = moduleManager.getModuleFactory().createModule(filePath.toFile());
                moduleManager.getRegistry().add(module);
                newInstalledModules.add(module);
            } catch (IOException e) {
                logger.warn("Could not load module {}", filePath.getFileName(), e);
            }
        }
        logger.info("Finished loading the downloaded modules");
        return newInstalledModules;
    }

    private Map<URI, Path> getDownloadUrls(Iterable<Module> modules) {
        Map<URI, Path> result = new HashMap<>();
        for (Module module : modules) {
            ModuleMetadata metadata = module.getMetadata();
            String version = metadata.getVersion().toString();
            String id = metadata.getId().toString();
            URI uri = null;
            try {
                uri = RemoteModuleExtension.getDownloadUrl(metadata).toURI();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            String fileName = String.format("%s-%s.jar", id, version);
            Path folder = PathManager.getInstance().getHomeModPath().normalize();
            Path target = folder.resolve(fileName);
            result.put(uri, target);
        }
        return result;
    }
}

@@ -1,13 +1,10 @@
 /
-*%0A *
+/
  Copyrig
@@ -12,586 +12,75 @@
 t 20
-17 MovingBlocks%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */
+21 The Terasology Foundation%0A// SPDX-License-Identifier: Apache-2.0
 %0Apac
@@ -1915,177 +1915,8 @@
 ));%0A
-//        ModuleLoader loader = new ModuleLoader(moduleManager.getModuleMetadataReader());%0A//        loader.setModuleInfoPath(TerasologyConstants.MODULE_INFO_FILENAME);%0A
     
@@ -2034,114 +2034,38 @@
 ger.
-getModuleFactory().createModule(filePath.toFile());%0A                moduleManager.getRegistry().add(module
+registerArchiveModule(filePath
 );%0A 

update for new ModuleManager